Teaching Binary Search With a Game

ยท 401 words ยท 2 minute read

Teaching my kid binary search with just words is hard. But make it a game, and they get it right away.

Studies show the best way to learn new concepts is through active engagement, and nothing engages quite like a game where you can immediately see the results of your strategy.

You’ve probably used binary search without knowing it:

  • Finding a word in the dictionary - You open somewhere in the middle, then go forward or backward
  • Looking up someone’s name in a phone book - Same trick
  • Guessing a number game - Just like the game below

Without binary search, finding something in a list of 100 items might take 100 tries. With binary search, it takes only 7 tries at most. That’s a huge difference!

To use binary search - list must be sorted.

How Binary Search Works ๐Ÿ”—

The game above demonstrates the core principle of binary search: eliminating half of the possibilities with each guess. When searching for a number between 0 and 99, you don’t start at 0 and count up - that would take up to 100 guesses. Instead, you start in the middle.

Here’s the strategy:

  1. Start by guessing the middle number (around 50)
  2. If the target is higher, you now know it must be between 51 and 99
  3. If the target is lower, you now know it must be between 0 and 49
  4. Repeat this process with the remaining range

With each guess, you cut the search space in half. This is why binary search is incredibly efficient: searching through 100 numbers takes at most 7 guesses (logโ‚‚ 100). Even searching through a million numbers takes only 20 guesses.

The three difficulty levels in the game teach different aspects:

  • Easy mode greys out impossible numbers, making the elimination pattern visible
  • Medium mode shows the attempt circles but requires you to track possibilities mentally
  • Hard mode offers no visual help - pure binary search thinking

Try playing on Easy first to see the pattern, then challenge yourself with Hard mode.

The Implementation ๐Ÿ”—

A typical binary search function:

function findNumber(array, target):
    left = 0
    right = array length - 1
    
    while left <= right:
        mid = left + (right - left) / 2
        
        if array[mid] == target:
            return mid
        else if array[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    
    return -1  // not found