Interview patterns
Binary Search Pattern Explained for Coding Interviews
Updated Nov 27, 2026 · 3 min read
Binary search checks the middle of a sorted range and throws away the half that can't hold the target. Every look halves what's left, so 1,000,000 items take about 20 looks, O(log n). Use it when the input is sorted, the question asks for a first or last position, or it wants better than O(n).

Key points
- ●Every look halves the search space: about 20 looks for 1,000,000 items.
- ●Clues: sorted or rotated sorted input, a first or last position, better than O(n).
- ●Loop while lo <= hi, and always move lo or hi past mid.
- ●Forgetting mid + 1 can loop forever; lo < hi with hi = mid - 1 skips an item.
- ●It solves LeetCode 704, 35, 33, 153 and 875.

What binary search does
You have a sorted list and you want to find one value. Reading it from the start takes up to n looks. Binary search does better: look at the middle, and use the order to throw away half the list.
- If the middle is the target, you're done.
- If the middle is too small, the target must be to its right. Drop the left half.
- If the middle is too big, the target must be to its left. Drop the right half.
Every look halves what's left. That's why 1,000,000 sorted items take about 20 looks instead of a million.
How to spot it
Interview questions rarely say "use binary search". They leave clues. The visual sheet above lists them:
- The input is sorted, or sorted and then rotated.
- The question asks for a first or last position, or a boundary.
- You can search on the answer: guess a value, check if it works, and halve the range of guesses.
- The problem wants better than O(n), which usually means O(log n).
If you see one of these, try binary search before anything else.
The three decisions
Every step compares the middle value with the target and makes one of three moves:
| Condition | Action | Reason |
|---|---|---|
| nums[mid] == target | return mid | Found it |
| nums[mid] < target | lo = mid + 1 | The target is to the right of mid |
| nums[mid] > target | hi = mid - 1 | The target is to the left of mid |
Note that both moves go past mid. Mid has already been checked, so there's no reason to keep it.
Trace it: find 7 in 1, 3, 5, 7, 9, 11
| Look | lo | hi | mid | nums[mid] | Move |
|---|---|---|---|---|---|
| 1 | 0 | 5 | 2 | 5 | too small, lo = 3 |
| 2 | 3 | 5 | 4 | 9 | too big, hi = 3 |
| 3 | 3 | 3 | 3 | 7 | found |
Three looks for six items. Notice the last look happens when lo and hi are equal. That's the reason for lo <= hi.
The Python template
def binary_search(nums, target):
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
Both bounds are inclusive: hi starts at the last index, and the loop runs while there's at least one item left between lo and hi. If the loop ends, the range is empty and the target isn't there, so it returns -1.
Complexity
- Time: O(log n), about 20 steps for 1,000,000 items.
- Space: O(1), just
lo,hiandmid.
The two bugs that break it
Most broken binary searches come from mixing up the bounds:
- Using
lo < hiwithhi = mid - 1. When lo and hi meet, there's still one item to check, and this loop skips it. Stick withlo <= hifor the inclusive template. - Forgetting
mid + 1. Writinglo = midmeans that when lo and hi sit side by side, mid lands on lo again and nothing changes. The loop never ends. Always move past mid.
Pick one template, learn it cold and use it everywhere. Mixing parts of different templates is where the off-by-one errors come from.
Problems that use it
The plain version solves LeetCode 704, Binary Search, and 35, Search Insert Position. Rotated arrays are next: 33, Search in Rotated Sorted Array, and 153, Find Minimum in Rotated Sorted Array. LeetCode 875, Koko Eating Bananas, is search on the answer: you binary search the eating speed, not the array.
Frequently asked questions
When should you use binary search in an interview?
When the input is sorted or rotated sorted, when the question asks for the first or last position of something, when you can search on the answer itself, or when the problem wants better than O(n). Any one of those is a strong hint.
What is the time complexity of binary search?
O(log n). Each look cuts the remaining range in half, so 1,000,000 items need about 20 looks. Space is O(1) for the iterative version, since it only keeps lo, hi and mid.
Why do you use lo <= hi instead of lo < hi?
With inclusive bounds, where hi starts at len(nums) - 1 and moves to mid - 1, the range lo to hi still has one item to check when lo equals hi. Stopping at lo < hi skips that last item and can miss the target.
Why does forgetting mid + 1 cause an infinite loop?
If you set lo = mid instead of lo = mid + 1, then when lo and hi are next to each other, mid equals lo and the range never shrinks. Moving past mid guarantees progress every step.