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 20 | Hard<br />

20.4 Write a method to count <strong>the</strong> number of 2s between 0 <strong>and</strong> n.<br />

pg 91<br />

SOLUTION<br />

Picture a sequence of numbers:<br />

0 1 2 3 4 5 6 7 8 9<br />

10 11 12 13 14 15 16 17 18 19<br />

20 21 22 23 24 25 26 27 28 29<br />

...<br />

110 111 112 113 114 115 116 117 118 119<br />

The last digit will be repeated every 10 numbers, <strong>the</strong> last two digits will be repeated every<br />

10^2 numbers, <strong>the</strong> last 3 digits will be repeated every 10^3 numbers, etc.<br />

So, if <strong>the</strong>re are X 2s between 0 <strong>and</strong> 99, <strong>the</strong>n we know <strong>the</strong>re are 2x twos between 0 <strong>and</strong> 199.<br />

Between 0 <strong>and</strong> 299, we have 3x twos from <strong>the</strong> last two digits, <strong>and</strong> ano<strong>the</strong>r 100 2s from <strong>the</strong><br />

first digit.<br />

In o<strong>the</strong>r words, we can look at a number like this:<br />

f(513) = 5 * f(99) + f(13) + 100<br />

To break this down individually:<br />

»»<br />

The sequence of <strong>the</strong> last two digits are repeated 5 times, so add 5 * f(99)<br />

»»<br />

We need to account for <strong>the</strong> last two digits in 500 -> 513, so add f(13)<br />

»»<br />

We need to account for <strong>the</strong> first digit being two between 200 -> 299, so add 100<br />

Of course, if n is, say, 279, we’ll need to account for this slightly differently:<br />

f(279) = 2 * f(99) + f(79) + 79 + 1<br />

To break this down individually:<br />

»»<br />

The sequence of <strong>the</strong> last two digits are repeated 2 times, so add 2 * f(99)<br />

»»<br />

We need to account for <strong>the</strong> last two digits in 200 -> 279, so add f(79)<br />

»»<br />

We need to account for <strong>the</strong> first digit being two between 200 -> 279, so add 79 + 1<br />

Recu<br />

rsive Code:<br />

1 public static int count2sR(int n) {<br />

2 // Base case<br />

3 if (n == 0) return 0;<br />

4<br />

5 // 513 into 5 * 100 + 13. [Power = 100; First = 5; Remainder = 13]<br />

6 int power = 1;<br />

7 while (10 * power < n) power *= 10;<br />

8 int first = n / power;<br />

9 int remainder = n % power;<br />

2 8 3<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Additional Review Problems

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

Saved successfully!

Ooh no, something went wrong!