Start with the honest baseline.
Scan deliberately and use a simple correct solution as a comparison baseline.
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.
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.
CHECK each candidate in order
RETURN at the first match
REPORT absence after all candidates fail03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
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))1 None
- 1
The first occurrence is returned because scanning proceeds from the left. The input can be unsorted.
- 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
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
O(n) worst case; O(1) best case
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
Compare a custom membership scan with Python’s in operator for three targets.
Reveal one possible solution +
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
Does linear search require sorted input?
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.