Binary Search on Answer Space: Allocation & Feasibility Checks
Binary Search on Answer Space: Allocation & Feasibility Checks
While traditional binary search finds a value within an explicit sorted array, advanced technical interview problems apply binary search to an abstract range of numerical answers (the "Answer Space"). This paradigm solves optimization problems like "Capacity to Ship Packages Within D Days", "Koko Eating Bananas", and "Book Allocation" in $O(n \log(\text{range}))$ time.
1. Real-World Analogy: Tuning the Perfect Water Faucet Temperature
Imagine adjusting the knob on a shower to find the exact temperature you can tolerate:
- You don't test every single degree between 0°C and 100°C one by one.
- You turn the knob to the middle (50°C). It is scalding hot (Infeasible).
- You know that every temperature above 50°C will also be scalding hot! You instantly eliminate the entire upper half.
- You turn the knob down to 25°C. It's too cold. You narrow your range to 25°C–50°C.
- You are binary searching a monotonic feasibility function!
2. When Can You Binary Search the Answer?
You can apply Binary Search on the Answer Space if the problem satisfies the Monotonicity Condition: $$\text{If an answer } X \text{ is feasible, then every value } > X \text{ (or } < X\text{) is guaranteed to be feasible!}$$
3. Classic Problem: Capacity To Ship Packages Within D Days
A conveyor belt has packages with weights weights[i]. We must ship all packages within days days. Packages must be loaded in order, and each day's total weight cannot exceed the ship's weight capacity. Return the minimum weight capacity of the ship.
The Algorithm:
- 1Search Space:
- Minimum possible capacity:
max(weights)(the ship must carry at least the heaviest single item!). - Maximum possible capacity:
sum(weights)(shipping everything in 1 day).
- 1Feasibility Function (
can_ship_in_days(capacity)):
- Greedily pack items into the current day until exceeding capacity. Count total days required.
- If
days_needed <= days, returnTrue; elseFalse.
4. Multi-Language Implementations: Shipping Capacity
Python 3:
Modern JavaScript / TypeScript:
5. Trace Table: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
low = max(weights) = 10high = sum(weights) = 55
| Iteration | low | high | mid | Days Needed with Capacity mid | Feasible? (<= 5) | Action |
|---|---|---|---|---|---|---|
| 1 | 10 | 55 | 32 | 2 days | Yes | ans = 32, high = 31 |
| 2 | 10 | 31 | 20 | 4 days | Yes | ans = 20, high = 19 |
| 3 | 10 | 19 | 14 | 5 days | Yes | ans = 14, high = 13 |
| 4 | 10 | 13 | 11 | 8 days | No | low = 12 |
| 5 | 12 | 13 | 12 | 7 days | No | low = 13 |
| 6 | 13 | 13 | 13 | 6 days | No | low = 14 (terminates) |
Final Result: Minimum feasible capacity is 15 (or verified at 15)!
Multiple Choice Questions
max(weights)?True while searching for the minimum possible valid capacity, how should the binary search bounds be updated?Elementary Sorting Algorithms: Bubble, Selection & Insertion Sort (O(n²))
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.