Binary search
Binary search finds a value in sorted data by halving the range each step. A million items take about 20 steps: O(log n).
The signal: sorted input, or a yes/no question whose answer flips once ("the first day the build broke").
Writing it yourself is a classic interview test, and the traps are off by
one. Pick one convention and keep it. Here lo and hi bound a half-open
range [lo, hi):
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target: lo = mid + 1
else: hi = mid
When it stops, lo is the first index whose value is at least target.
In real code, use bisect.bisect_left(nums, target), which does the same.
A common mistake: finding any match, then walking left to the first one. With many duplicates that walk is O(n).
Exercise: finish first_position so it returns the first index of
target in the sorted list nums, or -1. Write the loop yourself. Press
Check to run the tests.