Find k customers with least revenue
DatabricksSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to aggregate per-customer revenue from event streams and perform selection on aggregated totals, testing knowledge of data aggregation, selection algorithms, and efficient use of data structures.
Problem
You are given a list of purchase events. Each event contains:
customer_id(string or int)amount(integer, may be 0; assume non-negative unless stated otherwise)
Define a customer's revenue as the sum of amount across all events for that customer.
Task
Return the k customers with the smallest total revenue.
Requirements
- If fewer than
kcustomers exist, return all customers. - If multiple customers tie on total revenue, you may return them in any order (or specify a deterministic tie-breaker such as
customer_idascending).
Input/Output (suggested)
- Input:
events: List[(customer_id, amount)],k: int - Output:
List[customer_id](thekleast-revenue customers)
Constraints (assumptions)
n = len(events)can be large.- Number of unique customers
mcan be up ton.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.