Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Searching with intent
EXPERIMENT 16 / 66

Start with the honest baseline.

Scan deliberately and use a simple correct solution as a comparison baseline.

BY NAVAN SAMI◷ 7 MIN READCOREALGORITHM

01 Model the idea

Linear search checks candidates in a defined order without requiring sorted input. Its worst-case work is proportional to the number of candidates. A baseline like this is valuable: it is easy to understand, often sufficient for small data, and can serve as an oracle when testing a more complex algorithm. Faster asymptotic growth is useful only when its assumptions and overhead fit the problem.

THE THING THAT MUST STAY TRUE

All earlier candidates have been ruled out.

02 State the strategy

Read the steps independently of syntax. Explain what each step preserves before translating it into code.

the_strategy.txt
CHECK each candidate in order
RETURN at the first match
REPORT absence after all candidates fail

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

linear_search.py
def find(values, target):
    for index, value in enumerate(values):
        if value == target: return index
    return None
print(find([9, 3, 7, 3], 3))
print(find([9, 3, 7, 3], 8))
↳ EXPECTED OUTPUT
1
None
  1. 1

    The first occurrence is returned because scanning proceeds from the left. The input can be unsorted.

  2. 2

    An absent target requires examining every element in the worst case. A target at the first position is a constant-work best case.

  3. 3

    Use a known-correct simple version to compare outputs from an optimised implementation on many small inputs, including duplicates and empty input.

04 Count the cost

TIME

O(n) worst case; O(1) best case

SPACE

O(1) auxiliary

Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.

05 Your next move

A SMALL MISSION

Compare a custom membership scan with Python’s in operator for three targets.

Reveal one possible solution +
one_possible_solution.py
def member(values, target):
    return any(value == target for value in values)
values = [9, 3, 7]
for target in [9, 7, 8]: assert member(values, target) == (target in values)
print("Baseline checks passed")

06 Check your reasoning

QUICK SIGNAL CHECK

Does linear search require sorted input?

ANOTHER PATTERN IN YOUR TOOLKIT.

Understand it. Then own it.

Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.