Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Solutions</strong> to Chapter 6 | Brain Teasers<br />
6.4 A bunch of men are on an isl<strong>and</strong>. A genie comes down <strong>and</strong> ga<strong>the</strong>rs everyone toge<strong>the</strong>r<br />
<strong>and</strong> places a magical hat on some people’s heads (i.e., at least one person has<br />
a hat). The hat is magical: it can be seen by o<strong>the</strong>r people, but not by <strong>the</strong> wearer of<br />
<strong>the</strong> hat himself. To remove <strong>the</strong> hat, those (<strong>and</strong> only those who have a hat) must dunk<br />
<strong>the</strong>mselves underwater at exactly midnight. If <strong>the</strong>re are n people <strong>and</strong> c hats, how<br />
long does it take <strong>the</strong> men to remove <strong>the</strong> hats? The men cannot tell each o<strong>the</strong>r (in any<br />
way) that <strong>the</strong>y have a hat.<br />
FOLLOW UP<br />
SOLUTION<br />
Prove that your solution is correct.<br />
This problem seems hard, so let’s simplify it by looking at specific cases.<br />
Case c = 1: Exactly one man is wearing a hat.<br />
pg 60<br />
Assuming all <strong>the</strong> men are intelligent, <strong>the</strong> man with <strong>the</strong> hat should look around <strong>and</strong> realize<br />
that no one else is wearing a hat. Since <strong>the</strong> genie said that at least one person is wearing<br />
a hat, he must conclude that he is wearing a hat. Therefore, he would be able to remove it<br />
that night.<br />
Case c = 2: Exactly two men are wearing hats.<br />
The two men with hats see one hat, <strong>and</strong> are unsure whe<strong>the</strong>r c = 1 or c = 2. They know, from<br />
<strong>the</strong> previous case, that if c = 1, <strong>the</strong> hats would be removed on Night #1. Therefore, if <strong>the</strong> o<strong>the</strong>r<br />
man still has a hat, he must deduce that c = 2, which means that he has a hat. Both men<br />
would <strong>the</strong>n remove <strong>the</strong> hats on Night #2<br />
Case General: If c = 3, <strong>the</strong>n each man is unsure whe<strong>the</strong>r c = 2 or 3. If it were 2, <strong>the</strong> hats would<br />
be removed on Night #2. If <strong>the</strong>y are not, <strong>the</strong>y must deduce that c = 3, <strong>and</strong> <strong>the</strong>refore <strong>the</strong>y<br />
have a hat. We can follow this logic for c = 4, 5, …<br />
Proof by Induction<br />
Using induction to prove a statement P(n)<br />
If (1) P(1) = TRUE (e.g., <strong>the</strong> statement is true when n = 1)<br />
AND (2) if P(n) = TRUE -> P(n+1) = TRUE (e.g., P(n+1) is true whenever P(2) is true).<br />
THEN P(n) = TRUE for all n >= 1.<br />
Explanation<br />
» » Condition 2 sets up an infinite deduction chain: P(1) implies P(2) implies P(3) implies ...<br />
P(n) implies P(n+1) implies ...<br />
CareerCup.com<br />
1 4 6