The number guessing game — "I'm thinking of a number between 1 and 100" — is one of the simplest and most universally played games in human history. No equipment needed, no setup, no rules to explain beyond a single sentence. Yet it contains a genuinely profound mathematical insight: the optimal strategy for this game is identical to one of the most important algorithms in computer science.
The game exists in countless cultures and traditions. Parents play it with children on road trips. Teachers use it to introduce programming logic. Mathematics educators use it to demonstrate logarithms. And competitive programmers use it as the entry-level example for binary search.
The Optimal Strategy: Binary Search
Most people playing the number guessing game intuitively understand that they should start near the middle. But do you know exactly why? And do you know that with the correct strategy, you can guarantee solving a 1–100 puzzle in at most 7 guesses?
The strategy is called binary search. On each guess, you should always pick the midpoint of the remaining possible range:
- Range is 1–100. Guess 50.
If the answer is higher → range becomes 51–100.
If lower → range becomes 1–49. - Range is 51–100. Guess 75 (midpoint of 51 and 100). Continue halving.
Each guess cuts the remaining range roughly in half. Starting with a range of 100 numbers, after 1 guess you have at most 50 candidates. After 2 guesses, at most 25. After 3, at most 13. After 4, at most 7. After 5, at most 4. After 6, at most 2. After 7, at most 1 — you've guaranteed the answer.
The formal proof uses logarithms: the number of guesses needed to find any number in a range of N is at most ⌈log₂(N)⌉. For N=100, log₂(100) ≈ 6.64, so 7 guesses suffice. This is the exact limit on our Medium difficulty setting — if you play optimally, you will always win.
Binary Search in Computer Science
Binary search is one of the fundamental algorithms in programming. Any time a computer needs to find an item in a sorted list, binary search reduces an O(n) linear scan to an O(log n) operation. For a list of 1,000,000 sorted items, linear search might take up to 1,000,000 comparisons in the worst case. Binary search takes at most 20. For 1 billion items, binary search needs at most 30 comparisons.
The number guessing game is the intuitive, tangible version of this algorithm. When programmers are taught binary search for the first time, it's almost always explained using this exact game as the motivating example. Playing it — and internalising the "always guess the midpoint" heuristic — gives you the first building block of algorithmic thinking.
Tips for the Hard Level (1–500, 9 guesses)
On Hard mode, the range is 500 and you have 9 guesses. Binary search still works: log₂(500) ≈ 8.97, so 9 guesses is a tight but achievable limit with perfect play. The key is precision: always compute the exact midpoint of your current [low, high] range. Don't approximate — one lazy guess can cost you the win on this difficulty.