100 Prisoners in Solitary Cells
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?
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.
Your Answer
More puzzles to try-
How many times a day do all three hands of analog watch overlaps?
There is a perfectly accurate analog watch with hour, minute, and second hands. How many times a day do all three ...Read More »Musical Animal
It is a fruit. If you take away the first letter of its name it becomes a crime. Take away ...Read More »What does this say
What does below chemistry rebus means?Read More »Which Travels Faster
Which Travels Faster? Solve below famous picture riddle:Read More »Find the number
Double it and multiply it by 4. Then divide it by 8 and you’ll have it once more. What number ...Read More »Mailman
Two friends, Denis and Léo wish to communicate by mail. The mailman is dishonest. He steals. Each of the two ...Read More »Shoes Company Rebus Puzzle
What shoe company does this rebus represent?Read More »Ship with coins riddle
A pirate ship captures a treasure of 1000 golden coins. The treasure has to be split among the five pirates: ...Read More »How can you figure out?
There are 3 switches downstairs, each corresponding to one of the light bulbs upstairs. How can you figure out which ...Read More »Complete the name
A mother had five boys Monarch, Tushar, Webnar and Thomson. What would be the fifth boys name out of below ...Read More »When did the girls went for picnic puzzle
Sofia, Angelina & Emma went for a picnic. After a few days they forgot the date, day and month on ...Read More »Cloth Roll to be Cut
You have a roll of cloth 1km long. You have a machine which cuts this roll into pieces of 10-meter-long ...Read More »Seven Letter Word
I am a seven-letter word but if you remove four of my letters I will have one left. What word ...Read More »Who killed the little boy?
There was a family who lived in a circle house, one day they went on vacation but they left their ...Read More »How is this possible???
Two children are born in the same hospital, on the same year, on the same day, and have the same ...Read More »Prime Number Riddle
We know that the number 7 is the prime followed by a cube. Which next number is also a prime ...Read More »How do all three cross the river?
John, his wife and their daughter wish to cross a river. The row boat can only hold 100 kilos. John ...Read More »Why not?
A lawyer has a brother who is a Catholic priest. The lawyer wants to be a priest as well but ...Read More »A Man in Elevator
A man who lives on the tenth floor takes the elevator down to the first floor every morning and goes ...Read More »What am i?
What always murmurs but never talks, Always runs but never walks, Has a bed but never sleeps, Has a mouth ...Read More »