Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

100 Prisoners & 100 Boxes
INTERVIEWPUZZLE

100 Prisoners & 100 Boxes

Explore the famous prisoners-and-boxes puzzle and the cycle-following strategy that dramatically improves the probability of success.

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}(1/2)^\{100\}, which is effectively zero.

2. The Winning Strategy: Pointer Following

Each prisoner uses the numbers as “pointers”:

  1. Prisoner kk first opens box kk.
  2. If it contains number kk, he is done (Success!).
  3. If it contains another number jj, he next opens box jj.
  4. He repeats this until he either finds number kk 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: 15211 \to 5 \to 2 \to 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\le 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.

My Private Notes

Notes are auto-saved locally to this device.