Skip to main content

The Prisoners Black and White Hat Riddle


Problem :

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

Z | A B C
Where the “|” represents a wall. The wall has no mirrors. So, prisoner Z 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 among 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?And also discuss the various scenarios in which tell which prisoner will tell first the hat color he is wearing.

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

Solution :

Lets take the first Scenario

Z       |   A          B        C

White    White  Black  Black

In this case A can see both B and C's Hat and when he sees that both of them wearing Black hat he will know that he is wearing White Hat for sure,knowing that there are 2 Black and 2 White Hats.He than call the guard and tell him his Hat color and all the Prisoner will get released.

Now looking into second Scenario

Z       |  A          B           C

Black    Black   White    White

In this case again A will know that he must be wearing Black Hat as both  B and C are wearing White Hat.Again it is very Simple!!.

Now the problem will come for A when he will see two different color Hat next to him.Lets discuss third Scenario.

Z      |  A           B           C

Black  White   Black    White

Now A will see 1 Black Hat and 1 White hat next to him.So he will not be sure this time that what color hat he is wearing it may be White or may be Black.So he will not call guard.But do you think that prisoners will now never get released??..Actually its not true.What about B ? He can also call the guard isn't it?
So after waiting for sometime B will realize that A has not called the guard which means he is not sure of the color of his hat.Meaning he is seeing two different color Hat next to him.So B will check C's Hat color and see White color Hat.Now B is sure of wearing Black color hat and will call the guard.

In this riddle C and Z cannot call the guard as Z cannot see through wall and there is nobody on the right of C and he cannot see the color of Hat of B and A.So both Z and C can wait for A and B to guess the color of their hat correctly and all of them will get released.

Comments

  1. it is not clear that the 3rd case how A and Z release........

    ReplyDelete
  2. Actually in puzzle if any of the prisoner give correct answers...then all will be released and not only the prisoner who gave correct answer...so if B will give correct answer A and Z will also get released..

    ReplyDelete

Post a Comment

Popular posts from this blog

Tricky Questions or Puzzles in C ( Updated for 2026)

Updated for 2026 This article was originally written when C/C++ puzzles were commonly asked in interviews. While such language-specific puzzles are less frequent today, the problem-solving and logical reasoning skills tested here remain highly relevant for modern Software Engineering, Data Engineering, SQL, and system design interviews . Why These Puzzles Still Matter in 2026 Although most Software &   Data Engineering interviews today focus on Programming, SQL, data pipelines, cloud platforms, and system design , interviewers still care deeply about how you think . These puzzles test: Logical reasoning Edge-case handling Understanding of execution flow Ability to reason under pressure The language may change , but the thinking patterns do not . How These Skills Apply to Data Engineering Interviews The same skills tested by C/C++ puzzles appear in modern interviews as: SQL edge cases and NULL handling Data pipeline failure scenarios Incremental vs ...

Programs and Puzzles in technical interviews i faced

I have attended interview of nearly 10 companies in my campus placements and sharing their experiences with you,though i did not got selected in any of the companies but i had great experience facing their interviews and it might help you as well in preparation of interviews.Here are some of the puzzles and programs asked to me in interview in some of the good companies. 1) SAP Labs I attended sap lab online test in my college through campus placements.It had 3 sections,the first one is usual aptitude questions which i would say were little tricky to solve.The second section was Programming test in which you were provided snippet of code and you have to complete the code (See Tricky Code Snippets  ).The code are from different data structures like Binary Tree, AVL Tree etc.Then the third section had questions from Database,OS and Networks.After 2-3 hours we got the result and i was shortlisted for the nest round of interviews scheduled next day.Then the next day we had PPT of t...

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions