Cracking the Code Interview
1 Answer
Cracking the Code Interview
| Flag | PURGE
Brain teaser 6.4!
The solution to this on page 146 is incorrect. They state that if nobody else has a hat, then you have a hat. True. If another person has a hat, there will never be a way for you to know if you have a hat or not. C could equal 1 as the problem did not specify what would happen once all hats were removed. There is no signal to let the guys know that there are no more hats, thus no way for any person to know whether they can go into the water or not.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
The solution is correct.
- Gayle L McDowell June 03, 2013Okay, let's say C = 1. What will happen in this case? The person with the hat will see that no one else has a hat and remove it that night.
So, if C = 1, the hats are all gone after the first night. Do you agree with that? Are you sure? Okay. Great.
Suppose C = 2. Both hat-wearing people look around and see one other person with a hat. They know that C = 1 or C = 2, but they don't know which of these it is. But they *do* know that, if C = 1, then that other person would have removed their hat the first night [from the above paragraph]. So, after the first night, if both people see that the other person is still wearing a hat, then they must conclude that C != 1. Therefore, C = 2 and they have the other hat. That night, they both remove their hats.
So, if C = 2, the hats are all gone after the second night. Note that *both* hats are removed simultaneously.
Suppose C = 3. Again, the hat-wearers know that either C = 2 or C = 3. They also know that, if C = 2, then the hats will be removed on the second night. When the hats are not removed, they conclude that C = 3. They all remove their hats on the third night.
... and so on.