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

  1. Binary Search (Easy, free): Given a sorted array and a target, return the target's index, or -1 if it isn't there.
  2. 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.
  3. Is It In There? (Easy): Given a sorted array and a target, return true if the target is in it.
  4. Level Up (Easy): Everyone starts at level 1, and thresholds[i] is the total XP needed to reach level i + 2, in increasing order.
  5. Perfect Square? (Easy): Given n, return true if it's a perfect square, like 16.
  6. 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.
  7. 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.
  8. Closest Value (Medium): Given a sorted array and a target, return the value closest to the target.
  9. 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.
  10. Count the Copies (Medium): Given a sorted array and a target, return how many times the target appears.
  11. Find a Peak (Medium): Given an array, return the index of a peak, a value bigger than its neighbors.
  12. 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.
  13. First Occurrence (Medium): Given a sorted array and a target, return the index of its FIRST appearance, or -1.
  14. How Far Rotated? (Medium): Given a sorted array that was rotated, return how many places it was rotated.
  15. Integer Square Root (Medium): Given n, return the biggest whole number whose square is n or less.
  16. Last Occurrence (Medium): Given a sorted array and a target, return the index of its LAST appearance, or -1.
  17. Next Letter Around (Medium): Given a sorted list of letters and a target letter, return the first letter that comes strictly after the target.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. Load It Before They Get There (Hard): An open world streams assets in as you move.
  23. 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.
  24. Slowest Safe Speed (Hard): Given pile sizes and a number of hours, return the slowest eating speed that finishes every pile in time.
  25. Smallest in a Rotated List (Hard): Given a sorted array that's been rotated, like [3, 4, 5, 1, 2], return its smallest value.
  26. 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