Katkat

Find k customers with least revenue

DatabricksSoftware Engineer · Technical Screen

Last 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 k customers 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_id ascending).

Input/Output (suggested)

  • Input: events: List[(customer_id, amount)], k: int
  • Output: List[customer_id] (the k least-revenue customers)

Constraints (assumptions)

  • n = len(events) can be large.
  • Number of unique customers m can be up to n.