Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Customers by Event Spend

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

Your question is Top Customers by Event Spend. 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

Given a list of event records, write a function that returns summary insights per customer. Each record is a list in the form [customer_id, event_type, amount], where customer_id and event_type are strings and amount is a non-negative integer. Aggregate total amount per customer, count the number of events per customer, and return the top k customers sorted by descending total amount, then ascending customer_id for ties.

Constraints

  • 1 <= len(records) <= 10^5
  • Each record has exactly 3 fields: [customer_id, event_type, amount]
  • 1 <= len(customer_id), len(event_type) <= 20
  • 0 <= amount <= 10^6
  • 1 <= k <= number of distinct customer_id values

Function Signature

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