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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
TopK With Min-Heap
00:00
5 left

TopK With Min-Heap

MediumPython

Problem

Implement a topK function for spenders using a min-heap.

Use the signature def topK(spenders, k):. spenders is a list of [name, amount] pairs with unique names. Return the names of the top k spenders, ordered by amount descending; break equal amounts by name descending. Return an empty list when k is zero. The solution should maintain at most k entries in the heap.

Constraints

  • 1 <= len(spenders) <= 1000
  • 0 <= k <= len(spenders)
  • Each spender name is unique
  • 0 <= amount <= 10^9
  • Names are non-empty strings

Function Signature

def topK(spenders, k):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

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.