Here’s a fun riddle posed by a student in the recent Algorithms: Design and Analysis course from Stanford University I participated in on the Coursera platform. I’m recording it here for posterity so I won’t forget. It’s fun to collect such riddles. That might make for cool party tricks. Note that I no longer have access to the original post so all this is paraphrasing from memory.
Riddle:
There is a sadistic warden in charge of a prison with 100 prisoners. He wants to execute these prisoners, but after much persuasion on your part, condescends to play a game with them that could result in some of them from spared. Each of the prisoners is numbered from 1 to 100. A room is set up with 100 drawers, all also numbered 1 to 100. Inside each drawer is a slip of paper, again numbered 1 to 100. However, the placement of each slip of paper in the drawers is randomized.
The game is that each prisoner is allowed to visit this room, one at a time. He is allowed to open the drawers one at a time and may open no more than 50 drawers. If he finds his own number on a slip of paper after having opened 50 or fewer drawers, he is saved. He must return each slip of paper into the drawer he found it in and is not allowed to exchange the positions of the slips of paper.
As an additional concession, you are allowed to visit the room before any of the prisoners may do so and examine the contents of all of the drawers. Furthermore, you may perform one and only one exchange before the game starts. That is after having viewed the contents of all 100 drawers, you may choose any two drawers and exchange the slips of paper inside them. You may not make any other changes and you may not communicate anything about the contents of the drawers to the prisoners.
The riddle is thus: what is the optimal strategy for the prisoners and how many the prisoners may be saved from execution using this strategy? The solution is after the break.