Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Finding the M Largest Elements

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Finding the M Largest Elements. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

PingOne monitoring may need to identify the largest event-volume measurements from a batch of observations. Given an integer array and an integer m, return the m largest values in descending order.

Duplicate values count separately. For example, if the largest values include two occurrences of 9, both must appear in the result. You may assume the input is valid and contains at least m values.

Formal Specification

Implement top_m_largest(nums, m):

  • Input: nums, a non-empty list of integers, and m, an integer between 1 and len(nums).
  • Output: A list containing exactly the m largest values from nums, sorted in descending order.

Your solution should avoid sorting the entire array when m is much smaller than len(nums). Explain the data structure you choose and its time and space complexity.

Constraints

  • 1 <= m <= len(nums) <= 100000
  • -10^9 <= nums[i] <= 10^9
  • Duplicate values are allowed
  • The output must contain exactly m values in descending order

Function Signature

def top_m_largest(nums, m):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output