Binary search appears in interviews because it tests more than coding speed. It tests whether you can reason about
monotonic behavior, invariants, and off-by-one boundaries.
Most candidates remember only one pattern: "find target in sorted array." Interview problems usually ask for one of
three variants:
- Exact match
- Boundary search (first or last valid index)
- Search on answer (binary search over a numeric answer space)
Pattern 1: Exact match
Use this when you need any index where arr[i] === target.
export function binarySearchExact(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
Time complexity is O(log n). Space is O(1).
Pattern 2: Boundary search (lower and upper bound)
This is the most useful interview form. Instead of asking "is it equal?", ask:
- Lower bound: first index
iwherearr[i] >= target - Upper bound: first index
iwherearr[i] > target
These templates are stable and easy to reuse.
export function lowerBound(arr: number[], target: number): number {
let left = 0;
let right = arr.length; // half-open [left, right)
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] < target) left = mid + 1;
else right = mid;
}
return left;
}
export function upperBound(arr: number[], target: number): number {
let left = 0;
let right = arr.length;
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] <= target) left = mid + 1;
else right = mid;
}
return left;
}
To find count of target in sorted array:
count = upperBound(arr, target) - lowerBound(arr, target)
Pattern 3: Search on answer
This is the pattern interviewers like most. The array may not even be sorted. Instead:
- Define a candidate answer
x - Build a feasibility function
can(x)that is monotonic - Binary search the smallest or largest valid
x
Example: minimum ship capacity to deliver packages in d days.
function canShip(weights: number[], days: number, capacity: number): boolean {
let usedDays = 1;
let load = 0;
for (const w of weights) {
if (w > capacity) return false;
if (load + w > capacity) {
usedDays++;
load = 0;
}
load += w;
}
return usedDays <= days;
}
export function minShipCapacity(weights: number[], days: number): number {
let left = Math.max(...weights); // infeasible below this
let right = weights.reduce((sum, x) => sum + x, 0); // always feasible
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (canShip(weights, days, mid)) right = mid;
else left = mid + 1;
}
return left;
}
Interview checklist
When you pick binary search, state these clearly:
- Search space and its bounds
- Monotonic condition
- Invariant in your loop (
[left, right]or[left, right)) - Return meaning
Interviewers care less about memorized code and more about those four points.
Common mistakes
- Mixing closed and half-open intervals in one function
- Using
while (left <= right)with boundary logic meant forwhile (left < right) - Not proving monotonicity for search-on-answer
- Returning
midat loop end when boundary index is required
Practice set
- Find first and last position of element in sorted array
- Search in rotated sorted array
- Koko eating bananas
- Capacity to ship packages within D days
- Minimize max distance to gas station
If you master exact match plus boundary plus search-on-answer, most binary search interview questions collapse into
template work.
