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 support@mytechinterviews.com. If you have any interview questions which you feel would benefit others, I would love to hear about it.
Related posts:


Tough Question. Thanks for the post!
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.
So does BOB has to enter 99 times ?
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.
what if bob is not allowed more then once ,even if every other persons enterd more than 1000 times
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?