Skip to main content
All Questions

Find Largest Numbers

Medium

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]

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.