Skip to main content

Find Largest Numbers

MediumPremium

Let's say we have a long list of unsorted numbers (potentially millions), and we want to find the M largest numbers contained in it. Implement a function find_largest(input, m) that will find and return the largest m values given an input array or file.

If the input array is empty, return None (Python) or null.

Examples

Python
find_largest([1,5,4,2,3], 3) # => [3, 4, 5]

Example reading from a local file:

Python
# Create a new file with line-separated numbers with open('numbers.txt', 'w') as file: for n in range(0, 1000): print(str(n), file=file) # Find the largest numbers in the file with open('numbers.txt') as file: find_largest(file, 2) # => [998, 999]

What data structures would be useful for keeping track of minimum and maximum values?

If our input is very big, will we be able to store the entire list in memory?

One option is to use a min-heap and update it as you iterate through the input.

Is your solution space-efficient? What happens if input is too long to fit in memory?