Process Transactions and Compute Balances
StripeSoftware Engineer · Technical ScreenLast updated
This problem tests your ability to manage stateful account data, handle time-ordered transaction processing, and implement basic validation logic for financial integrity.
Process Transactions and Compute Balances
You are building a simple ledger system for a banking platform. You are given a list of transactions, where each transaction consists of a transaction_id, timestamp, sender_id, receiver_id, and amount.
Rules:
- All accounts start with a balance of 0.
- Transactions must be processed in chronological order. If two transactions have the same timestamp, process them by the order in which they appear in the input list.
- A transaction is only successful if the
sender_idhas sufficient funds (balance >= amount). If a transaction fails, it is ignored and does not affect any balances. - Account IDs can be any string. If a
sender_idorreceiver_idhas never appeared before, treat their starting balance as 0.
Input
transactions: A list of objects/dictionaries with the following fields: id (str), timestamp (int), sender (str), receiver (str), amount (int).
Output
Return a dictionary where keys are account IDs and values are their final balances.
Example
transactions = [
{"id": "t1", "timestamp": 10, "sender": "A", "receiver": "B", "amount": 50},
{"id": "t2", "timestamp": 5, "sender": "B", "receiver": "A", "amount": 20},
{"id": "t3", "timestamp": 15, "sender": "A", "receiver": "B", "amount": 60}
]
# Expected Output:
# {'A': -10, 'B': 10}
# Explanation:
# 1. t2 (time 5) is processed first: B sends 20 to A. Balances: B=-20, A=20.
# 2. t1 (time 10): A sends 50 to B. A has 20, needs 50. Transaction fails.
# 3. t3 (time 15): A sends 60 to B. A has 20, needs 60. Transaction fails.
Constraints
amount> 0.- Number of transactions: 1 to 10^5.
- Timestamps: 1 to 10^9.
Hints
Hint 1
Sort the transaction list by timestamp first, maintaining the relative order for equal timestamps.
Hint 2
Use a hash map (dictionary) to keep track of the current balance for every account ID encountered.
Hint 3
Iterate through the sorted list, checking the sender's balance in your map before updating values for both sender and receiver.
Follow-up questions
- How would you modify the system to handle multiple currencies, where transactions between different currencies require an exchange rate conversion?
- If the system needs to support millions of transactions per second, how would you partition the data across multiple servers?
- How would you ensure atomicity so that if the system crashes midway, the ledger remains in a consistent state?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.