Logarithmic O(log N) search on sorted spaces, monotonic predicates, and answers
318 problems·34 Easy·178 Medium·106 Hard
Pattern Study Guide & Cheat Sheet· O(log(range) * O(check)) · O(1) auxiliary space
▼
Halve the search space at each step over any monotonically ordered sequence or decision boundary (FF...FTTT...T).
Core Invariant: The predicate `feasible(mid)` divides the range into two halves: [False, ..., False, True, ..., True]. Binary search pinpoints the exact boundary where the transition occurs.
Recognize it (Keywords & Signals)
Search target in sorted or rotated sorted array in O(log n)
Find first or last occurrence of an element in a duplicated sorted array
Find minimum or maximum value satisfying a condition (Binary Search on Answer)
Keywords: "minimize the maximum", "maximize the minimum", "capacity to ship", "eating speed"
When NOT to use
Search space is neither sorted nor monotonic (the feasibility predicate does not stay True once satisfied).
How to solve (Step-by-step)
1.Define search boundaries: `low` = minimum possible answer, `high` = maximum possible answer.
4.For minimization: if feasible, record `ans = mid` and search smaller values `high = mid - 1`; else `low = mid + 1`.
5.For maximization: if feasible, record `ans = mid` and search larger values `low = mid + 1`; else `high = mid - 1`.
6.Return recorded `ans`.
Watch for (Interview Traps)
Infinite loop from improper midpoint update: avoid `low = mid` without `+ 1` in `while low <= high`
Setting `low` too small in answer search (e.g. `low = 0` when individual items cannot fit)
Integer overflow in non-Python languages: use `low + (high - low) // 2` instead of `(low + high) // 2`
Binary Search on Answer (Minimize Feasible Value)
def binary_search_on_answer(weights: list[int], days: int) -> int:
def can_ship(capacity: int) -> bool:
needed_days = 1
curr_load = 0
for w in weights:
if curr_load + w > capacity:
needed_days += 1
curr_load = 0
curr_load += w
return needed_days <= days
# Boundaries: minimum single package and total package weight
low, high = max(weights), sum(weights)
best = high
while low <= high:
mid = low + (high - low) // 2
if can_ship(mid):
best = mid # Capacity works, try to find smaller
high = mid - 1
else:
low = mid + 1 # Capacity too small, must increase
return best
Cost
O(log(range) * O(check)) · O(1) auxiliary space (Logarithmic iterations guarantee extremely fast convergence (< 32 iterations for 10^9).)
Canonical problems
#704 Binary Search:Canonical search for value in sorted array
#33 Search in Rotated Sorted Array:One half is always sorted; check if target falls inside it
#875 Koko Eating Bananas:Search eating speed in range [1, max(piles)]
#1011 Capacity To Ship Packages Within D Days:Search minimum ship capacity in range [max(w), sum(w)]
What is the Binary Search & Modified BS pattern in coding interviews?
Logarithmic O(log N) search on sorted spaces, monotonic predicates, and answers It is categorized under Fundamentals and features 318 curated interview problems across Easy (34), Medium (178), and Hard (106) difficulties.
Which tech companies frequently test Binary Search & Modified BS?
How do I master the Binary Search & Modified BS pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.