Prisoners & 100 Boxes
The Puzzle: 100 prisoners are numbered 1 to 100. A room contains 100 boxes, each with a unique prisoner’s number inside (shuffled randomly).
- One by one, each prisoner enters the room and can open 50 boxes.
- They must find the box containing their own number.
- They must leave the room exactly as they found it and cannot communicate with prisoners who haven’t entered yet.
- The Rule: If all 100 prisoners find their own numbers, they are set free. If even one fails, they are all executed. What is their best strategy?
1. The Random approach
If everyone picks 50 boxes randomly, the probability of success is (1/2){100}, which is effectively zero.
2. The Winning Strategy: Pointer Following
Each prisoner uses the numbers as “pointers”:
- Prisoner k first opens box k.
- If it contains number k, he is done (Success!).
- If it contains another number j, he next opens box j.
- He repeats this until he either finds number k or has opened 50 boxes.
3. Why it Works
This setup divides the boxes into Cycles. For example: Box 1 has 5, Box 5 has 2, Box 2 has 1. (Cycle: 1→5→2→1).
- A prisoner will find their number if and only if they are part of a cycle of length 50 or less.
- The group succeeds if there is no cycle longer than 50 in the permutation.
- The Probability: Mathematically, the chance of a random permutation having no cycle longer than 50 is about 31%.
Interview-Focused Questions
Q: Why is 31% so much better than zero?
A: In the random strategy, the failures are independent. In the pointer strategy, the failures are highly correlated. If the room has a “bad” cycle (length 51), all 51 prisoners in that cycle will fail together. If the room is “good,” almost everyone succeeds together.
Q: How would you explain this using Graph Theory?
A: Consider each box as a node and the number inside as a directed edge. Since every node has exactly one incoming and one outgoing edge, the graph is a collection of disjoint Cycles. The puzzle is simply asking for the probability that the longest cycle in a random graph of 100 nodes is ≤100/2.
Key Takeaway
This is the ultimate FAANG puzzle. It tests your knowledge of Permutations and your ability to see “hidden structures” (cycles) in random data.
Premium Content
Unlock 100 Prisoners & 100 Boxes and all premium lessons with a subscription.
From ₹199.99/year — See plans