Binary Search Problems for Coding Interviews

Binary search is not only for sorted arrays: it works whenever you can test a yes or no condition that flips exactly once. That includes searching an answer range (minimum capacity, smallest speed) as well as finding a position.

Most bugs come from off-by-one boundaries. Fix a convention (inclusive or exclusive bounds, which half to keep) and use it every time. Practise lower bound, upper bound, rotated arrays and search on the answer.

Coding problems

Related

Frequently asked questions

How do I get good at binary search problems?

Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.

Can I run binary search problems online here?

Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.