Binary Searcher — Algorithms

Search by halves: 12 levels of binary search, left and right bounds and targets. The most fundamental search algorithm game for interviews.

What you learn in this game

Binary search works on a sorted array. At each step, we look at the middle element (mid). If the target is larger, search the right. If smaller, search the left.

With 16 elements, binary search needs at most 4 steps (log₂ 16 = 4). Linear search could need up to 16 steps.

Sometimes the target isn't in the array. Binary search ends when the search area is empty (low > high). When that happens, we know the target isn't there.

A variation of binary search: if the target isn't found, determine where the target should be inserted. The insert position = the value of low when the search ends.

What the levels ask

  1. Find the target number with binary search.
  2. Find the target number.
  3. Find the target number.
  4. Search an array of 16 elements: still efficient.
  5. The target is at the start of the array: how many steps?
  6. The target is at the far end. Find it.
  7. Find the target, but is it even in the array?
  8. Search for the target: present or not?
  9. Search for 17 in this array.
  10. At which position should this number be inserted to keep the array sorted?
  11. Where's the insert position for this number?
  12. Where's the insert position for this number?