Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Binary Search on Answer invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Binary-search a monotone feasibility predicate when the answer lies in an ordered numeric space. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Binary Search on Answer when the prompt's constraints and required operations match this shape: Binary-search a monotone feasibility predicate when the answer lies in an ordered numeric space.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Sometimes the result is a number rather than an input position. Establish lower and upper bounds that contain every possible answer, then binary-search that numeric domain.
A predicate must switch only once: for a minimization problem, capacities below the answer fail and capacities at or above it succeed. Test extreme candidates before trusting the search.
def capacity_table(weights,days,candidates):
def feasible(capacity):
used=1; load=0
for weight in weights:
if weight>capacity: return False
if load+weight>capacity: used+=1; load=0
load+=weight
return used<=days
return [feasible(value) for value in candidates]Implement capacity_table(weights,days,candidates). Return whether each candidate capacity ships the ordered weights within days.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use a first-true template. A feasible midpoint remains a candidate, so move right to mid; an infeasible midpoint can be discarded with everything below it, so move left to mid plus one.
def minimum_capacity(weights,days):
left,right=max(weights),sum(weights)
def feasible(capacity):
used=1; load=0
for weight in weights:
if load+weight>capacity: used+=1; load=0
load+=weight
return used<=days
while left<right:
mid=(left+right)//2
if feasible(mid): right=mid
else: left=mid+1
return leftImplement minimum_capacity(weights,days). Return the smallest capacity that ships ordered weights within days.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use answer search when feasibility is cheaper than constructing the optimum and the answer domain is ordered. Prefer direct greedy or dynamic programming when it already yields the exact value in comparable time. Total cost is predicate cost times logarithm of the answer range.
Say: “Feasibility is monotone because increasing capacity cannot require more days. My bounds are max item and total sum, and I keep the first feasible boundary.” Distinguish binary searching input indexes from searching possible answers.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Binary Search on Answer invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Binary Search on Answer decision loop | O(log n) | O(log n) | When feasibility changes monotonically as a numeric answer grows, search the answer range instead of enumerating candidates. left is the first unresolved candidate and right is always a feasible candidate, so the smallest feasible value remains inside [left, right]. |
O(1) for the focused Minimize a Feasible Value implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Minimize a Feasible Value before optimizing.
Avoid
def minimum_square_side(item_count):
passUse instead
def minimum_square_side(item_count):
left, right = 0, item_count
while left < right:
mid = (left + right) // 2
if mid * mid >= item_count:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Minimize a Feasible Value(opens in a new tab)
Uses a strict feasibility comparison and overshoots whenever the item count is a perfect square.
Prevent it: Use the public tests and preserve this state: The answer remains inside the current inclusive or half-open bounds.
Avoid
def minimum_square_side(item_count):
left, right = 0, item_count
while left < right:
mid = (left + right) // 2
if mid * mid > item_count:
right = mid
else:
left = mid + 1
return leftUse instead
def minimum_square_side(item_count):
left, right = 0, item_count
while left < right:
mid = (left + right) // 2
if mid * mid >= item_count:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Minimize a Feasible Value(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(log n).
Avoid
def minimum_square_side(item_count):
left, right = 0, item_count
while left < right:
mid = (left + right) // 2
if mid * mid > item_count:
right = mid
else:
left = mid + 1
return leftUse instead
def minimum_square_side(item_count):
left, right = 0, item_count
while left < right:
mid = (left + right) // 2
if mid * mid >= item_count:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Cutting Ribbons(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27