Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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

Your question is TopK With Min-Heap. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.