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

  • SEARCH
  • Home
  • 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 3283384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:02:08+00:00 2026-05-17T20:02:08+00:00

I went to an interview today and was asked this question: Suppose you have

  • 0

I went to an interview today and was asked this question:

Suppose you have one billion integers which are unsorted in a disk file. How would you determine the largest hundred numbers?

I’m not even sure where I would start on this question. What is the most efficient process to follow to give the correct result? Do I need to go through the disk file a hundred times grabbing the highest number not yet in my list, or is there a better way?

  • 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-05-17T20:02:09+00:00Added an answer on May 17, 2026 at 8:02 pm

    Here’s my initial algorithm:

    create array of size 100 [0..99].
    read first 100 numbers and put into array.
    sort array in ascending order.
    while more numbers in file:
        get next number N.
        if N > array[0]:
            if N > array[99]:
                shift array[1..99] to array[0..98].
                set array[99] to N.
            else
                find, using binary search, first index i where N <= array[i].
                shift array[1..i-1] to array[0..i-2].
                set array[i-1] to N.
            endif
        endif
    endwhile
    

    This has the (very slight) advantage is that there’s no O(n^2) shuffling for the first 100 elements, just an O(n log n) sort and that you very quickly identify and throw away those that are too small. It also uses a binary search (7 comparisons max) to find the correct insertion point rather than 50 (on average) for a simplistic linear search (not that I’m suggesting anyone else proffered such a solution, just that it may impress the interviewer).

    You may even get bonus points for suggesting the use of optimised shift operations like memcpy in C provided you can be sure the overlap isn’t a problem.


    One other possibility you may want to consider is to maintain three lists (of up to 100 integers each):

    read first hundred numbers into array 1 and sort them descending.
    while more numbers:
        read up to next hundred numbers into array 2 and sort them descending.
        merge-sort lists 1 and 2 into list 3 (only first (largest) 100 numbers).
        if more numbers:
            read up to next hundred numbers into array 2 and sort them descending.
            merge-sort lists 3 and 2 into list 1 (only first (largest) 100 numbers).
        else
            copy list 3 to list 1.
        endif
    endwhile
    

    I’m not sure, but that may end up being more efficient than the continual shuffling.

    The merge-sort is a simple selection along the lines of (for merge-sorting lists 1 and 2 into 3):

    list3.clear()
    while list3.size() < 100:
        while list1.peek() >= list2.peek():
            list3.add(list1.pop())
        endwhile
        while list2.peek() >= list1.peek():
            list3.add(list2.pop())
        endwhile
    endwhile
    

    Simply put, pulling the top 100 values out of the combined list by virtue of the fact they’re already sorted in descending order. I haven’t checked in detail whether that would be more efficient, I’m just offering it as a possibility.

    I suspect the interviewers would be impressed with the potential for “out of the box” thinking and the fact that you’d stated that it should be evaluated for performance.

    As with most interviews, technical skill is one of the the things they’re looking at.

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

Sidebar

Related Questions

This question was recently asked to me in an interview for which i went
I went to a job interview today and was given this interesting question. Besides
Today, I went for an interview and the interviewer asked me how I would
So today I went to an interview and one of the questions was the
Today I faced one question in interview. Is it possible to apply inheritance concept
I recently went for a Java Interview where the interviewer asked me a question
I was given this MySQL Interview Question, which made me disqualified for the job.
I was asked this question in an interview. I implemented an algorithm using sieve
I went to a PHP job interview, I was asked to implement a piece
I came across this File parsing programming challenge today and found it quite interesting.

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.