Suppose there are 4 prisoners named W, X, Y, and Z. Prisoner W is standing on one side of a wall, and prisoners X Y and Z are standing on the other side of the wall. Prisoners X, Y, and Z are all standing in a straight line facing right – so X can see prisoner Y and Z, and Y can see prisoner Z. This is what their arrangement looks like:

W || X Y Z 

Where the “||” represents a wall. The wall has no mirrors. So, prisoner W can see the wall and nothing else.

There are 2 white hats and 2 black hats and each prisoner has a hat on his head. Each prisoner can not see the color of his own hat, and can not remove the hat from his own head. But the prisoners do know that there are 2 white hats and 2 black hats amongst themselves.

The prison guard says that if one of the prisoners can correctly guess the color of his hat then the prisoners will be set free and released. The puzzle for you is to figure out which prisoner would know the color of his own hat?

Note that the prisoners are not allowed to signal to each other, nor speak to each other to give each other hints. But, they can all hear each other if one of them tries to answer the question. Also, you can assume that every prisoner thinks logically and knows that the other prisoners think logically as well.

A good way to approach this problem is to start by reasoning out what each prisoner knows, and also account for each and every one of the assumptions stated above in the problem.

The answer to the prisoner hat riddle

Clearly prisoners W and Z can not immediately know anything since neither of those prisoners can see any of the other prisoners. So, let’s instead focus on prisoners X and Y.

As always, it helps to think in terms of actual scenarios in order to find a solution to this brain teaser because it makes things more concrete. Suppose we have the following scenario with the arrangement of different hat colors:

W      || X        Y       Z
Black || Black White White 

In the scenario above, prisoner X will clearly see that Y and Z both have white hats, and logically deduce that he must have a black hat since there are 2 white hats and 2 black hats all together – and he would be correct. Very simple! And this simple logic would also apply to this scenario as well:

W      || X        Y       Z
White  || White  Black Black 

But let’s consider another example:

W      || X        Y       Z
Black || White Black White 

In the example above, prisoner X will see that Y and Z have black and white hats. This means that prisoner X will reason that he can not conclusively say whether or not he has a white hat or a black hat – because he knows that that there are 2 black hats and 2 white hats and he sees 1 black and 1 white, so he himself could be wearing either a white or a black hat.

If 5 minutes pass and none of the prisoner say anything…

But, if prisoner X does not say anything for some time (it doesn’t really matter how long much time has passed), then prisoner Y will know that prisoner X does not know the color of his own hat. The silence of prisoner X means that prisoner Y will know that prisoner X must be seeing both a white hat and a black hat – and one of those hats is the one that prisoner X is wearing. And if prisoner Y can see that prisoner Z is wearing a white hat, then prisoner Y knows that he himself must be wearing a black hat. So, prisoner Y will speak out and all of the prisoners will be released again!

Clarifying assumptions for the black and white hat puzzle

Some sloppy interviewers may not provide you with all of the assumptions that this problem needs – for instance, not knowing which hats the prisoners can see would definitely prevent you from solving this problem. So, it’s always good to clarify what assumptions there are with problems like these, especially if you find yourself stuck and unable to provide the answer to the riddle.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

One thought on “Prisoner Hat Riddle”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.