Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:16:57+00:00 2026-06-04T08:16:57+00:00

This is another question from cracking coding interview, I still have some doubt after

  • 0

This is another question from cracking coding interview, I still have some doubt after reading it.

9.4 If you have a 2 GB file with one string per line, which sorting algorithm 
    would you use to sort the file and why?

SOLUTION

When an interviewer gives a size limit of 2GB, it should tell you something – in this case, it suggests that they don’t want you to bring all the data into memory.
So what do we do? We only bring part of the data into memory..
Algorithm:

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

  1. Divide the file into K chunks, where X * K = 2 GB. Bring each chunk into memory and sort the lines as usual using any O(n log n) algorithm. Save the lines back to the file.

  2. Now bring the next chunk into memory and sort.

  3. Once we’re done, merge them one by one.

The above algorithm is also known as external sort. Step 3 is known as N-way merge
The rationale behind using external sort is the size of data. Since the data is too huge and we can’t bring it all into memory, we need to go for a disk based sorting algorithm.

Doubt:

When in step 3, doing the merge sort, while comparing 2 arrays, do we need 2*X space each time we compare? And the limit was X MB. Should we make the chunks into (X/2)*2K = 2GB? So that each chunk will be X/2 MB and there will be 2K chunks. Or I am just understanding the merge sort wrong?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:16:59+00:00Added an answer on June 4, 2026 at 8:16 am

    First off, step 3 itself is not a merge sort, the whole thing is a merge sort. Step 3 is just a merge, with no sorting involved at all.

    And as to the storage required, there are two possibilities.

    The first is to merge the sorted data in groups of two. Say you have three groups:

    A: 1 3 5 7 9
    B: 0 2 4 6 8
    C: 2 3 5 7
    

    With that method, you would merge A and B in to a single group Y then merge Y and C into the final result Z:

    Y: 0 1 2 3 4 5 6 7 8 9         (from merging A and B).
    Z: 0 1 2 2 3 3 4 5 5 6 7 7 8 9 (from merging Y and C).
    

    This has the advantage of a very small constant memory requirement in that you only ever need to store the “next” element from each of two lists but, of course, you need to do multiple merge operations.

    The second way is a “proper” N-way merge where you select the next element from any of the groups. With that you would check the lowest value in every list to see which one comes next:

    Z: 0 1 2 2 3 3 4 5 5 6 7 7 8 9 (from merging A, B and C).
    

    This involves only one merge operation but it requires more storage, basically one element per list.

    Which of these you choose depends on the available memory and the element size.

    For example, if you have 100M memory available to you and the element size is 100K, you can use the latter. That’s because, for a 2G file, you need 20 groups (of 100M each) for the sort phase which means a proper N-way merge will need 100K by 20, or about 2M, well under your memory availability.

    Alternatively, let’s say you only have 1M available. That will be about 2000 (2G / 1M) groups and multiplying that by 100K gives 200M, well beyond your capacity.

    So you would have to do that merge in multiple passes. Keep in mind though that it doesn’t have to be multiple passes merging two lists.

    You could find a middle ground where for example each pass merges ten lists. Ten groups of 100K is only a meg so will fit into your memory constraint and that will result in fewer merge passes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was the question asked in interview. Can we call one constructor from another
I have came from another question to this one: How can one start with
this kind of follows on from another question of mine. Basically, once I have
This is a follow on from another question i made I have this query
This question is an extension from another question of mine @ Need some advice
I used some modified code found from another question on this website. I am
This is another question about well disposing objects from .NET. After having read a
This is borrowed from another question I made on the site, but would have
Another jquery calculation question. I've this, which is sample code from the plugin site
This question is related to another question I asked Basically, I have 2 horizontally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.