Pattern 5: Binary search (on index and on answer)
Comes up in: DSA
This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.
What it covers
- invariant-based template (lo/hi, half-open intervals)
- avoiding infinite loops
- lower/upper bound
- rotated arrays
- binary search on the answer space (monotonic predicate)
Words you will know
What you will practise
- binary-search (E)
- first-last-position (M)
- search-rotated-array (M)
- find-min-rotated (M)
- koko-bananas (M)
- median-two-sorted (H, P2)
Interview questions this answers
- Write binary search without off-by-one errors. Explain your loop invariant.
- What does "binary search on the answer" mean? Give an example.