Binary Search Applications Beyond Arrays
Fast recall
Quick revision
Core answer
feasible(x) that is false up to some point and then true forever after (or vice versa), you can binary search for the boundary. This 'binary search on the answer' solves problems like minimum shipping capacity, Koko eating bananas, and integer square root in O(log(range) x cost-of-check).Whether you see binary search as a pattern over a monotonic predicate, not only an array operation
Believing binary search needs a physical sorted array, so missing optimization problems that have no array
Spot the 'minimum/maximum value that satisfies a condition' phrasing and map it to a feasible() predicate
Interview-ready guide
Interview answer
Yes, binary search is much more general than searching a sorted array.
The real requirement is a monotonic search space, meaning I can write a decision function that's false for small candidate values and then flips to true at some threshold and stays true. The pattern of answers looks like false, false, false, true, true, true, and binary search just finds where it flips. A great example is Koko eating bananas.
I'm asked for the minimum eating speed so that all the banana piles are finished within a given number of hours. There's no array to search here. The space is the range of possible speeds, from one up to the largest pile.
For each candidate speed I write a feasibility check that adds up how many hours it would take at that speed, and returns true if it fits in the time limit. As speed increases, the hours needed only ever go down, so feasibility is monotonic, which is exactly what binary search needs.
I start with a low and a high speed, take the midpoint, and if that speed is feasible I try something slower, otherwise I speed up. Another example is integer square root, where I binary search the answer from one to n and check whether mid times mid is too big. A useful signal is a problem asking for the smallest or largest value that satisfies a condition.
When a candidate can be checked efficiently and feasibility is monotonic, binary search on the answer replaces a linear scan of possible values with a logarithmic number of checks.
Binary Search Applications Beyond Arrays
Direct Answer
Yes - binary search works on any monotonic search space, not just arrays. If you can write a decision function feasible(x) that is false up to some point and then true forever after (or vice versa), you can binary search for the boundary. This 'binary search on the answer' solves problems like minimum shipping capacity, Koko eating bananas, and integer square root in O(log(range) x cost-of-check).
Deep Dive & Architecture
Binary search is not limited to locating an item in an array. It works on any ordered answer space with a monotonic test: once an answer becomes valid, all larger answers stay valid, or once it becomes invalid, all larger answers stay invalid.
Example: minimum server capacity
Suppose a job must finish within 8 hours. Define canFinish(capacity) to simulate the work and return whether that capacity is enough. Capacity 10 might fail, while 20 succeeds. If 20 succeeds, every capacity above 20 also succeeds. Binary search can find the first successful capacity without checking every number between the lower and upper bounds.
The same idea finds an integer square root, the smallest shipping capacity for a deadline, the minimum eating speed, or the largest feasible distance between placed items. The array is replaced by possible answers, and comparison with a target is replaced by a yes/no feasibility function.
Correctness depends on three things: prove the test is monotonic, choose bounds that contain the answer, and decide whether the boundary sought is the first valid or last valid value. If validity can switch back and forth, binary search is not applicable. With a logarithmic number of tests, total cost is O(log R × cost of one test), where R is the answer range.
Code Example
// Minimum eating speed so all piles finish within h hours.
int minEatingSpeed(int[] piles, int h) {
int lo = 1, hi = Arrays.stream(piles).max().getAsInt();
while (lo < hi) {
int mid = lo + (hi - lo) / 2; // candidate speed
if (feasible(piles, h, mid)) hi = mid; // works -> try slower
else lo = mid + 1; // too slow -> speed up
}
return lo;
}
boolean feasible(int[] piles, int h, int speed) {
long hours = 0;
for (int p : piles) hours += (p + speed - 1) / speed; // ceil divide
return hours <= h;
}
// piles={3,6,7,11}, h=8: speeds 1..11.
// feasible: F F F T T... -> binary search lands on speed 4.
Common Mistakes & Pitfalls
Believing binary search needs a physical sorted array, so missing optimization problems that have no array
Check your understanding
Answer these without looking back at the article.
- How does binary search work and what is its time complexity?
- Why do you use mid = left + (right - left) / 2 instead of (left + right) / 2 in binary search?
- How do you find the first and last occurrence of a number in a sorted array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.