Track Customer Referral Revenue
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of dynamic data structure design and algorithms for maintaining aggregated metrics over a referral tree, specifically computing subtree revenue contributions and selecting customers by total contributed revenue under a threshold.
Constraints
- 1 <= len(operations) <= 3000
- 0 <= revenue, minTotalRevenue <= 10^9
- 0 <= referrerID < current number of customers whenever a referred insert appears
- 1 <= k <= 3000
Examples
Input: [('insert', 10), ('insert', 4, 0), ('insert', 3, 0), ('get', 1, 3), ('get', 2, 3)]
Expected Output: [0, 1, 2, [2], [1, 2]]
Explanation: After the inserts, total contributed revenues are: customer 0 -> 17, customer 1 -> 4, customer 2 -> 3. For ('get', 1, 3), the smallest qualifying total is customer 2, so return [2]. For ('get', 2, 3), the two smallest qualifying customers are 2 and 1, and the final returned list is sorted by ID as [1, 2].
Input: [('insert', 7), ('get', 3, 10)]
Expected Output: [0, []]
Explanation: Customer 0 has total contributed revenue 7, which is below 10, so the query returns an empty list.
Input: [('get', 2, 0)]
Expected Output: [[]]
Explanation: There are no customers yet, so no one qualifies.
Input: [('insert', 2), ('insert', 5), ('insert', 3, 0), ('insert', 5), ('get', 2, 5), ('get', 10, 5)]
Expected Output: [0, 1, 2, 3, [0, 1], [0, 1, 3]]
Explanation: Totals become: customer 0 -> 5, customer 1 -> 5, customer 2 -> 3, customer 3 -> 5. For threshold 5, customers 0, 1, and 3 qualify. Since their totals are tied, smaller IDs come first, so the first two are 0 and 1.
Input: [('insert', 4), ('insert', 2, 0), ('insert', 1, 1), ('get', 2, 2)]
Expected Output: [0, 1, 2, [0, 1]]
Explanation: Totals are: customer 0 -> 7, customer 1 -> 3, customer 2 -> 1. With threshold 2, customers 1 and 0 qualify. They are chosen in total order as 1 then 0, but the returned ID list is sorted ascending, so the answer is [0, 1].
Hints
- A newly inserted customer is always a leaf. Their direct revenue increases the total contributed revenue of every ancestor on the path to the root.
- For each get query, filter customers whose total is at least the threshold, sort them by (total contributed revenue, customer ID), take the first k, then sort those chosen IDs for output.