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: The total number of trips finished by a deadline
tonly ever grows astgrows, so binary-search for the smallesttwhose floored per-bus trip-sum reaches the quota.
Problem, rephrased
You run a small fleet of delivery drones. Drone i takes time[i] minutes to fly one round trip (out, drop, back). A drone starts its next round trip the instant it finishes the previous one, and drones fly in parallel, completely independent of each other.
Given a target totalTrips โ the total number of round trips the whole fleet must complete together โ find the minimum number of minutes until the fleet has collectively flown at least totalTrips trips.
The key arithmetic: in t minutes, a drone that takes time[i] per trip completes exactly floor(t / time[i]) full trips. (Half-finished trips don't count.) So the fleet's output at minute t is:
trips_by(t) = sum( t // time[i] for every drone i )
time (min/trip) |
totalTrips |
Answer (min) | Why |
|---|---|---|---|
[2, 3, 5] |
6 |
6 |
At t=6: 3+2+1 = 6 trips. At t=5 only 4. |
[5, 10, 10] |
9 |
25 |
At t=25: 5+2+2 = 9. At t=24 only 8. |
[3, 3] |
4 |
6 |
At t=6: 2+2 = 4. At t=5 only 2. |
[2] |
1 |
2 |
Single drone, first trip lands at t=2. |
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