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 11 | System Design <strong>and</strong> Memory Limits<br />

9 while (in.hasNextInt()) {<br />

10 int n = in.nextInt();<br />

11 blocks[n / (bitfield.length * 8)]++;<br />

12 }<br />

13<br />

14 for (int i = 0; i < blocks.length; i++) {<br />

15 if (blocks[i] < bitfield.length * 8){<br />

16 /* if value < 2^20, <strong>the</strong>n at least 1 number is missing in<br />

17 * that section. */<br />

18 starting = i * bitfield.length * 8;<br />

19 break;<br />

20 }<br />

21 }<br />

22<br />

23 in = new Scanner(new FileReader(“input_file_q11_4.txt”));<br />

24 while (in.hasNextInt()) {<br />

25 int n = in.nextInt();<br />

26 /* If <strong>the</strong> number is inside <strong>the</strong> block that’s missing numbers,<br />

27 * we record it */<br />

28 if( n >= starting && n < starting + bitfield.length * 8){<br />

29 bitfield [(n-starting) / 8] |= 1

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

Saved successfully!

Ooh no, something went wrong!