Skip to content
← Back to all questions
FreeBrainteaserHard

One Hundred Prisoners and One Hundred Boxes

Asked atOptiverIMC TradingSIG

Question

One hundred prisoners are numbered 1 to 100. In a room are 100 closed boxes, and inside them, in random order, are slips bearing the numbers 1 to 100.

Each prisoner enters alone, may open at most 50 boxes, and must find the slip bearing their own number. Boxes are closed again afterwards and no communication is allowed once the process begins. All prisoners must succeed or all are executed.

They may agree a strategy beforehand. Find a strategy that succeeds with probability greater than 30%.

Solution

Try it yourself first. Open a hint if you get stuck.

Show solution

The naive baseline. If each prisoner opens 50 boxes at random, each succeeds with probability 12\tfrac12, and all succeed with probability 2−100≈10−302^{-100} \approx 10^{-30}.

The strategy. Prisoner kk opens box kk first. Whatever number is on the slip inside, they open that box next, and keep following the slips, up to 50 boxes.

Step 1: The slips form loops. Draw an arrow from each box to the box named by the slip inside it. Every box has exactly one arrow going out (its own slip) and exactly one coming in (exactly one slip names it). So the arrows split the boxes into closed loops.

Eight boxes. Arrows run from each box to the box named by its slip, forming loops of 4, 3 and 1. Prisoner 1 follows boxes 1, 4, 7, 2 and finds slip 1 in box 2.

Step 2: Why this helps. Prisoner kk starts at box kk and walks around the loop that contains box kk. Slip kk is in the box whose arrow points back to box kk, which is the last box of the loop. So prisoner kk finds their slip after exactly as many boxes as the loop has. That means:

  • prisoner kk succeeds exactly when the loop through box kk has at most 5050 boxes;
  • everyone succeeds exactly when no loop has more than 5050 boxes.

Step 3: Count the arrangements with a long loop. All 100!100! ways of putting the slips in the boxes are equally likely. Fix a length ℓ\ell between 5151 and 100100. Every arrangement with a loop of exactly ℓ\ell boxes can be built in three steps:

  1. Choose which ℓ\ell boxes are in the long loop: (100ℓ)\binom{100}{\ell} ways. This is just picking a set of ℓ\ell boxes out of 100100; the order comes in the next step.
  2. Join those ℓ\ell boxes into one loop: (ℓ−1)!(\ell - 1)! ways. Start from the smallest box in the set. The other ℓ−1\ell - 1 boxes can follow it in any order, and each order is a different loop. Once the loop is fixed, so are the slips in these boxes: each box holds the number of the next box.
  3. Fill the other 100−ℓ100 - \ell boxes with the other 100−ℓ100 - \ell slips in any way: (100−ℓ)!(100 - \ell)! ways.

The six ways to join boxes 1, 2, 3, 4 into one loop: box 1 comes first and the other three follow in any of 3! = 6 orders.

No arrangement is counted twice, because two loops longer than 5050 would need more than 100100 boxes. Multiplying, and using ℓ!=ℓ⋅(ℓ−1)!\ell! = \ell \cdot (\ell - 1)!:

(100ℓ)(ℓ−1)! (100−ℓ)!=100!ℓ! (100−ℓ)!⋅(ℓ−1)! (100−ℓ)!=100!ℓ.\binom{100}{\ell}(\ell - 1)!\,(100 - \ell)! = \frac{100!}{\ell!\,(100 - \ell)!} \cdot (\ell - 1)!\,(100 - \ell)! = \frac{100!}{\ell}.

Dividing by the 100!100! arrangements in total:

P(there is a loop of exactly ℓ boxes)=1ℓ.P(\text{there is a loop of exactly } \ell \text{ boxes}) = \frac{1}{\ell}.

Check on a small case: with 44 boxes and a loop of 33, the steps give (43)⋅2!⋅1!=8\binom43 \cdot 2! \cdot 1! = 8, and 4!/3=84!/3 = 8.

Step 4: The probability. At most one loop can be longer than 5050, so these events for different ℓ\ell never happen together, and we can add them:

P(some loop is longer than 50)=151+152+⋯+1100≈0.6882.P(\text{some loop is longer than } 50) = \frac{1}{51} + \frac{1}{52} + \dots + \frac{1}{100} \approx 0.6882.

Answer:

P(all survive)≈1−0.6882=0.3118,P(\text{all survive}) \approx 1 - 0.6882 = 0.3118,

just over 31%.

With more prisoners, 2n2n prisoners each opening nn boxes, the sum 1n+1+⋯+12n\frac{1}{n+1} + \dots + \frac{1}{2n} approaches ln⁡2\ln 2. So the chance of survival only falls to 1−ln⁡2≈0.3071 - \ln 2 \approx 0.307, even with a million prisoners.

Why interviewers like it. Random guessing makes the prisoners succeed or fail independently. Following the loops ties their fates together, so everyone succeeds on the same event: no long loop.