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 9 | Sorting <strong>and</strong> Searching<br />

9.4 If you have a 2 GB file with one string per line, which sorting algorithm would you use<br />

to sort <strong>the</strong> file <strong>and</strong> why?<br />

SOLUTION<br />

pg 66<br />

When an interviewer gives a size limit of 2GB, it should tell you something - in this case, it<br />

suggests that <strong>the</strong>y don’t want you to bring all <strong>the</strong> data into memory.<br />

So what do we do? We only bring part of <strong>the</strong> data into memory..<br />

Algorithm:<br />

How much memory do we have available? Let’s assume we have X MB of memory available.<br />

1. Divide <strong>the</strong> file into K chunks, where X * K = 2 GB. Bring each chunk into memory <strong>and</strong><br />

sort <strong>the</strong> lines as usual using any O(n log n) algorithm. Save <strong>the</strong> lines back to <strong>the</strong> file.<br />

2. Now bring <strong>the</strong> next chunk into memory <strong>and</strong> sort.<br />

3. Once we’re done, merge <strong>the</strong>m one by one.<br />

The above algorithm is also known as external sort. Step 3 is known as N-way merge<br />

The rationale behind using external sort is <strong>the</strong> size of data. Since <strong>the</strong> data is too huge <strong>and</strong> we<br />

can’t bring it all into memory, we need to go for a disk based sorting algorithm.<br />

CareerCup.com<br />

1 8 2

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

Saved successfully!

Ooh no, something went wrong!