Skip to main content
Binary SearchPatterns

Binary Search on the Answer — The Most Underrated Trick

· 7 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Binary search is not just for sorted arrays. The answer-space variant unlocks a whole class of "minimum/maximum satisfying a condition" problems.

The idea

If the answer to a problem is a value in some range [lo, hi], and there is a monotonic feasibility function — meaning "if X is feasible, then X+1 is also feasible" or vice versa — you can binary search on that answer. Instead of searching for a value in an array, you search for the minimum (or maximum) value that satisfies the condition.

The template

lo = minimum possible answer, hi = maximum possible answer. While lo < hi: mid = lo + (hi - lo) / 2. If feasible(mid): hi = mid (looking for the leftmost feasible). Else: lo = mid + 1. Return lo. The feasibility function is where 90% of the problem-specific logic lives.

Classic examples

Koko Eating Bananas: binary search on eating speed; feasibility = can Koko finish all piles within h hours? Minimum Days to Make m Bouquets: binary search on days; feasibility = can we pick m bouquets? Capacity to Ship Packages in D Days: binary search on ship weight; feasibility = can all packages be shipped in D days with this capacity?

Recognition signal

The phrase "minimum X such that condition holds" or "maximum X such that condition holds" is the strongest signal. Also look for: large numeric ranges in constraints (suggesting O(n log n) is expected), and a feasibility check that runs in O(n).

Frequently Asked Questions

What is binary search on the answer space?
Binary search on the answer searches not through an array, but through the possible values of the answer itself. If the answer falls in range [lo, hi] and you can write a feasibility function that checks "is answer X achievable?", you can binary search on X. This converts O(n²) or O(n³) brute force into O(n log n) or O(n²) solutions.
How do I identify a binary search on answer problem?
Look for the phrases "minimum X such that..." or "maximum X such that..." with a large numeric range in the constraints (suggesting the answer itself is what you search). The feasibility check must be monotonic: if X is feasible, then X+1 is also feasible (or vice versa). Classic examples: Koko Eating Bananas, Capacity to Ship Packages, Split Array Largest Sum.
What is the binary search template for answer-space problems?
Set lo = minimum possible answer, hi = maximum possible answer. While lo < hi: mid = lo + (hi - lo) // 2. If feasible(mid): hi = mid (looking for leftmost feasible). Else: lo = mid + 1. Return lo. The feasibility function is where the problem logic lives — it typically runs in O(n) or O(n log n), making the total O(n log(hi-lo)).
What are the most important binary search LeetCode problems?
Must-know binary search problems: Binary Search (LC 704), Find Minimum in Rotated Sorted Array (LC 153), Search in Rotated Sorted Array (LC 33), Koko Eating Bananas (LC 875), Capacity to Ship Packages in D Days (LC 1011), Split Array Largest Sum (LC 410), and Median of Two Sorted Arrays (LC 4 — Hard). The rotated array variants appear frequently at Amazon and Google.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee