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-
Brother age easy puzzle
A boy is 2 years old, his brother is half as old as him. When the first boy is 100, ...Read More »South West Direction
If we change the South-East direction into North and North-East into West and all others similarly. Can you find out ...Read More »Which switch belongs to which light bulb?
There are 4 light bulbs in a room and 4 switches corresponding to these four light bulbs (in a random ...Read More »It is what you love
Ii is what you seek, It is only what is right. It is never wrong. It could be short, It ...Read More »Kris Cross
Can you place six X (crosses) in below board without making three in a row in any way ?Read More »What is the value for ‘?’
Find the replacement value for “?” Hint 1 – Start and finish at ”?” (it’s about results); Hint 2 – ...Read More »Wheel Invention
What happened when the wheel was invented?Read More »Shopping 100 chocolates puzzle
You must buy 100 Chocolates for exactly $100, and purchase at least one Chocolates from each store. The first store ...Read More »What did the wise man say to them?
An Arab sheikh tells his two sons to race their camels to a distant city to see who will inherit ...Read More »Five digit number of which First and last digits are same riddle
I am thinking of a five digit number such that: First and last digits are same, their submission is an ...Read More »Can\’t walk with four legs
He has four legs and one back, yet he can not walk, Who is he ?Read More »Tattoo Artist\’s Shop
Outside a tattoo artist\’s shop, there was a signboard which read \’I make tattoos on only them who do not ...Read More »Ratio of Boys and Girls in a Country where people want only boys
In a country, All families wants a boy. They keep having babies till a boy is born. What is the ...Read More »Which door will you find the spirit?
You are an expert on paranormal activity and have been hired to locate a spirit haunting an old resort hotel. ...Read More »A hunter and a shepherd riddle
A hunter met two shepherds, one of whom had three loaves and the other, five loaves. All the loaves were ...Read More »you can see the letter E in the empty space.
Some people only see three angled lines, but if you look carefully you can actually see the letter E in ...Read More »How many marbles are there under each mug?
There are 4 mugs placed upturned on the table. Each mug have the same number of marbles and a statement about ...Read More »One colour but not one size riddle
He has one colour, but not one size, Stuck at the bottom, yet easily flies. Present in sun, but not ...Read More »The Stairs climb Up
You need to climb ten stairs. At every stair, you can either take one step up or you can jump ...Read More »Where is the white house?
There are three houses one is red one is blue band one is white. If the red house is to ...Read More »