Tier 2Intermediate

Questions to ask before binary searching

“Before applying binary search, what questions do you ask yourself?”

What this tests

  • Whether the candidate understands the real precondition (monotonic predicate), not the superficial one (sorted array).
  • Awareness that random access is required.
  • Ability to distinguish "find the value" from "find the boundary" and pick the right template.
  • Recognition of binary search on the answer.
Pattern RecognitionSystematic ReasoningEdge Cases

Strong answer

The first question is is there a monotonic predicate over the search space? Sortedness is the common special case, but the real requirement is that some yes/no test flips exactly once as you move across the space — false…false true…true. A rotated sorted array is not sorted, yet "is a[mid] in the same sorted half as the target" is checkable, so binary search still works. Without monotonicity, discarding half the space is not justified.

Second, do I have `O(1)` random access? Binary search on a linked list gives no benefit, because reaching the middle is O(n). Third, am I looking for a value or a boundary? Finding an exact value uses lo <= hi and returns on equality; finding the first index where the predicate is true uses lo < hi with hi = mid and never returns early. Mixing the two templates is the source of most infinite loops and off-by-ones.

Fourth, is the search space the input or the answer? In Binary Search on the answer, the space is a range of candidate answers (minimum speed, minimum capacity, maximum distance), and the predicate is a feasibility check that costs O(n). The signal is "minimize the maximum" or "maximize the minimum" phrasing with a feasibility check that is monotonic in the candidate. The candidate should be able to prove monotonicity ("if speed s works, so does s + 1") before searching.

Green flags · Red flags

Green flags
  • Says "monotonic predicate" rather than "sorted".
  • Mentions random access and dismisses binary search on linked lists.
  • Knows the two templates and when each applies.
  • Recognizes search-on-answer from "minimize the maximum" phrasing and proves monotonicity of the feasibility check.
  • Discusses the bounds: lo must be feasible-or-not correctly, hi must be a valid upper bound.
  • Mentions overflow-safe mid computation.
Red flags
  • Thinks binary search only applies to sorted arrays.
  • Cannot state the loop invariant.
  • Writes hi = mid - 1 in a boundary search and does not see why it can skip the answer.
  • Applies binary search on the answer without checking the predicate is monotonic.

Follow-up questions

Each follow-up changes a requirement; the right answer changes with it.

F1
Why does the rotated sorted array still admit binary search?
F2
What are the bounds for "minimum eating speed" in Koko?
F3
When is a linear scan better than binary search?

Related concepts

Practice problem

Koko Eating Bananasmedium