DEV Community

Cover image for Binary Search Explained: How It Works (Visualized)
Iurii Rogulia
Iurii Rogulia

Posted on • Originally published at iurii.rogulia.fi

Binary Search Explained: How It Works (Visualized)

Think of a number. Any number between 1 and 1000.

Don't write it down. Don't tell anyone. Just hold it in your head.

🎮 Interactive demo: play the binary-search game on the original article.


If you just came back — you saw it happen. You answered a few questions, and the computer landed on your number. Not by luck. Every time. For any number you could have picked.

There's something quietly unsettling about that. You were certain you had control. You chose the number. You gave the hints. And yet it felt like the game was over before it started.

The Trick Isn't What You Think

It's not a trick, actually. No hidden camera. No reading your mouse movements. No AI trying to predict human behavior.

What the computer is doing is something much older and stranger: it eliminates half the remaining possibilities with every single guess.

You start with 1,024 possible numbers. After one question, 512 are gone. After two, 512 become 256. Then 128. Then 64. Then 32. Then 16. Then 8. Then 4. Then 2. Then 1.

Ten steps. Any number. Every time.

It works because the space of possibilities — no matter how large — collapses exponentially when you cut it in half repeatedly. 2 to the power of 10 is 1,024. That's more than 1,000, which is why 10 questions are always enough.

You may have noticed the game says 1 to 1,000, not 1 to 1,024. That's intentional. Showing 1,024 would hand you the answer before you'd even started — a power of two is a dead giveaway to anyone who's ever written code. So the upper bound is 1,000, and numbers from 1,001 to 1,024 simply don't exist as far as the game is concerned. If the algorithm's arithmetic ever lands above 1,000, it silently answers "lower" to itself and moves on. You never see it. The math still works.

This is called binary search. It's one of the oldest ideas in computer science, and it shows up everywhere — in databases, in dictionary lookups, in version control, in how your phone's contacts list finds a name in milliseconds.

But the reason I like it as a party trick is that it reveals something about intuition versus math. When you're choosing a number, 1000 feels like a lot. A machine guessing it in 10 steps feels like magic. The reality is neither: it's just that the space of possibilities collapses faster than human intuition expects.

Why I Built This

I build software for a living — mostly EU e-commerce and SaaS systems where the hard problems are in the complexity: tax rules for 35 countries, order automation pipelines, AI integrations that have to be fast and cheap at scale. The pikkuna.fi platform handles 30 languages and 35 countries; the vatnode VAT validation SaaS serves all EU member states. Both rely on the same principle: simple, well-understood algorithms applied at the right layer.

But the ideas underneath all of that are usually simple. Binary search is one of them. I built this game because it's the cleanest demonstration I know of how a small, elegant rule can feel impossible from the outside and obvious once you see it.

If you haven't played yet, go try it. Pick a difficult number. See how many guesses it takes.

It'll be fewer than you expect.

If you're curious about the engineering behind the systems I've built — read UUID v7 in Production: Why Your Database Hates v4 for another case where the simple algorithm underneath turns out to be the important one. Or get in touch if you have a real project to discuss.

Top comments (0)