Binary Search Interview Questions & Solutions
20 solutions ยท 5 Easy ยท 13 Medium ยท 2 Hard
Everyone learns binary search on a sorted array, but interviews reward the candidates who see it everywhere else. The real pattern is more general: whenever you can ask a yes/no question whose answer is monotonic โ true, true, true, then false, false โ you can binary-search for the boundary in O(log n). That reframing is what lets you "binary search on the answer" for problems that never mention a sorted array at all.
The solutions here start with the fundamentals done right โ because off-by-one and infinite-loop bugs in the basic template are shockingly common under pressure โ then build up to the interview favorites: searching in rotated sorted arrays, finding boundaries and peak elements, and the "minimum capacity / smallest feasible value" problems solved by binary-searching the answer space. We're deliberate about loop invariants and the `left`/`right`/`mid` bookkeeping so your implementation is correct the first time. Get comfortable here and a surprising number of "hard" optimization problems reduce to a clean logarithmic search.
- Binary SearchEasy
- Peak Index in a Mountain ArrayEasy
- Search Insert PositionEasy
- Sqrt(x)Easy
- Valid Perfect SquareEasy
- Capacity To Ship Packages Within D DaysMedium
- Find First and Last Position of Element in Sorted ArrayMedium
- Find K Closest ElementsMedium
- Find Minimum in Rotated Sorted ArrayMedium
- Find Peak ElementMedium
- Koko Eating BananasMedium
- Kth Smallest Element in a Sorted MatrixMedium
- Magnetic Force Between Two BallsMedium
- Search a 2D MatrixMedium
- Search a 2D Matrix IIMedium
- Search in Rotated Sorted ArrayMedium
- Search in Rotated Sorted Array IIMedium
- Single Element in a Sorted ArrayMedium
- Median of Two Sorted ArraysHard
- Split Array Largest SumHard
Practice Binary Search the right way
SkillFlow serves you Binary Search problems matched to your level, times you like a real interview, and tracks your proficiency as it climbs. Free.
Start practicing free โ