All puzzles › Binary Search
Binary Search Practice Problems
26 puzzles (7 easy, 13 medium, 6 hard), 1 free, in Python, JavaScript and C++.
Binary search throws away half of what is left at every step, which is why it finds one item among a billion in about thirty looks. The idea takes a minute to learn and the details take practice: which half to keep, whether the bounds include their ends, and when the loop stops. These problems drill exactly those details.
How to spot a binary search problem
Sorted input is the obvious sign. The less obvious one is any question with a yes or no answer that flips exactly once as a number grows: can it be done in k days, does this capacity suffice. If the answers go no, no, no, yes, yes, you can binary search for the first yes.
New to it? Read the lesson first: Binary Search.
The puzzles, easiest first
- Binary Search (Easy, free): Given a sorted array and a target, return the target's index, or -1 if it isn't there.
- Guess the Number (Easy): Given n and a secret target between 1 and n, return how many guesses it takes if you always guess the middle of what's left.
- Is It In There? (Easy): Given a sorted array and a target, return true if the target is in it.
- Level Up (Easy): Everyone starts at level 1, and thresholds[i] is the total XP needed to reach level i + 2, in increasing order.
- Perfect Square? (Easy): Given n, return true if it's a perfect square, like 16.
- Where Would It Go? (Easy): Given a sorted array and a target, return the target's index, or the index where it would go to keep the array sorted.
- Which Two Moments? (Easy): Given a sorted list of snapshot times and a target time, return the index of the last snapshot at or before the target.
- Closest Value (Medium): Given a sorted array and a target, return the value closest to the target.
- Complete Coin Rows (Medium): Given n coins, return how many complete rows you can build when row 1 needs 1 coin, row 2 needs 2, and so on.
- Count the Copies (Medium): Given a sorted array and a target, return how many times the target appears.
- Find a Peak (Medium): Given an array, return the index of a peak, a value bigger than its neighbors.
- First Bad Build (Medium): Given a list of builds where false is good and true is broken, and once a build breaks every later one is broken too, return the index of the first broken one, or -1.
- First Occurrence (Medium): Given a sorted array and a target, return the index of its FIRST appearance, or -1.
- How Far Rotated? (Medium): Given a sorted array that was rotated, return how many places it was rotated.
- Integer Square Root (Medium): Given n, return the biggest whole number whose square is n or less.
- Last Occurrence (Medium): Given a sorted array and a target, return the index of its LAST appearance, or -1.
- Next Letter Around (Medium): Given a sorted list of letters and a target letter, return the first letter that comes strictly after the target.
- Range of a Value (Medium): Given a sorted array and a target, return [first index, last index] of the target, or [-1, -1] if it isn't there.
- Search a Sorted Grid (Medium): Given a grid where every row is sorted and each row starts after the last one ends, return true if the target is in it.
- Who Owns This Key (Medium): Given the sorted positions of servers on a hash ring and a key's position, return the position of the server that owns the key: the first server at or after the key.
- Fairest Split (Hard): Given an array and k, split it into k chunks in a row so the biggest chunk sum is as small as possible, and return that sum.
- Load It Before They Get There (Hard): An open world streams assets in as you move.
- Search Rotated Array (Hard): Given a sorted array that's been rotated at some unknown point, like [4, 5, 6, 7, 0, 1, 2], return the index of the target, or -1.
- Slowest Safe Speed (Hard): Given pile sizes and a number of hours, return the slowest eating speed that finishes every pile in time.
- Smallest in a Rotated List (Hard): Given a sorted array that's been rotated, like [3, 4, 5, 1, 2], return its smallest value.
- Smallest Ship (Hard): Given parcel weights in order and a number of days, return the smallest ship capacity that ships everything in time.
Practice another pattern
Or browse by subject
Browse all 676 puzzles