Log inBook a Demo
DebugJuniorBackendAuto-graded

Fix a search that returns the wrong index

Exact reasoning at close quarters, where returning the first match rather than any match is the whole difficulty.search.py searches a sorted list that may contain duplicates. find_first should return the leftmost occurrence of a value and find_last the rightmost — and both are broken in the classic ways. find_first returns any matching index rather than the first, and on some inputs its window stops shrinking so it loops forever. find_last is off by one at the high end and never confirms the value actually sits at the index it returns. Fix both, and resist the urge to replace them with a linear scan.
20 min
python

You're done when

  • find_first returns the leftmost index of the target, not merely some index where it appears.
  • find_last returns the rightmost index, with the off-by-one at the upper bound corrected.
  • Both return -1 when the target is absent, rather than a nearby index.
  • Neither function can loop forever — the search window always shrinks.
  • Both remain genuine binary searches; a linear scan or a built-in shortcut does not count.

What it measures

Data structures and algorithmsGetting binary search boundaries exactly right when duplicates mean 'found it' is not the same as 'found the first one'.
Debugging and profilingSpotting the non-shrinking window that causes the infinite loop, and the missing confirmation that lets a wrong index escape.
Precision under constraintFixing the algorithm in place rather than sidestepping it, which is where off-by-one reasoning is actually tested.

Your environment

Terminal + editorA full shell in the browser
Claude CodePre-authenticated, no key setup
Isolated microVMYours alone, wiped afterwards

Start this lab

20 min
Start labYou'll sign in to Anthropos to start. The lab opens in a private cloud workspace with Claude Code ready to use.
AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo