Top 5 Customers by Total Transaction Volume
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
A bank processes a stream of transactions and wants to report its top 5 customers by transaction volume. You are given the list of `transactions` processed so far, where each transaction is `[customer_id, amount]`. Process all transactions and return the top 5 customer IDs sorted by their total transaction volume in descending order.
If two customers have the same total volume, the customer with the smaller ID comes first. If there are fewer than 5 customers, return all of them in the same order.
### Function Signature
```python
def top_five_customers(transactions: list[list[int]]) -> list[int]:
...
```
### Rules
- A customer's total volume is the sum of the amounts of all their transactions.
- Order customers by total volume from highest to lowest; equal totals are ordered by customer ID ascending.
- Return at most 5 customer IDs, each at most once.
### Constraints
- `1 <= len(transactions) <= 10^5`
- `1 <= customer_id <= 10^6`
- `1 <= amount <= 10^6`
- Each transaction is `[customer_id, amount]`.
### Examples
```text
Input: transactions = [[1,500],[2,300],[3,700],[1,200],[4,100],[5,600],[2,400],[6,50]]
Output: [1,2,3,5,4]
```
Customer totals are 1 = 700, 2 = 700, 3 = 700, 4 = 100, 5 = 600 and 6 = 50. Customers 1, 2 and 3 tie at 700 and are ordered by ID, then come 5 (600) and 4 (100). Customer 6 is sixth and is left out.
```text
Input: transactions = [[10,1000],[20,2000],[30,3000]]
Output: [30,20,10]
```
Only 3 customers exist, so all of them are returned, sorted by volume descending: 30 (3000), 20 (2000), 10 (1000).
```text
Input: transactions = [[7,100],[3,100],[9,200],[1,300],[4,400],[8,500]]
Output: [8,4,1,9,3]
```
The first four places go to 8, 4, 1 and 9 by volume. Customers 3 and 7 tie at 100 for the fifth place, and the smaller ID, 3, takes it.
Overview: Given a list of banking transactions as customer ID and amount pairs, return the five customers with the highest total volume, breaking ties by smaller ID. It tests aggregation with a hash map, heap or sort ordering with a composite key, and handling fewer than five customers.
A bank processes a stream of transactions and wants to report its top 5 customers by transaction volume. You are given `transactions`, the list of transactions processed so far, where each transaction is `[customer_id, amount]`. Process all of them and return the IDs of the top 5 customers, sorted by total transaction volume in descending order.
Rules:
- A customer's total volume is the sum of the amounts of all of their transactions.
- Order customers by total volume from highest to lowest. If two customers have the same total volume, the customer with the smaller ID comes first.
- Return at most 5 customer IDs, each at most once.
- If there are fewer than 5 customers, return all of them in that same order.
A single customer's total can reach 10^5 * 10^6 = 10^11, which exceeds 2^31 - 1, so accumulate totals in a 64-bit integer (`long` in Java, `long long` in C++). Customer IDs and individual amounts fit in a 32-bit `int`.
Example 1:
Input: transactions = [[1,500],[2,300],[3,700],[1,200],[4,100],[5,600],[2,400],[6,50]]
Output: [1,2,3,5,4]
Explanation: The totals are 1 = 700, 2 = 700, 3 = 700, 4 = 100, 5 = 600 and 6 = 50. Customers 1, 2 and 3 tie at 700 and are ordered by ID, then come 5 (600) and 4 (100). Customer 6 is sixth and is left out.
Example 2:
Input: transactions = [[7,100],[3,100],[9,200],[1,300],[4,400],[8,500]]
Output: [8,4,1,9,3]
Explanation: The first four places go to 8, 4, 1 and 9 by volume. Customers 3 and 7 tie at 100 for fifth place, and the smaller ID, 3, takes it.
Constraints:
- 1 <= len(transactions) <= 10^5
- 1 <= customer_id <= 10^6
- 1 <= amount <= 10^6
- Each transaction is [customer_id, amount].
Constraints
- 1 <= len(transactions) <= 10^5
- 1 <= customer_id <= 10^6
- 1 <= amount <= 10^6
- Each transaction is [customer_id, amount].
- A customer's total volume can reach 10^11, which exceeds 2^31 - 1, so use long in Java and long long in C++ for totals.
Examples
Input: ([[5, 10]],)
Expected Output: [5]
Explanation: Minimum valid input: one transaction gives a one-element list.
Input: ([[1, 500], [2, 300], [3, 700], [1, 200], [4, 100], [5, 600], [2, 400], [6, 50]],)
Expected Output: [1, 2, 3, 5, 4]
Explanation: Source example (8 transactions): 1, 2 and 3 tie at 700 and go by ID, then 5 (600) and 4 (100); customer 6 (50) is sixth and excluded.
Hints
- A customer's place depends on the sum of all of their transactions, so many small transactions can outrank one large one.
- Equal totals are ordered by the smaller customer ID, no matter whose transactions appear first in the input.
- The answer holds at most five IDs and never repeats one; with fewer than five customers, every customer is returned.