Facebook Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

load 1Gb file content onto the main memory...sort it using inplace heap sort...write the sorted data in a temporary file....do this for all 1000 chunks and then merge these files

- Anonymous May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't you have to re-sort the merged files?

- Sir May 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Merging sorted arrays results on a sorted array -- that's very basic (google it).

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's wrong. In order to run a classic merge algorithm you would need to load all the data in memory and you would have the same problem.
You certainly need to run a merge algorithm with the sorted files, but you need to have just a min-heap in memory that keeps only the minimum element of every file that havent been processed.

- carvajal February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's wrong. In order to run a classic merge algorithm you would need to load all the data in memory and you would have the same problem.
You certainly need to run a merge algorithm with the sorted files, but you need to have just a min-heap in memory that keeps only the minimum element of every file that havent been processed.

- carvajal February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

upps, sorry for comment twice, network issue...
that exercise is well explained in the book "Algorithms for Interviews"

- carvajal February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

No guys, Anonymous is right. He splits up the 1TB file into 1024 smaller files. Sorts each file separately in memory (each small file is 1GB). Then does a k-way (1024-way) merge of the sorted smaller files in memory (and writes out result to a new file).

For the merge part we don't need to have all 1TB available in memory. We just need the (current) smallest element from each chunk (smaller file); the smallest of these 1024 smallest elements gets written to the final result file. We can maintain the smallest element with a min-heap. Merging (i.e. the heap) requires only 1024*4 bytes (assuming one element is 4 bytes) which is just 4K.

So we'll have plenty of space for extra stuff to improve performance like 1024 buffers to read elements from the smaller files and an output buffer to write elements into the resulting file (after popping head of the heap 2^38 times) - so we don't have to read/write elements from/to disk one-by-one.

- Safi December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

external sorting

- Anonymous May 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two way external sort.

- Anonymous May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Two way? That would mean splitting up the file to 2 smaller files each with size of 512 GB. To perform the merge sort you have to sort the smaller files first; but they don't fit into the main memory... Of course you can recursively two-way external sort each of the smaller files; if that's what you meant. ;)

- Safi December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

D&C shud work...obviously merge sort

- Anonymous August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Geek joke: Assuming we are sorting Int32's, and having whole 1GB for allocation (program is running 'elsewhere'), allocate 1GB array (0-2^32-1) and use RadixSort. Linear complexity 8)

- Pz April 19, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More