Interview Question: Find Median From Mega Number Of Integers

后端 未结 9 1170
暖寄归人
暖寄归人 2020-12-12 14:02

There is a file that contains 10G(1000000000) number of integers, please find the Median of these integers. you are given 2G memory to do this. Can anyone come up with an re

9条回答
  •  感情败类
    2020-12-12 14:42

    Make a pass through the file and find count of integers and minimum and maximum integer value.

    Take midpoint of min and max, and get count, min and max for values either side of the midpoint - by again reading through the file.

    partition count > count => median lies within that partition.

    Repeat for the partition, taking into account size of 'partitions to the left' (easy to maintain), and also watching for min = max.

    Am sure this'd work for an arbitrary number of partitions as well.

提交回复
热议问题