100 Prisoners in Solitary Cells

Question: 100 prisoners are stuck in the prison in solitary cells. The warden of the prison got bored one day and offered them a challenge. He will put one prisoner per day, selected at random (a prisoner can be selected more than once), into a special room with a light bulb and a switch which controls the bulb. No other prisoners can see or control the light bulb. The prisoner in the special room can either turn on the bulb, turn off the bulb or do nothing. On any day, the prisoners can stop this process and say “Every prisoner has been in the special room at least once”. If that happens to be true, all the prisoners will be set free. If it is false, then all the prisoners will be executed. The prisoners are given some time to discuss and figure out a solution. How do they ensure they all go free?

Answer: Since this is the only way they will EVER get out of that prison, they decide to work together and make a plan. They select one prisoner (Bob, easier to refer) as the counter.

Every time any prisoner is selected other than Bob, they follow these steps. If they have never turned on the light bulb before and the light bulb is off, they turn it on. If not, they don’t do anything (simple as that). Now if Bob is selected and the light bulb is already on, he adds one to his count and turns off the bulb. If the bulb is off, he just sits there meditates or whatever he wants to. The day his count reaches 99, he calls the warden and tells him “Every prisoner has been in the special room at least once”.

So how does this solution work? Every time a prisoner enters the room first, he turns on the bulb if it is off. This way every prisoner turns on the bulb only once. When Bob enters and sees the bulb on, he knows that one new prisoner has entered  the room so he adds one to his count. So when his counter reaches 99, he knows the rest of them have all been in the special room and obviously, he has been in the special room.

Do they really as these questions in a software interview? They do so better be prepared. Oh wait how can you prepare for such questions? The only thing you can do is practice.

If you have any questions, please feel free to send me an email at [email protected]. If you have any interview questions which you feel would benefit others, I would love to hear about it.

If you're looking for some serious preparation for your interviews, I'd recommend this book written by a lead Google interviewer. It has 189 programming questions and solutions:

Book Cover

21 Responses

  1. Why can’t they write their names on the wall of the room after entering for the first time?

  2. don says:

    Answer – lets assume bulb is off.
    Bob and other prisoner decided that , the person goes first time will turn ON the bulb,if that person goes again and finds bulb is ON then he had to turn it OFF and if it is OFF then do nothing. And bob has to ask every single person about the status of bulb while leaving that room including himself and adding 1 to his count only if he gets reply ‘the bulb is ON ‘ until it reaches to 100.

  3. Great info. I love all the posts, I really enjoyed

  4. Micky Mouse says:

    Solution (or problem) is wrong. Suppose Bob is taken first and then the other 99. And then the warden choses only the others 99 ad infinitum (never Bob again). Even though everybody has visited the room they will not have a chance to ever get out. They will not be free until Bob visits the cell at least 99 times. But this may never happen. even though everybody has visited the room.

  5. sigma says:

    How random is random?
    Assuming a uniform random distribution (actually random). let the prisoners be chosen INDEPENDENTLY. Calculate the P( everyone selected ) < epsilon.

    chance of being chosen within 100 days.
    .67 =1-BINOMDIST(0, 100, 0.01, 1)

    when is
    1-BINOMDIST(0, n, 0.01, 1) = .99

    solve for n:
    .99 =1-BINOMDIST(0, 465, 0.01, 1)

    0.9991196888 = =1-BINOMDIST(0, 700, 0.01, 1)

    Being safe, I would tell everyone to call it in 700 days to have a 99.93% chance of being set free. Depending how impatient we are, 465 days with a 99.00% chance.

  6. Vivek says:

    I don’t think this answer is correct.
    The initial state and mainly the importance of having Bob go in after every prisoner makes this solution a broken one.

    Why can’t they ask each 99 of them, “have you ever been to the room”, or if day-to-day basis, just let the prisoner report his name. when the names r all ticked, its done. it is a random variable on which you have no control. but u can monitor and filter out duplicate entries. thats how you solve it..

  7. manful says:

    The longest time for their release is just 27 years.Nothing big.

  8. MrRabbit says:

    As Vineet pointed out the initial state of the bulb is not known, which means Bob could undercount by one.

    The solution to that is to count each prisoner twice. When Bob reaches a count of 198 each prisoner is guaranteed to have visited the room at least once.

    If the initial state of the bulb was on, one prisoner only visited the cell once. If the initial state was off, each prisoner visited the cell twice.

    Regardless Bob can be certain that each prisoner has visited the cell at least once.

  9. Lis says:

    Each prisoner must turn on the bulb at least once – there is no loss of counts, because if prisoner B enters for the first time and finds the bulb off, he turns it on. If prisoner C – also a first-timer – comes in and sees the light already on, he doesn’t touch it, and Bob will only count B’s first time. HOWEVER prisoner C has NEVER turned the light bulb on – so if he gets picked again, and the bulb is off, he will turn it on, and get counted in this way.

    Them prisoners better be patient!

  10. Alen says:

    The solution to the problem is fine. There is no restriction on how long this takes. It is true that it could potentially take very long for them to figure out when all prisoners have gone through, but using the method described, it is 100% fail safe (i.e. they won’t call it wrong). However, I do see how it could be interpreted as above. A better explanation would be:

    When a prisoner enters the room and he has NEVER turn the light bulb on AND the light bulb is currently off, he flicks on the switch. Thus, all prisoners will flick on the light bulb exactly once and the probability of calling it wrong is 0%. Of course, we won’t be able to tell exactly how long it will take for Bob finish the count (which wasn’t required).

  11. lockie says:

    there’s another solution, although not very good..
    everyone counts the number of days so that they know the beggining of each 100 day cycle. On the first day of the cycle the person turns on the light, and if during the cycle someone enters more than once they turn the light off. If someone enters on the final day of the cycle and the light is on, everyone has been in during that cycle. The chances of this are small though so they will probly all die before they get out anyway.

  12. sailee says:

    well done ….boss
    really a nice answer

  13. ash says:

    The solution to the question is not right as it demands bob to go in every night after a prisoner who has never been there goes in – and if bob happens to miss a particular prisoner’s first time in chance then, all of them are screwed forever!

  14. Vineet says:

    If the initial state of the bulb is not known??
    What if the bulb is ON in the beginning, and Bob is the first one to enter in the room.

  15. Nabs says:

    Isn’t there a risk of Bob loosing counts, i.e. if two new prisoners go in, the light only goes on once. Bob doesn’t know how many new prisoners have been in since his last visit. Another way to put it, if all 99 go in before Bob, but increments his count just by 1. When the others subsequentially go in they will never turn the light on, hence Bob will never increment his count and never get to 100 and they will all be executed.

  16. Doggie says:

    If bob is not allowed more than once, then it is impossible to say “Every prisoner has been in the special room at least once”. Do you have any suggestions if that is the case?

  17. rak says:

    what if bob is not allowed more then once ,even if every other persons enterd more than 1000 times

  18. Ruchi says:

    The second time Sam goes in, he doesn’t do anything. So picking them at alternating frequencies will not change the outcome. Bob will only count it once.

  19. Phoenix says:

    So does BOB has to enter 99 times ?

  20. Mike says:

    You need to mention that if a prisoner has been in the room before, he should turn off the bulb. Otherwise there is the probability that a prisoner Sam is chosen, who turns on the light and then Bob is chosen so Bob increments his count.

    Now if Sam and Bob are chosen at alternating frequencies 100 times in a row, Bob will increment the count to 100 since the light is on every time, though only him and Sam have been in the room.

  21. Sean says:

    Tough Question. Thanks for the post!

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>