Blog
Binary Search Can Find a Feasible Boundary
Some optimization problems are easier to verify than to construct. Given a candidate capacity, can the workload finish within the deadline? Given a rate limit, does the queue remain bounded? If feasibility changes monotonically, binary search can locate the boundary.
The key is a predicate such as feasible(x). For a minimum-capacity problem, values below the answer are false and values at or above it are true. Binary search finds the first true value without enumerating every possibility.
long low = smallestPossible;
long high = knownFeasible;
while (low < high) {
long mid = low + (high - low) / 2;
if (feasible(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
Most bugs live in the contract, not the loop. Prove monotonicity. Define whether bounds are inclusive. Establish that the upper bound is truly feasible. Use overflow-safe arithmetic in both midpoint calculation and the predicate; a feasibility check that multiplies large values can silently reverse its answer.
Test the boundary directly: the returned value must be feasible, and the previous value must be infeasible when it exists. Include cases where the answer equals the lower or upper bound. These assertions reveal off-by-one errors better than a large random example.
For real-valued answers, iteration count or tolerance replaces exact adjacency. Be explicit about numerical error and whether the final value must be rounded up to remain safe. Binary search does not make a noisy or non-monotonic measurement reliable.
This technique is sometimes called “binary search on the answer,” but the name can obscure its reasoning. You are searching an ordered decision space whose predicate changes once. When that invariant is real and the check is cheaper than direct optimization, the method turns a difficult construction into a controlled sequence of yes-or-no questions.