Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Linear Search proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Inspect an unsorted sequence in input order and stop only when its occurrence contract is decided. Track inspected positions, duplicates, the missing sentinel and the cost of building an index.
2 practice exercises1 interview problem
Recognize it when One lookup has no useful existing ordering.
Checking your account…
Linear search inspects items in order and stops as soon as the requested occurrence is determined. It needs no ordering or preprocessing.
First, last, any, and all matches are different contracts. First match returns immediately; last match must scan the entire input while updating a candidate index.
def linear_trace(values,target):
trace=[]
for index,value in enumerate(values):
matched=value==target;trace.append([index,value,matched])
if matched:break
return traceImplement linear_trace(values,target). Return [index,value,matched] for inspected items through the first match.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def linear_trace(values,target):
trace=[]
for index,value in enumerate(values):
matched=value==target;trace.append([index,value,matched])
if matched:break
return tracePredict both calls to the existing decision trace. The first stops at the earliest 5; the second has no stopping match and records every input position.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Reject index 0 | matched=value==target;trace.append([index,value,matched]) |
[0, 8, False] |
A failed comparison rules out exactly one position. |
| Accept index 1 | matched=value==target;trace.append([index,value,matched]) |
[1, 5, True] |
Every earlier position is known not to match. |
| Stop | if matched:break |
indexes 2 and 3 uninspected |
The second 5 is irrelevant to a first-occurrence question. |
| Absent call | return trace |
two False decisions |
Exhaustion proves absence only because the whole sequence was inspected. |
| Output | |
|---|---|
print(linear_trace([8, 5, 5, 2], 5)) |
[[0, 8, False], [1, 5, True]] |
print(linear_trace([8, 2], 5)) |
[[0, 8, False], [1, 2, False]] |
Decide first/last/all before writing the loop. A hash index can speed later queries, but duplicate-position lists and original-index semantics must still match the question.
Choose a missing result that cannot be confused with a valid answer, commonly -1 or None. Do not use truthiness for index zero.
def find_first(values,target):
for index,value in enumerate(values):
if value==target:return index
return -1Implement find_first(values,target). Return the first matching index, or -1.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def find_first(values,target):
for index,value in enumerate(values):
if value==target:return index
return -1Use a scan for one or few queries over unsorted data. Build a dictionary, set, or sorted index when many queries justify O(n) preprocessing and extra space. A single scan is O(n) time and O(1) auxiliary space.
Say: “I inspect in input order, and returning here proves this is the first match. If the loop ends, no item satisfies the predicate.” Cover empty input, duplicates, equality semantics, and the missing sentinel.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Linear Search proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| One equality scan | O(n) | O(n) | Best case O(1); assumes equality is constant cost. |
O(1) scan state; the decision-trace Lab stores O(n) inspected rows.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
The first valid position is 0, which is falsy. A truthiness check reports a present match as missing.
Prevent it: Compare the sentinel explicitly.
found = bool(index)found = index != -1Where you will hit this: Count Vowels(opens in a new tab)
Repeated assignment without stopping leaves the last index 2 for target 4 in [4,1,4].
Prevent it: Break as soon as the first match is found.
for i, v in enumerate(values):
if v == target: answer = ifor i, v in enumerate(values):
if v == target:
answer = i
breakWhere you will hit this: Count Vowels(opens in a new tab)
In Python values[-1] is a real last element. It does not mean there is no result.
Prevent it: Check the sentinel before indexing.
value = values[index]value = None if index == -1 else values[index]Where you will hit this: First and Last Target Position(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
python-docs · checked 2026-07-12