← Create New Paste

Binary Search on the Answer: The Coding Interview Pattern Most Candidates Miss

7 views
Most candidates know binary search as "find a target in a sorted array." In coding interviews, though, one of the most useful variations has nothing to do with a sorted array at all. It is called binary search on the answer (sometimes parametric search), and once you can spot it, a whole family of "hard-looking" problems becomes routine. THE CORE IDEA Instead of searching through the input, you search through the range of possible answers. You need two things: 1. A range [lo, hi] that is guaranteed to contain the answer. 2. A yes/no check, feasible(x), that is monotonic: if x works, every larger x also works (or every smaller x, depending on the problem). When both are true, you can binary search for the smallest (or largest) x where feasible(x) flips from false to true. The check is usually a simple greedy pass over the input in O(n), so the whole solution runs in O(n log R), where R is the size of the answer range. HOW TO RECOGNIZE IT IN AN INTERVIEW Listen for phrasing like: - "Minimize the maximum..." (split an array into k parts so the largest part sum is as small as possible) - "Maximize the minimum..." (place k objects so the smallest distance between any two is as large as possible) - "What is the smallest speed / capacity / time such that..." (ship packages within D days, eat bananas within H hours) If the brute force would be "try every possible answer and check it," and checking is easy, binary search on the answer is almost always the intended solution. A WORKED EXAMPLE Problem: Given package weights and a number of days D, find the minimum ship capacity that ships everything in order within D days. - Range: lo = max(weights) (the ship must carry the heaviest package), hi = sum(weights) (ship everything in one day). - Check: simulate greedily. Fill the current day until the next package would exceed capacity, then start a new day. Feasible if days used <= D. - Monotonic: a bigger ship never needs more days. def ship_within_days(weights, D): def feasible(cap): days, load = 1, 0 for w in weights: if load + w > cap: days += 1 load = 0 load += w return days <= D lo, hi = max(weights), sum(weights) while lo < hi: mid = (lo + hi) // 2 if feasible(mid): hi = mid else: lo = mid + 1 return lo COMMON MISTAKES INTERVIEWERS WATCH FOR - Wrong bounds. Setting lo = 0 or 1 here makes the check return false for reasons unrelated to the search. Always justify both bounds out loud. - Off-by-one loops. Pick one template (lo < hi with hi = mid on success) and use it every time so you are not improvising under pressure. - Forgetting to prove monotonicity. Spend one sentence on why the check is monotonic. Interviewers often care more about that sentence than about the code. - Floating-point answers. For continuous answers, loop a fixed number of times (say 100) instead of comparing floats for equality. HOW TO PRACTICE Take five classic problems (capacity to ship packages, Koko eating bananas, split array largest sum, aggressive cows / magnetic balls, minimum days to make bouquets) and, for each one, write down the range and the feasibility check before writing any code. If you can state those two things in under a minute, the implementation is mechanical. It also helps to rehearse explaining this out loud, because the hard part in a live interview is usually communicating the insight, not typing the loop. Tools like Linkjob AI (https://linkjob.ai) give real-time help during live job interviews and technical interviews, which some candidates use to keep their reasoning structured when nerves kick in. Whatever you use, the goal is the same: recognize the pattern, state the range and the check, and then let the template do the work.
Created Nov 14, 2026 21:06 Expires Nov 14, 2027 21:06