100 Doors

Level: Beginner 15–30 min

Concepts: Algorithms

Solutions: C# | TypeScript | Python


There are 100 doors in a row that are all closed. You make 100 passes through the doors. With each pass you toggle the doors state. On each pass you visit the Nth door. That is on the first pass you visit every door. On the second pass you visit every 2nd door. On the third pass you visit every 3rd door and so on until you complete the 100th pass.

  • The first pass you visit every door and open it.
  • The second pass you visit only every second door (#2, #4, #6, …) and close them as you visit.
  • The third pass you visit every 3rd door (#3, #6, #9, …) toggling the door’s state.
    • If the door is closed you open it, it if is open you close it.
  • Continue until you complete the 100th pass only visiting the 100th door.

After 100 passes what is the state of each door? Print which doors are open and which are closed as a single string. Use @ for an open door and # for a closed door.

Examples

The first six doors will look something like this : @@##@@##

Solutions

You can find all language solutions here 100 Doors Solutions

Or you can select a specific language below.

Reference Walkthrough

Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/100-doors. Four scenarios cover the shape — zero doors, one door, ten doors ([1, 4, 9]), and one hundred doors ([1, 4, 9, 16, 25, 36, 49, 64, 81, 100]) — shared across all three languages, each a single pure function int → list[int] returning the 1-indexed open door numbers in ascending order. The reference uses the simulation approach (nested passes toggling boolean state) because the simulation matches the narrative of the problem. The walkthroughs name the perfect-square insight as a teaching note: only perfect-square-indexed doors end open, because only perfect squares have an odd number of divisors. The closed-form sqrt(i)-is-integer implementation is a one-liner; the simulation ships because it reads like the spec.

This kata ships in Agent Full-Bake mode at high gear (F1 tier): the algorithm is small enough to land as one commit per language, with a brief walkthrough noting there are no builders because the algorithm’s inputs and outputs are the domain. No aggregates to construct, no value types to introduce, no collaborators to inject — just openDoors(numDoors). See the repo’s Gears section for when high gear is the right call.