## 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
```

## 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.

Subscribe to our newsletter for more free interview questions.