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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 10 | Ma<strong>the</strong>matical<br />

10.1 You have a basketball hoop <strong>and</strong> someone says that you can play 1 of 2 games.<br />

Game #1: You get one shot to make <strong>the</strong> hoop.<br />

Game #2: You get three shots <strong>and</strong> you have to make 2 of 3 shots.<br />

If p is <strong>the</strong> probability of making a particular shot, for which values of p should you pick<br />

one game or <strong>the</strong> o<strong>the</strong>r?<br />

SOLUTION<br />

Probability of winning Game 1: p<br />

Probability of winning Game 2:<br />

pg 68<br />

Let s(k,n) be <strong>the</strong> probability of making exactly k shots out of n. The probability of winning<br />

game 2 is s(2, 3)+s(3, 3). Since, s(k, n) = C(n, k) ( 1- p)^(n - k) p^k, <strong>the</strong> probability of<br />

winning is 3 * (1 - p) * p^2 + p^3.<br />

Simplified, it becomes 3 * p^2 - 2 * p^3.<br />

You should play Game1 if P(Game1) > P(Game2):<br />

p > 3*p^2 - 2*p^3.<br />

1 > 3*p - 2*p^2<br />

2*p^2 - 3*p + 1 > 0<br />

(2p - 1)(p - 1) > 0<br />

Both terms must be positive or both must be negative. But we know p < 1, so (p - 1) < 0. This<br />

means both terms must be negative.<br />

(2p - 1) < 0<br />

2p < 1<br />

p < .5<br />

So, we should play Game1 if p < .5.<br />

1 8 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!