Binary Search Interview Boundary Bugs

Binary Search Interview Boundary Bugs

Binary search interview failures are rarely about the idea “look at the middle.” They are about boundaries: off-by-one left/right updates, unclear return values, and predicates that are not actually monotonic. This guide catalogs the common boundary bugs and gives talk tracks that keep the loop correct under pressure.

The contract you must state

Binary search needs a monotonic predicate on a search space: once the answer flips from false to true (or values go from too small to too big), it never flips back.

Talk track:

“I’ll binary search on X because if a capacity of m works, every larger capacity also works.”

If you cannot say that sentence, do not binary-search yet.

left ← 0
right ← n - 1
while left <= right:
  mid ← left + (right - left) // 2
  if a[mid] == target: return mid
  if a[mid] < target: left ← mid + 1
  else: right ← mid - 1
return -1

Boundary notes:

Template B — lower bound (first true)

left ← 0
right ← n  # half-open [left, right)
while left < right:
  mid ← left + (right - left) // 2
  if predicate(mid):
    right ← mid
  else:
    left ← mid + 1
return left

Use when searching for the first index where a condition holds. Returning left after the loop is the usual answer.

Mid calculation bugs

Left/right update bugs (the infinite loop zone)

MistakeSymptom
right ← mid when you needed mid - 1 with left <= rightInfinite loop or wrong branch
left ← mid without guaranteeing progressInfinite loop when left stays put
Mixing closed and half-open conventionsOff-by-one returns
Updating both pointers wrong on equalitySkips the real answer

Progress rule: every iteration must shrink the search space. If mid can equal left and you set left ← mid, you may stall—use left ← mid + 1 or a different template.

Answer-on-value vs answer-on-index

Bugs appear when the prompt asks for a value (minimum capacity) but you binary-search indices of an array without defining the predicate on the value domain.

Answer-space search talk track:

“Low = max(single pile), high = sum(piles); mid is a candidate capacity; predicate is ‘can finish in H hours.’”

Keep low/high as inclusive values and document what you return when predicate becomes true.

Rotated array and duplicates

First five minutes

  1. Confirm sorted / monotonic predicate
  2. Choose index search vs answer-space search
  3. Pick closed vs half-open template and stick to it
  4. Define what equality means (found / go left for lower bound)
  5. Dry-run two cases: target missing, target at ends

Dry-run checklist (do this on the pad)

If any case stalls the loop on paper, fix the template before coding.

Freeze recovery

  1. Delete the buggy loop; rewrite from the chosen template
  2. Print (mentally) left, right, mid for three iterations
  3. State the predicate in words again
  4. If duplicates confuse you, solve the unique-elements version first and note the limitation

Talk track:

“I’m going to lock a half-open lower-bound template so left is always the first feasible index.”

Practice plan

SessionFocus
1Classic index search + missing target
2Lower/upper bound variants
3Answer-space (Koko / capacity style)
4Rotated array with spoken half choice

Soft practice note

Boundary bugs vanish with slow narration more than with more LeetCode reps alone. If you want a Chrome side panel to practice saying the template and dry-running edges under time pressure, Luna Interview is built for that rehearsal. Use it for calm correctness—not to skip understanding monotonicity or to violate interview rules.

Day-of checklist

Master the boundaries—the binary search interview is mostly that.