Write a program to find 100 largest numbers out of an array of 1 billion numbers

前端 未结 30 2144
深忆病人
深忆病人 2020-11-29 14:04

I recently attended an interview where I was asked \"write a program to find 100 largest numbers out of an array of 1 billion numbers.\"

I was only able to give a br

30条回答
  •  温柔的废话
    2020-11-29 15:03

    Possible improvements.

    If the file contains 1 billions number, reading it could be really long...

    To improve this working you can :

    • Split the file into n parts, Create n threads, make n threads look each for the 100 biggest numbers in their part of the file (using the priority queue), and finally get the 100 biggest numbers of all threads output.
    • Use a cluster to do a such task, with a solution like hadoop. Here you can split the file even more and have the output quicker for a 1 billion (or a 10^12) numbers file.

提交回复
热议问题