Binary Search Interview Questions 2026
Binary search extends far beyond finding an element in a sorted array. The key insight is that binary search applies to any monotone decision function, not just arrays.
The Universal Template
Template: lo, hi = 0, len(arr)-1; while lo <= hi: mid = lo + (hi-lo)//2 (avoids overflow). If found: return mid. If too small: lo = mid+1. If too large: hi = mid-1. Return lo for insertion point.
Off-by-one errors are the main failure mode. Know the difference between open/closed interval variants. Verify the invariant: what does lo represent? What does hi represent? The invariant must hold throughout.
Binary Search on Answer
When the problem asks for a minimum/maximum value satisfying some condition, and the condition is monotone (true for all values above/below a threshold), binary search the answer space.
Example: given a sorted array and k, find the smallest divisor such that the sum of ceiling-divided elements is <= threshold. Binary search on divisor (1 to max_element). Check function tests if a given divisor produces sum <= threshold. This transforms O(n * max) to O(n log max).
Rotated Sorted Array
A sorted array rotated at some pivot maintains a useful property: one half is always sorted. Use this to determine which half contains the target.
Template: if arr[lo] <= arr[mid], the left half is sorted. If target is in [arr[lo], arr[mid]), search left; else search right. If the right half is sorted, apply similar logic. Handle duplicates as a special case (arr[lo] == arr[mid]: lo++).
2D Search Problems
Search in a 2D matrix where rows and columns are sorted: start at top-right corner. If target < current, move left. If target > current, move down. Time O(m+n).
Binary search per row: if each row is sorted independently, binary search each row for O(m log n). For a fully sorted matrix (each row starts after the previous row ends), treat it as a 1D sorted array with index math.
Browse Real Binary Search Questions
Real binary search problems asked at top companies.
Browse Binary Search Questions