CS concepts
Binary Search Explained: Every Variant Interviewers Ask
Updated Nov 27, 2026 · 5 min read
Every binary search variant is the same loop: look at the middle, decide which half can't hold the answer, and throw it away. What changes is the question you ask at the middle. Exact match returns on a hit, leftmost and rightmost find the first or last copy, rotated arrays trust the sorted half, search-the-answer searches a range of speeds or sizes, and peak follows the uphill side.

Key points
- ●Each step halves what's left: 1 million items take about 20 looks, a billion about 30.
- ●Exact match uses lo <= hi with hi as the last index; leftmost uses lo < hi with hi as the length and hi = mid.
- ●Rightmost is leftmost with < changed to <=; rightmost minus leftmost counts the copies.
- ●Rotated arrays: one half is always sorted, so only trust that half.
- ●Search the answer when yes/no flips once, like Koko's eating speed: no, no, no, yes, yes, yes.

Why binary search, and why six versions
A million sorted numbers. Find one. You get 20 looks. Every step throws away half of what's left, so 1 million items take about 20 steps and a billion about 30. It's the phone book trick: open the middle, decide which half can't have it, drop that half.
Interviews rarely ask the textbook version. They ask one of six twists, and each one breaks in its own way: an off-by-one here, a loop that never ends there. Every version is the same loop. What changes is the question you ask at the middle and what you keep. The visual sheet above puts all six templates on one page.
Variant 1: exact match (LeetCode 704)
Use it when every value is different and you need to know whether the target is there, and where.
Sorted list 1, 3, 5, 7, 9, 11, find 7. The middle is the 5, too small, so lo = mid + 1. The next middle is 9, too big, so hi = mid - 1. One box left: 7, at index 3. Three looks.
def exact_match(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
Two details carry it: hi is the last index, and the loop runs while lo <= hi, so both ends are fair game.
Variants 2 and 3: leftmost and rightmost (LeetCode 35 and 34)
Now there are repeats: 5, 7, 7, 8, 8, 10. Where's the first 8? Exact match might land on index 4, the wrong one.
So ask a different question at the middle: is this number smaller than the target? Yes means the answer is to the right. No means it's here or to the left, so hi = mid, not mid - 1. You don't throw away a box that might win.
def leftmost(nums, target):
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target: # rightmost: change < to <=
lo = mid + 1
else:
hi = mid
return lo
Here hi starts at the length and the loop is lo < hi. On the example, lo and hi meet at index 3, the first 8. If the target isn't there, it returns where it would go, which is LeetCode 35, Search Insert Position.
For the last 8, change < to <=. Now lo stops on the first number bigger than 8, the 10 at index 5, so the last 8 is at index 4. That's LeetCode 34, Find First and Last Position: the answer is 3 and 4. Two binary searches is still O(log n).
Free trick: rightmost minus leftmost counts the copies, 5 minus 3 is two 8s. In Python, bisect_left and bisect_right already do these two searches and return 3 and 5.
Variant 4: rotated arrays (LeetCode 153 and 33)
Take a sorted list, cut it and move the front to the back: 15, 18, 22, 3, 7, 11. It isn't sorted anymore, but one half always is.
Minimum (153). Compare the middle with the right end. Middle bigger? The drop is on the right, so lo = mid + 1. Otherwise the smallest is the middle or left of it, so hi = mid. 22 beats 11, go right; 7 is under 11, keep it and go left; 3 is under 7. Found 3 in three peeks.
Target (33). Find 7. First find the sorted half: left end 15, middle 22, so the left half is in order. Does 7 fit between 15 and 22? No, so drop the whole left half. What's left is 3, 7, 11, and the middle is 7, at index 4. You only trust the half that's sorted, because that's the only half where you can check a range.
Variant 5: search the answer (LeetCode 875)
This one wins interviews: you binary search the answer, not the list.
Koko Eating Bananas. Piles of 3, 6, 7 and 11, and 8 hours. Each hour she eats up to k bananas from one pile. What's the slowest speed that still finishes? Too slow fails, fast enough works, and once a speed works every faster one does too. No, no, no, yes, yes, yes: a sorted list in disguise.
def min_eating_speed(piles, h):
lo, hi = 1, max(piles)
while lo < hi:
mid = (lo + hi) // 2
if sum((p + mid - 1) // mid for p in piles) <= h:
hi = mid
else:
lo = mid + 1
return lo
Speed 6 takes 6 hours, works. 3 takes 10, too slow. 5 takes 8, works. 4 takes 1 + 2 + 2 + 3 = 8, works. The answer is 4, found in 4 looks. It's the leftmost template again: find the first yes. Questions about minimum speed, minimum capacity or the smallest maximum, like LeetCode 1011, use the same code with a different check.
Variant 6: peak (LeetCode 162)
A peak is bigger than both neighbors, and the list isn't sorted at all. In 1, 2, 3, 1 the peak is the 3. Compare the middle with its right neighbor. If the right one is bigger you're walking uphill, so a peak must be to the right: lo = mid + 1. Otherwise it's here or to the left: hi = mid.
Binary search doesn't need sorted data. It needs a question that tells you which half to keep.
The four bugs
lo = midinstead ofmid + 1. With two boxes left, mid lands on lo, nothing moves, and the loop runs forever.- Mixing templates.
lo <= higoes with hi as the last index;lo < higoes with hi as the length. - Overflow. In 2006, Joshua Bloch at Google wrote that the binary search he'd put in Java had a bug:
lo + higot too big for an int and wrapped negative. The fix islo + (hi - lo) // 2. Python ints don't overflow, but mention it in a Java or C++ interview. - Trusting the leftmost answer. If the target is bigger than everything,
locomes back as the length of the list. Check it's in range and really the target before you read it.
Which variant?
| The question asks | Variant |
|---|---|
| Is it here, and where? | Exact match |
| First copy, or where it would go | Leftmost |
| Last copy, or how many copies | Rightmost (minus leftmost) |
| A sorted list that was cut and rotated | Trust the sorted half |
| Minimum speed, capacity or smallest maximum | Search the answer |
| Any peak in an unsorted list | Follow the uphill side |
Frequently asked questions
What is the difference between lo <= hi and lo < hi in binary search?
They go with different ranges. lo <= hi pairs with hi as the last index, so both ends are checked, and suits exact match. lo < hi pairs with hi as the length of the list and suits leftmost and rightmost searches. Mixing the two makes you miss the last box or run off the end.
Why does binary search loop forever sometimes?
Usually because of lo = mid instead of lo = mid + 1. With two boxes left, mid lands on lo, nothing moves, and the loop never ends.
What do bisect_left and bisect_right return in Python?
bisect_left is the leftmost search and bisect_right is the rightmost one. On [5, 7, 7, 8, 8, 10] with target 8 they return 3 and 5. On [2, 4, 4, 4, 9], bisect_right for 4 returns 4, the position just after the last 4.
When should I binary search the answer instead of the array?
When the question asks for a minimum speed, minimum capacity or smallest maximum, and once a value works every bigger value works too. Koko Eating Bananas (LeetCode 875) and shipping packages within D days (LeetCode 1011) are the classic examples.