27.10.2014 Views

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

SHOW MORE
SHOW LESS

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

»»<br />

Condition one (P(1) is true) ignites this chain, with truth cascading off into infinity.<br />

Base Case: c = 1 (See previous page).<br />

Assume true for c hats. i.e., if <strong>the</strong>re are c hats, it will take c nights to remove all of <strong>the</strong>m.<br />

Prove true for c+1 hats.<br />

Each man with a hat sees c hat, <strong>and</strong> can not be immediately sure whe<strong>the</strong>r <strong>the</strong>re are c hats or<br />

c+1 hats. However, he knows that if <strong>the</strong>re are c hats, it will take exactly c nights to remove<br />

<strong>the</strong>m. Therefore, when c nights have passed <strong>and</strong> everyone still has a hat, he can only conclude<br />

that <strong>the</strong>re are c+1 hats. He must know that he is wearing a hat. Each man makes <strong>the</strong><br />

same conclusion <strong>and</strong> simultaneously removes <strong>the</strong> hats on night c+1.<br />

Therefore, we have met <strong>the</strong> principles of induction. We have proven that it will take c nights<br />

to remove c hats.<br />

1 4 7<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Concepts <strong>and</strong> Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!