Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Finding the M Largest Elements
00:00
5 left

Finding the M Largest Elements

MediumPython

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):
Interviewer

Your question is Finding the M Largest Elements. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.