25.03.2013 Views

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Solutions to Chapter 9 | Sorting and 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 and why?<br />

SOLUTION<br />

CareerCup com<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 and<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 and 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 and we<br />

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

1 8 2

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

Saved successfully!

Ooh no, something went wrong!