Find k customers with least revenue
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: 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.
Constraints
- 0 <= len(events) <= 2 * 10^5
- 0 <= amount <= 10^9
- 0 <= k <= 2 * 10^5
- Within one test case, all `customer_id` values are comparable because they are all ints or all strings
Examples
Input: ([('A', 30), ('B', 10), ('A', 20), ('C', 5)], 2)
Expected Output: ['C', 'B']
Explanation: Total revenues are A=50, B=10, C=5. The two smallest are C and B.
Input: ([('alice', 0), ('bob', 0), ('alice', 3), ('bob', 3), ('cara', 2)], 2)
Expected Output: ['cara', 'alice']
Explanation: Total revenues are alice=3, bob=3, cara=2. Cara is smallest, and between alice and bob the tie is broken by customer_id ascending.
Input: ([(1, 4), (2, 1), (1, 1), (3, 0)], 5)
Expected Output: [3, 2, 1]
Explanation: Total revenues are 1=5, 2=1, 3=0. There are only 3 unique customers, so return all of them in sorted order by revenue.
Input: ([], 3)
Expected Output: []
Explanation: There are no events, so there are no customers to return.
Input: ([('solo', 7)], 0)
Expected Output: []
Explanation: When k is 0, the result must be an empty list.
Solution
def solution(events, k):
if k <= 0 or not events:
return []
revenue = {}
for customer_id, amount in events:
revenue[customer_id] = revenue.get(customer_id, 0) + amount
ordered = sorted(revenue.items(), key=lambda item: (item[1], item[0]))
return [customer_id for customer_id, _ in ordered[:k]]Time complexity: O(n + m log m), where n is the number of events and m is the number of unique customers. Space complexity: O(m).
Hints
- Use a hash map/dictionary to accumulate total revenue for each customer.
- After aggregation, sort the unique customers by `(revenue, customer_id)` and take the first `k`.