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

20.3 Write a method to r<strong>and</strong>omly generate a set of m integers from an array of size n. Each<br />

element must have equal probability of being chosen.<br />

SOLUTION<br />

pg 91<br />

Our first instinct on this problem might be to r<strong>and</strong>omly pick elements from <strong>the</strong> array <strong>and</strong> put<br />

<strong>the</strong>m into our new subset array. But <strong>the</strong>n, what if we pick <strong>the</strong> same element twice? Ideally,<br />

we’d want to somehow “shrink” <strong>the</strong> array to no longer contain that element. Shrinking is<br />

expensive though because of all <strong>the</strong> shifting required.<br />

Instead of shrinking / shifting, we can swap <strong>the</strong> element with an element at <strong>the</strong> beginning<br />

of <strong>the</strong> array <strong>and</strong> <strong>the</strong>n “remember” that <strong>the</strong> array now only includes elements j <strong>and</strong> greater.<br />

That is, when we pick subset[0] to be array[k], we replace array[k] with <strong>the</strong> first element in<br />

<strong>the</strong> array. When we pick subset[1], we consider array[0] to be “dead” <strong>and</strong> we pick a r<strong>and</strong>om<br />

element y between 1 <strong>and</strong> array.size(). We <strong>the</strong>n set subset[1] equal to array[y], <strong>and</strong> set array[y]<br />

equal to array[1]. Elements 0 <strong>and</strong> 1 are now “dead.” Subset[2] is now chosen from array[2]<br />

through array[array.size()], <strong>and</strong> so on.<br />

1 /* R<strong>and</strong>om number between lower <strong>and</strong> higher, inclusive */<br />

2 public static int r<strong>and</strong>(int lower, int higher) {<br />

3 return lower + (int)(Math.r<strong>and</strong>om() * (higher - lower + 1));<br />

4 }<br />

5<br />

6 /* pick M elements from original array. Clone original array so that<br />

7 * we don’t destroy <strong>the</strong> input. */<br />

8 public static int[] pickMR<strong>and</strong>omly(int[] original, int m) {<br />

9 int[] subset = new int[m];<br />

10 int[] array = original.clone();<br />

11 for (int j = 0; j < m; j++) {<br />

12 int index = r<strong>and</strong>(j, array.length - 1);<br />

13 subset[j] = array[index];<br />

14 array[index] = array[j]; // array[j] is now “dead”<br />

15 }<br />

16 return subset;<br />

17 }<br />

CareerCup.com<br />

2 8 2

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

Saved successfully!

Ooh no, something went wrong!