This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
If you can tell which half the answer is in, throw the other half away.
The question to ask
Is there a yes/no test that is false up to some point and true after it?
Binary search needs a monotonic yes/no question, not necessarily a sorted array. On a sorted array the question is "is this value at least the target?". On a rotated array it's "which half is sorted, and is the target in it?". On the answer itself it's "can I finish with speed x?", where every x above the minimum also works. Find that boundary and you halve the space each step: O(log n), or O(n log range) when each check is linear.
Want to watch it run first? Step through the interactive walkthrough.
Time O(log n), or O(n log range) on the answer · Space O(1)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Koko Eating Bananas: the speed is the search space, 1 to the largest pile. canFinish is monotonic (faster always still finishes), so find the first speed where it's true. The same shape solves shipping capacity, splitting arrays and minimum days.
function minEatingSpeed(piles: number[], h: number): number {
const canFinish = (speed: number) =>
piles.reduce((hours, pile) => hours + Math.ceil(pile / speed), 0) <= h;
let lo = 1;
let hi = Math.max(...piles);
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (canFinish(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}Find Minimum in Rotated Sorted Array: compare mid with the right end. If nums[mid] > nums[hi], the drop is to the right of mid; otherwise mid could be the minimum, so keep it.
function findMin(nums: number[]): number {
let lo = 0;
let hi = nums.length - 1;
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return nums[lo];
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given a sorted array of integers and a target, return the target's index or -1. You must write an algorithm with O(log n) runtime.
"Sorted" plus "O(log n)". Write the template from memory until it's automatic.
Not attempted yet
Given a sorted array and a target, return the index where the target is, or the insert position where it would go, in O(log n).
"Insert position" is the lower bound, the first index with value at least target. Exactly the template.
Not attempted yet
Versions 1 to n: once one is bad, every later version is bad. Using an isBadVersion API, find the first bad version with as few calls as possible.
"First bad version" over a good...good bad...bad sequence is the textbook monotonic test.
Not attempted yet
Each row is sorted and each row starts after the previous row ends. Search a 2D matrix for a target in O(log(m * n)).
Rows chained in order make one sorted list. Binary search index i as matrix[Math.floor(i / n)][i % n].
Not attempted yet
In a sorted array, find the first and last position of a target value in O(log n), or [-1, -1].
"First and last position": lowerBound(target) and lowerBound(target + 1) - 1. Two boundary searches.
Not attempted yet
Koko eats from piles at k bananas per hour and must finish within h hours. Return the minimum speed k that lets her finish.
"Minimum speed" to finish "within h hours": a faster speed always still works, so binary search the speed.
Not attempted yet
A sorted array of unique values has been rotated. Find the minimum element in O(log n).
"Rotated" sorted plus "O(log n)": compare mid with the right end to find which side holds the drop.
Not attempted yet
A sorted array has been rotated at an unknown pivot. Return the index of target, or -1, in O(log n).
One half around mid is always sorted. Check whether the target lies in that sorted half; if not, it's in the other.
Not attempted yet
Design a store that saves values per key with a timestamp, and returns the value with the largest timestamp at or before a query time.
Timestamps arrive increasing, so each key's list is sorted. "Largest timestamp at or before" is a boundary search.
Not attempted yet
Packages must ship in order within d days. Return the minimum capacity of the ship that makes it possible.
"Minimum capacity" "within d days": binary search capacity from max(weight) to sum(weights), with a greedy day count as the test.
Not attempted yet
Return the median of two sorted arrays in O(log (m + n)) time.
"Median of two sorted" arrays in log time: binary search how many elements the smaller array contributes to the left half.
Not attempted yet