100 Doors in a Row

Problem: you have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), etc, until you only visit the 100th door.

question: what state are the doors in after the last pass? which are open which are closed?

Solution

For example, after the first pass every door is open. on the second pass you only visit the even doors (2,4,6,8…) so now the even doors are closed and the odd ones are opened. the third time through you will close door 3 (opened from the first pass), open door 6 (closed from the second pass), etc..

question: what state are the doors in after the last pass? which are open which are closed?

solution: you can figure out that for any given door, say door #42, you will visit it for every divisor it has. so 42 has 1 & 42, 2 & 21, 3 & 14, 6 & 7. so on pass 1 i will open the door, pass 2 i will close it, pass 3 open, pass 6 close, pass 7 open, pass 14 close, pass 21 open, pass 42 close. for every pair of divisors the door will just end up back in its initial state. so you might think that every door will end up closed? well what about door #9. 9 has the divisors 1 & 9, 3 & 3. but 3 is repeated because 9 is a perfect square, so you will only visit door #9, on pass 1, 3, and 9… leaving it open at the end. only perfect square doors will be open at the end.

2026 Update: The Locker Problem — Square Numbers and Divisor Counting

Classic puzzle: 100 doors, all closed. 100 students walk by. Student k toggles every k-th door (student 1 toggles all, student 2 toggles doors 2,4,6,…, student 100 toggles door 100). Which doors are open at the end?

Answer: Doors with perfect square numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100.

Why? A door ends open iff toggled an odd number of times = iff it has an odd number of divisors = iff it’s a perfect square (since divisors pair up EXCEPT when √n is an integer).

import math

def simulate_doors(n: int) -> list:
    """Simulate the locker problem for n doors."""
    doors = [False] * (n + 1)  # doors[i] = True means open
    for student in range(1, n + 1):
        for door in range(student, n + 1, student):
            doors[door] = not doors[door]
    return [i for i in range(1, n + 1) if doors[i]]

def count_divisors(n: int) -> int:
    count = 0
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            count += 2 if i != n // i else 1
    return count

def open_doors_analytical(n: int) -> list:
    """Perfect squares up to n — O(sqrt(n))."""
    return [k*k for k in range(1, int(n**0.5) + 1) if k*k <= n]

# Verify simulation matches analytical solution
simulated = simulate_doors(100)
analytical = open_doors_analytical(100)
print(simulated)    # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(analytical)   # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(simulated == analytical)  # True

# How many doors open for n=1,000,000?
print(int(math.sqrt(1_000_000)))  # 1000 doors open

# Generalization: what if only odd-numbered students toggle?
# or every prime-th student skips?
def modified_doors(n, toggle_fn):
    """toggle_fn(student) returns list of doors student toggles."""
    doors = [False] * (n + 1)
    for student in range(1, n + 1):
        for door in toggle_fn(student, n):
            if 1 <= door <= n:
                doors[door] = not doors[door]
    return [i for i in range(1, n+1) if doors[i]]

Interview insight: The key leap is recognizing that the simulation question is really a number theory question about divisor counts. This pattern — “find the mathematical structure in a seemingly physical simulation” — appears in many FAANG and trading firm puzzles. Time complexity drops from O(n²) simulation to O(√n) analytical solution.

Scroll to Top