Quick 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.

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

  1. A customer's place depends on the sum of all of their transactions, so many small transactions can outrank one large one.
  2. Equal totals are ordered by the smaller customer ID, no matter whose transactions appear first in the input.
  3. The answer holds at most five IDs and never repeats one; with fewer than five customers, every customer is returned.

Top Five Customers by Total Transaction Volume

easySalesforceSoftware Engineer

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].

Examples

Example 1
Input
([[5, 10]],)
Output
[5]
Notes
Minimum valid input: one transaction gives a one-element list.
Example 2
Input
([[1, 500], [2, 300], [3, 700], [1, 200], [4, 100], [5, 600], [2, 400], [6, 50]],)
Output
[1, 2, 3, 5, 4]
Notes
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.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def top_five_customers(transactions: list[list[int]]) -> list[int]:
Sample call
top_five_customers([[5, 10]])
Language
Case 1
Input
([[5, 10]],)
Expected
[5]
Case 2
Input
([[1, 500], [2, 300], [3, 700], [1, 200], [4, 100], [5, 600], [2, 400], [6, 50]],)
Expected
[1, 2, 3, 5, 4]

Browse More Questions

Show the approach

Approach

Aggregate first, then rank. One pass adds every amount to its customer's running total, so each customer ends up with exactly one (customer_id, total) entry and can never appear twice in the answer. The Python and C++ references use a hash map. The JavaScript reference sorts transaction indices by customer ID and sums each run of equal IDs. The Java reference uses a hash map. Next, rank the entries by the key (-total, customer_id): higher totals come first, and equal totals fall back to the smaller ID, which is the rule the statement gives. The answer is the IDs of the first min(5, number of customers) entries. After aggregation every ID is distinct, so this key is a strict total order and the output is unique. It does not depend on input order or on hash iteration order. Python and C++ sort all entries. Java and JavaScript keep a buffer of at most five entries in rank order, insert each customer at its rank, and drop whatever falls to sixth place, which gives the same top five. Edge cases: a single transaction gives a one-element list. If every transaction belongs to one customer, the answer is that one ID. With fewer than five customers, all of them are returned. A tie that straddles fifth place goes to the smaller ID. Totals can reach 10^11, so Java and C++ accumulate in 64-bit integers, and JavaScript numbers hold integers exactly up to 2^53.

Time complexity:
O(n log n)
Space complexity:
O(n)