Linear Search
Return the first index holding the target, or -1 when it is absent.
EXECUTION LAB
Return the first index holding the target, or -1 when it is absent.
Input and constraints
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
def solve(values, target): i = 0 while (i < len(values)): if (values[i] == target): return i i = (i + 1) return -1Visualizer
Step 1 of 308i
13
25
33
Inspect the current state
Inspect position 0 for the target 3.
Variable watch
i 0target 3
Step explanation
Inspect position 0 for the target 3.
comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(n) time · O(1) space
Worked explanation
Check numbered lockers in order until the label matches.
Guided solution
Read each position once, stopping at the first match.
O(n) time · O(1) auxiliary space
Examples
Worked example · {"values":[8,3,5,3],"target":3} ⇒ 1
Edge case 1 · {"values":[],"target":4} ⇒ -1
Edge case 2 · {"values":[1],"target":1} ⇒ 0
Progressive hints
Hint 1
Check numbered lockers in order until the label matches.Hint 2
Read each position once, stopping at the first match.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
Stopping one position early misses a match at the very end.
Interview follow-up
Can any algorithm skip positions in an unsorted collection?