Skip to content
TOPIC · 13 PROBLEMS · 5 PATTERNS

Binary search

Halve the search space with every question. A million options take twenty guesses, as long as each answer tells you which half to throw away.

How to spot a binary search problem

“sorted”The input is already in order. A linear scan wastes that, and the interviewer is waiting for you to notice.
“minimum … such that”Find the smallest speed, size or capacity that works. If a bigger answer always works too, you can search over the answers.
“O(log n)”The statement asks for logarithmic time outright. There is only one usual way to get it.

Five patterns cover almost everything

Classic search

O(log n)

Look at the middle. If the target is bigger, drop the left half, otherwise drop the right. Stop when you hit it or run out.

Reach for it when the array is sorted and you want one exact value.Go deeper: Where log n comes from

Boundary search

O(log n)

Treat the range as a row of false then true. Narrow in on the first true, never stopping early on a match.

Reach for it when you need the first or last position, an insert point, or the first bad version.Go deeper: Where log n comes from

Rotated array

O(log n)

A sorted array cut and swapped still has one sorted half at every middle. Check which half it is, then ask if the target lives there.

Reach for it when the array was sorted once but now starts somewhere in the middle.

Search on the answer

O(n log m)

Guess an answer, check it with a simple pass over the input, and halve the range of guesses depending on whether it worked.

Reach for it when checking a candidate is easy but finding the best one directly is not, and bigger candidates only get easier.

Partition search

O(log min(m, n))

Binary search on where to cut one array. The cut in the other array follows, and you move until both left sides sit below both right sides.

Reach for it when two sorted arrays must be split together, as in finding their combined median.

The one template to memorise

Learn one shape, not five. Find the first value where a yes/no check turns true, and almost every binary search becomes a choice of range and check.

solution.py
def first_true(lo, hi, ok):
# Smallest x in [lo, hi) with ok(x), or hi if there is none.
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
return lo

Problems, easy to hard

Do them in order the first time
#ProblemPatternDifficultySolution
1Binary SearchClassic searchEasyRead
2Search Insert PositionBoundary searchEasyRead
3First Bad VersionBoundary searchEasyRead
4Sqrt(x)Search on the answerEasyRead
5Find First and Last Position of Element in Sorted ArrayBoundary searchMediumRead
6Search a 2D MatrixClassic searchMediumRead
7Find Peak ElementBoundary searchMediumRead
8Find Minimum in Rotated Sorted ArrayRotated arrayMediumRead
9Search in Rotated Sorted ArrayRotated arrayMediumRead
10Koko Eating BananasSearch on the answerMediumRead
11Capacity To Ship Packages Within D DaysSearch on the answerMediumRead
12Split Array Largest SumSearch on the answerHardRead
13Median of Two Sorted ArraysPartition searchHardRead
Next upPrefix sumsStacks