Top 5 Customers by Total Transaction Volume

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.

Salesforce logo
Salesforce
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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

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.