Pattern

Binary search on the answer

When "does this candidate value work" is monotone, binary search the candidate itself instead of searching an array.

What it is

Binary search on the answer applies binary search not to an array's indices but to the space of possible answers to an optimization question — the smallest speed, capacity, or budget that satisfies some condition. The technique needs two things: a way to check "does this specific candidate value work" (the feasibility check), and a proof that feasibility is monotone — every value above (or below) a working candidate also works. Once both hold, the set of working values is a clean prefix or suffix of the range, and binary search finds its boundary in O(log(range)) evaluations of the feasibility check, regardless of how expensive it would be to try every candidate directly.

When it applies

Tells in the problem

The classic mistake

Trying every candidate value one at a time (or writing a DP over the full input) misses the problem's real signal: monotonicity — missing that "does this speed/capacity work" is a cheap, reusable check and that the answer sits at a clean boundary between infeasible and feasible values. A second, more subtle mistake is assuming the technique generalizes when the underlying assumption breaks: if the problem changes so that items can be reordered rather than processed in a fixed sequence, the simple greedy feasibility check often stops being exact (packing degenerates into bin packing, which is NP-hard), and forcing the same binary-search template onto it silently produces a wrong or unjustified answer. Before applying this pattern, explicitly verify monotonicity — "does making the candidate value larger only ever help, never hurt" — rather than assuming it because the problem superficially resembles one that worked.

Questions to start with

All 3 binary search on the answer questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.