Binary search isn’t just for sorted arrays. The deeper pattern — ‘binary search on the answer space’ — shows up in problems that look nothing like search. Master the three variants and a surprising number of medium/hard problems become tractable.
Sorted array, look up a target. O(log n). Everyone knows the classic; FAANG rarely stops there. Used as a warm-up before the harder variants.
Find first occurrence, last occurrence, smallest index ≥ target. The off-by-one trap. Mastering this unlocks rotated-array problems.
The hardest variant. Search over a value range (not array indices) where the predicate is monotonic. Used for: capacity problems, sqrt, allocate-min-pages.
If you can write a function isFeasible(x) that returns True/False monotonically over x, you can binary-search the answer. Recognizing monotonicity is the skill.
Off-by-one errors kill binary search interviews. The canonical template (left ≤ right, mid = (left+right)//2) avoids most of them. The AI coach grades boundary handling.
The full written guide covers every variant with worked examples. This page is the summary.
Classic binary search is one of three variants. The harder two come up far more often in FAANG mediums than the classic.
Given target x, return its index or -1. The canonical template. O(log n).
Find leftmost index where condition is true, or rightmost. Used in rotated-sorted-array problems and duplicate-handling.
Binary-search over a range of possible answers (not indices). Predicate is monotonic. Used for capacity allocation, square root, ‘minimize the maximum’ type problems.
Off-by-one errors are the leading cause of binary-search bugs in interviews. This template avoids most of them:
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1Key boundary choices: closed interval [left, right], condition left <= right, updates mid + 1 and mid - 1. Consistent with these three, every classic binary search is mechanical.
arr[mid] == target, don’t return — record the index and continue searching left (for first occurrence) or right (for last).left = min_possible_answer, right = max_possible_answer. The predicate replaces the array comparison.left + right, use mid = left + (right - left) // 2 to avoid integer overflow. Rarely matters in Python, often matters in Java/C++.Full deep-dive with worked examples: Binary search — variants and templates.
This is the hardest binary search variant to recognize. Three cues:
isFeasible(x) is True, then isFeasible(x+1) is also True. That monotonicity is the unlock.Three cues. (1) Sorted input — classic binary search. (2) 'First/last occurrence' or 'smallest index where' — boundary search. (3) 'Minimize the maximum' or 'smallest X such that condition holds' — search on answer space. The third is the hardest to spot; practice it specifically.
Off-by-one errors. The boundary conditions (inclusive vs exclusive, left/right updates) trip up candidates under pressure. The fix: memorize ONE canonical template and use it consistently. Don't improvise boundary logic during the interview.
Important. ~15-20% of FAANG coding rounds include a binary search problem, often disguised as 'search on answer space'. The classic version is too easy for FAANG; the boundary and answer-space variants are what they actually test.
Yes — 'parametric search', 'binary search on the answer', and 'binary search over the value range' all describe the same pattern. The common element: a monotonic predicate (isFeasible(x)) that you can evaluate, and you're searching for the smallest/largest x that satisfies it.
Yes. PhantomCode's coach grades boundary-condition handling explicitly — the single biggest source of bugs in binary search interview rounds. The coach also surfaces the 'search on answer space' recognition cue when you describe a problem with a monotonic predicate.
Voice-paced practice with explicit feedback on off-by-one handling. Same coach that runs in your real coding interview.
Start binary search drillsDownload now — invisible, undetectable, and works on every platform. Plans start at $19.