What the Hundred Doors Challenge Actually Is

The Hundred Doors Challenge is a logic and programming problem where you start with 100 doors all closed, then make a series of passes toggling them based on divisors. On pass 1, you toggle every door. On pass 2, you toggle every second door. On pass 3, every third door, and so on through pass 100. After all passes are complete, you figure out which doors remain open. It sounds simple when someone explains it in five minutes. It does not stay simple when you actually try to optimize it or explain why certain doors behave the way they do. Most people stop at writing a simulation that iterates through every door on every pass, which works fine for 100 doors but becomes absurd if you scale up to ten thousand or a million.

How to Solve It

The naive approach uses nested loops. You create an array of 100 booleans, all false, then loop from 1 to 100 on the outside and from the current pass number to 100 on the inside, toggling each door. This runs in roughly O(n squared) time. For n equals 100, that is ten thousand operations. Trivial. But the insight that matters is recognizing that a door ends up open only if it has an odd number of divisors. Most numbers have divisors in pairs. Four has one and four, two and two. Six has one and six, two and three. But perfect squares are different. Sixteen has one and sixteen, two and eight, and four and four. The four gets counted once, making the total divisor count odd. So the open doors are exactly the perfect squares: one, four, nine, sixteen, twenty-five, thirty-six, forty-nine, sixty-four, eighty-one, and one hundred. The optimized solution is a single loop from one to the square root of n. For one hundred doors, that is one iteration per perfect square up to ten. Ten operations instead of ten thousand. You can compute it in constant time relative to the original problem size.

Implementing the Hundred Doors Challenge in Python

Here is the straightforward simulation approach: doors = [False] * 100 for pass_num in range(1, 101): for door in range(pass_num - 1, 100, pass_num): doors[door] = not doors[door] print([i + 1 for i, open in enumerate(doors) if open]) And here is the optimized version that just prints the perfect squares:

Get the Full Details

100 Doors Challenge - Download & Play for Free Here
100 Doors Challenge - Download & Play for Free Here

import math n = 100 print([i * i for i in range(1, int(math.isqrt(n)) + 1)]) Both produce the same result. The second one is what you would use if someone asked you to solve it for a million doors and expected an answer before lunch.

The Problem Nobody Warns You About

I ran into a specific edge case when I was using this challenge as a coding exercise for junior developers. Someone wrote the optimized version correctly but implemented the square root calculation using floating point math instead of an integer square root function. For n equals 999999999999, the floating point result of the square root was something like 999999.9999999999, and depending on how you truncated it, you either missed the last perfect square or included a spurious one. This cost us about forty minutes of debugging because the output looked almost right. The fix was straightforward: use an integer square root function instead of math.sqrt with casting. In Python, that is math.isqrt. In other languages, you either need a library function or a binary search implementation for integer square roots. This is the kind of thing that does not show up in tutorial videos. The tutorial shows you the perfect square trick and moves on. It does not mention that the transition from theory to production code introduces floating point precision issues that can silently corrupt your results at scale.

Where the Approach Breaks Down

The perfect square shortcut only works because this is a deterministic problem with fixed rules. If you modify the challenge—say, you skip every fifth pass or toggle instead of close on the final pass—the divisor logic no longer applies cleanly and you are back to simulating the process. The mathematical insight is elegant but narrow. It solves this exact formulation and nothing close to it without significant modification. There is also the readability problem. If you hand someone the O of n solution without explaining the divisor logic, they will likely ask what the code actually does because the relationship between perfect squares and open doors is not obvious from the output alone. The naive simulation is slower but self-documenting. A reviewer can read it and immediately understand the mechanics. The optimized version requires either a comment or a moment of thought to parse. If you are teaching this concept, the simulation version is almost always the better starting point. The math shortcut is the reward you give students after they have wrestled with the brute force approach and earned the insight.

100 Doors Challenge - Puzzle Escape Game
100 Doors Challenge - Puzzle Escape Game

Common Pitfalls

Off-by-one errors dominate the mistakes I see. Doors are typically numbered one to one hundred in the problem statement but arrays are zero-indexed in most programming languages. Toggling door number pass_num in a zero-indexed array means accessing index pass_num minus one, not pass_num itself. If you get this wrong, every result shifts by one and the perfect square check appears to fail even though your logic is internally consistent. Another frequent mistake is starting the inner loop at zero instead of at the pass number. The problem specifies that on pass k you toggle every kth door, which means you start at door k, not door zero. Starting at zero means you toggle every door on every pass, which is a different problem entirely and produces no interesting pattern. And finally, some implementations forget that pass one toggles every single door. A common offhand assumption is that you start with all doors closed and the first meaningful action happens on pass two. That is wrong. Pass one opens every door. Get this wrong and your simulation starts with the opposite state from the intended problem.

Why This Challenge Persists

The Hundred Doors Challenge survives as a teaching tool because it forces the same progression every time: naive simulation, pattern recognition, mathematical insight, optimization. It is not particularly hard, which means it does not consume a whole interview slot. It is not trivial either, because the jump from simulation to perfect squares requires an actual conceptual leap rather than just syntax knowledge. That balance makes it useful for assessing whether someone can move beyond the first solution that comes to mind. The variant where you ask candidates to handle arbitrary starting states or modified toggle rules separates people who memorized the perfect square trick from people who understand the underlying mechanics. The memorized answer fails immediately when the rules change. The mechanical understanding adapts.

When to Use the Simulation Versus the Shortcut

Use the simulation when n is small and readability matters more than performance. Ten thousand doors with the naive approach takes maybe fifteen milliseconds in Python on a modern machine. There is no reason to optimize that unless you are doing it repeatedly in a loop or running it inside a larger system where that fifteen milliseconds compounds. Use the perfect square shortcut when n is large or when the solution needs to run in real time. Scaling the simulation to ten million doors would take several minutes. The shortcut takes microseconds regardless of how large n gets. If the challenge is a gateway to a system that processes these problems frequently, the shortcut is not optional. The Hundred Doors Challenge is one of those problems that looks like a warm-up exercise and turns into a reasonable filter for understanding. The math is clean. The edge cases are annoying enough to be memorable. And the gap between the naive and optimized approaches illustrates a point about algorithmic thinking that takes longer to explain any other way.

100 Doors Challenge Level 21 to 30 Walkthrough - Walkthroughs.net
100 Doors Challenge Level 21 to 30 Walkthrough - Walkthroughs.net