Binary search & search-space thinking
Beyond sorted arrays: binary search on the answer (minimize max, Koko-eating-bananas class). The monotonic-predicate insight unlocks a whole family.
The mental model
Everyone learns binary search as "find a number in a sorted array": look at the middle, go left or right, halve the search space each time. That is the training-wheels version. The real, powerful idea is this: binary search does not need a sorted array — it needs a yes/no question whose answer flips exactly once as you increase a parameter. If there is a threshold where "no, no, no, YES, yes, yes" turns over, you can find that threshold in O(log range) probes, whether the thing you are searching is an array index, a speed, a capacity, a deadline, or any number at all.
The analogy that sticks: the hot-and-cold guessing game. A friend thinks of a number between 1 and 1,000. Every guess, they tell you "too low" or "too high" — a monotone signal, because once you are too high you stay too high as you go higher. You never scan; you halve. Twenty questions covers a million, thirty covers a billion. The monotone signal is the whole engine, and the profound realization is that most optimization problems can be phrased as this game: "What is the smallest capacity that lets me finish in time?" is "guess a capacity; I'll tell you whether it's enough." Enough-ness is monotone — more capacity is never worse — so you binary-search on the capacity itself.
This is called binary search on the answer (or "binary search on the predicate"), and the roadmap names the signature problem: Koko eating bananas — find the minimum eating speed to finish all bananas within h hours. You do not search an array; you search the *space of possible speeds*, using "can Koko finish at speed s?" as the monotone predicate. The unlock is enormous: a whole family of "minimize the maximum" / "maximize the minimum" / "find the smallest feasible X" problems that look like they need clever DP or greedy insight instead reduce to *guess an answer, check feasibility, halve.* The one idea to carry: if you can (a) guess an answer and (b) check whether it works in a way that is monotone in the guess, you can binary-search the answer — turning an O(n) or exponential search over answers into O(log range × cost-of-one-check).
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision