Pattern · 16 problems
Binary Search Interview Problems
Binary search halves the space by asking a yes-or-no question. Everything on one side must answer no.
Free: write the code and run it, no sign-up.
When I reach for it
- The input is sorted, or sorted and then rotated.
- You want a boundary: the first or last place something is true.
- The answer is a number, and you can test any guess cheaply.
Problems, easiest first
3 easy · 9 medium · 4 hard. Open one to read it, then write it in the free editor.
- Missing NumbereasyBit ManipulationArrays & Hashing
- Binary SearcheasyArrays & Hashing
- First Bad Versioneasy
- Longest Increasing SubsequencemediumDynamic ProgrammingArrays & Hashing
- Find Minimum in Rotated Sorted ArraymediumArrays & Hashing
- Search in Rotated Sorted ArraymediumArrays & Hashing
- Find the Duplicate NumbermediumBit ManipulationTwo Pointers
- K Closest Points to OriginmediumHeapsArrays & Hashing
- Kth Largest Element in an ArraymediumHeapsArrays & Hashing
- Search a 2D MatrixmediumArrays & Hashing
- Koko Eating BananasmediumArrays & Hashing
- Time Based Key-Value StoremediumDesignArrays & Hashing
- Median of Two Sorted ArrayshardArrays & Hashing
- Minimum Interval to Include Each QueryhardIntervalsHeaps
- Swim in Rising WaterhardHeapsGraphs
- Maximum Profit in Job SchedulinghardDynamic ProgrammingArrays & Hashing
The idea
Pick a question that splits the space cleanly. Everything below the answer says no. Everything from the answer up says yes. Then you chase the boundary.
Search the answer, not only the array. If you can check one guess quickly, you can find the best guess in logarithmic steps.
lo, hi = 0, len(nums) # search the half-open range [lo, hi)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target: # too small: the answer is right of mid
lo = mid + 1
else:
hi = mid # mid might be the answer: keep it
return lo # first index with nums[i] >= targetWhere it goes wrong
- Off-by-one errors. Pick half-open or closed ranges and stay in one.
- An infinite loop because lo = mid never moves.
- Integer overflow in other languages. Compute mid as lo + (hi - lo) / 2.
Say this before you type. Say the yes-or-no question, and which side each answer throws away.
Where Coco comes in
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.
Questions people ask
- Does binary search only work on sorted arrays?
- It works on anything where a yes-or-no test flips once from no to yes. Sorted arrays are the common case, not the only one.
- How do I stop writing off-by-one bugs?
- Choose a range style before you code and keep every update consistent with it. Then test on a one-item array.
Meet these next
Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.
