Search the Answer
What is this?
This is the clever leap that surprises everyone: instead of searching through your data, you search through the possible answers. Picture asking "could I finish in 5 hours?" โ if no, you clearly need more time, and if yes, you wonder whether even less would do. Because "is this much enough?" only ever flips from no to yes once as you allow more, you can guess-and-halve your way to the smallest amount that works, even when the answer is a speed, a capacity, or an amount of time rather than a spot in a list.
๐ก Fun fact: "Search the answer" is exactly how engineers size real systems โ to find the smallest server fleet that keeps a website fast, you binary-search the count by repeatedly load-testing a guess, instead of trying every possible number one by one.
๐ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: When a problem asks for the smallest (or largest) value that satisfies some condition, you do not search an array at all โ you binary-search the range of possible answers. If "does answer
xwork?" flips from no to yes exactly once asxgrows, that monotonic boundary is exactly what binary search finds, turning a huge answer space into O(log range) feasibility checks.
The pattern
This is the most powerful idea in the chapter: binary search on the answer. Instead of looking for a position, you guess a candidate answer and ask a yes/no feasibility question โ and the cost of that single guess is usually a quick O(n) pass.
It works only when feasibility is monotonic: once some value is good enough, every larger (or every smaller) value is too. That gives a clean threshold to home in on. The recipe is always the same. First, identify the answer and its bounds โ a lo that is obviously too small and a hi that obviously works. Second, write feasible(x): given candidate x, can the task be done? Third, binary-search for the boundary, biasing toward the smallest x that is feasible (or the largest, depending on the ask).
The skill is recognising the shape: "minimum capacity," "minimum speed," "minimum time," "maximum size that fits" all hide a monotonic feasibility test behind them.
The problems
Koko Eating Bananas โ find the slowest eating speed that clears all piles within h hours. Faster is always feasible, so it is monotonic. Check feasibility of a min speed: for candidate speed k, sum the hours needed and test against h; binary-search the smallest k that fits.
Capacity To Ship Packages Within D Days โ find the smallest ship capacity that delivers everything in D days. Check feasibility of a min capacity: greedily pack days under capacity c, count days used, and search for the smallest c needing at most D days. lo is the largest single package, hi is the total.
Minimum Time to Complete Trips โ find the least total time for buses to finish a quota of trips. Count trips at a time budget: at candidate time t, each bus does t / time[i] trips; sum them and search for the smallest t meeting the quota.
Arranging Coins โ find how many full rows a staircase of n coins can form. The answer is the largest k with k(k+1)/2 โค n โ a triangular number threshold you binary-search directly.
Key takeaways
- Binary-search the answer, not an array. When you want the smallest/largest value that works, the answer space is your search range.
- Feasibility must be monotonic. "Good" values must form one contiguous side; otherwise binary search cannot bracket them.
- Each guess costs one O(n) check. Total cost is O(n log range) โ usually a massive win over brute force.
- Set bounds deliberately. A
lothat is too small and ahithat always works keep the loop correct and the boundary inside. - Why this chapter matters: "search the answer" reframes optimization as search, and it is one of the most common and highest-leverage interview patterns.
Core idea: Don't search for a schedule โ search for the smallest capacity that lets a simple greedy pack everything into D days or fewer.
Problem, rephrased
You run the loading dock for a single cargo ship that makes one round trip per day. A line of crates sits on a conveyor belt in a fixed order, each with a known weight. You must ship them all, and crucially you must load them in the order they arrive โ no cherry-picking from down the line. Each day you load crates onto the ship from the front of the belt until the next crate would push you over the ship's weight limit; then the ship sails and the rest wait for tomorrow.
You get to choose the ship's capacity once, before the first day. A bigger ship costs more, so you want the smallest capacity that still clears the entire belt within D days.
| Input | Meaning | Output |
|---|---|---|
weights = [5,3,8,4,6,2], D = 3 |
6 crates, 3 days | 12 |
weights = [7,2,5,10,8], D = 2 |
5 crates, 2 days | 18 |
weights = [2,3,1,2,4,3], D = 5 |
6 crates, 5 days | 4 |
The answer is always a single number: the minimum capacity.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson