They have a trunk of hats, and they show the prisoners there are five hats in the trunk—three black hats and two white hats. The guards make the prisoners sit in chairs and line them up three in a row, such that the prisoner in the back of the line can see the two in front of him, the prisoner in the middle can see the one person in front of him, and the prisoner in the front can see nothing but the prison wall.
The guards blindfold the prisoners, and place a hat on each of their heads. They then remove the blindfolds and tell the inmates that they can go free if they correctly name the color of the hat they have on—but if they guess wrong, they will be shot dead. Needless to say, the prisoners cannot see what color hat they have on their own head.
The guards first ask the prisoner in the back of the line, "what color hat do you have on?" He says he doesn't know. They ask the man in the middle, and he also doesn't know. When the guards ask the prisoner in the front of the line what color hat he has on, he answers correctly, and goes free.
What color hat did he have on, and how did he know?