Top K Merchant Accounts by Transaction Volume
Razorpay processes millions of transactions daily. Identifying top-performing merchant accounts is crucial for business intelligence, fraud detection, and targeted support. This problem challenges you to efficiently find the K merchant accounts with the highest total transaction volume from a given dataset.
k.merchantId (string) and an amount (number).merchantIds, representing the top k merchant accounts sorted by their total transaction volume in descending order. If two merchants have the same total volume, their relative order does not matter.transactions: An array of Transaction objects. Each Transaction object has two properties:
merchantId: A string representing the unique identifier of the merchant.amount: A number representing the transaction amount.k: An integer representing the number of top merchant accounts to retrieve.merchantId of a top k merchant. The list should be sorted in descending order of their total transaction volume.k is greater than the number of unique merchants? Return all unique merchants, sorted by volume.transactions is empty? Return an empty array.amount is always a positive number.merchantId be empty or null? Assume merchantId is always a non-empty string.Examples
Example 1
Input:
transactions = [{"merchantId": "M1", "amount": 100}, {"merchantId": "M2", "amount": 200}, {"merchantId": "M1", "amount": 50}, {"merchantId": "M3", "amount": 300}, {"merchantId": "M2", "amount": 100}], k = 2
Output:
["M3", "M2"]
Note:
M1 total: 100 + 50 = 150 M2 total: 200 + 100 = 300 M3 total: 300 Sorted by volume: M3 (300), M2 (300), M1 (150). Top 2 are M3, M2.
Example 2
Input:
transactions = [{"merchantId": "A", "amount": 10}, {"merchantId": "B", "amount": 20}, {"merchantId": "C", "amount": 30}], k = 5
Output:
["C", "B", "A"]
Note:
Unique merchants: A (10), B (20), C (30). Since k=5 is greater than the number of unique merchants (3), all merchants are returned, sorted by volume.
Example 3
Input:
transactions = [], k = 3
Output:
[]
Note:
No transactions, so no merchants to return.
Constraints
0 <= transactions.length <= 10^51 <= k <= 10^51 <= amount <= 10^9merchantId length: 1 <= merchantId.length <= 50merchantIds can be up to transactions.length.O(N + M log K) where N is transactions.length and M is the number of unique merchants.O(M + K).merchantId formats.k value).Loading editor...