Account balances from a transaction log
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's HackerRank assessment, last asked May 2026. It tests hash map, parsing and simulation.
A payments system receives a log of transactions and must turn it into account statements. Each log entry is a string with four comma-separated fields:
transaction_id,account_id,amount,timestamp
-
transaction_id: a unique, non-empty string. -
account_id: a non-empty string that names a customer or business account. -
amount: an integer. A positive amount is a deposit and a negative amount is a withdrawal. Use minor units such as cents. -
timestamp: an integer. Entries may arrive out of timestamp order.
Every account starts at 0. Apply transactions in timestamp order. When two transactions have the same timestamp, apply them in the order they appear in the log. Skip malformed lines, meaning a wrong field count, an empty id, or a non-integer number. Apply a repeated transaction_id only once.
Implement AccountBalanceProcessor:
-
balances(logs) -> List[Tuple[str, int]]: returns each account with a nonzero final balance, sorted by account id. Balances may go negative. -
balances_rejecting_overdrafts(logs): rejects any transaction that would make its account's balance negative. A rejected transaction leaves the balance unchanged. Returns the final statement and the ids of rejected transactions, in the order they were applied. -
AccountBalanceProcessor(platform)andbalances_with_coverage(logs): no transaction is rejected. When a non-platform account would go negative, the platform account pays the shortfall, so the account ends at exactly 0 and the platform is debited by the same amount. The platform's own transactions are never covered. Returns the final statement and the total amount the platform covered.
Constraints
- Up to 10^5 log lines; amounts fit in 64-bit integers.
Example
logs = ["t1,alice,100,1", "t2,bob,50,2", "t3,alice,-30,3",
"t4,bob,-80,4", "t5,carol,20,5"]
Part 1: [("alice", 70), ("bob", -30), ("carol", 20)]
Part 2: [("alice", 70), ("bob", 50), ("carol", 20)], rejected ["t4"]
Part 3 (platform = "platform"):
[("alice", 70), ("carol", 20), ("platform", -30)], covered 30
Out of order
logs = ["x2,dan,-40,20", "x1,dan,50,10"]
Part 1: [("dan", 10)] Part 2: nothing rejected, because x1 applies first
Interviewers usually add the parts one at a time. Later follow-ups can include multiple currencies, repaying the platform's coverage from later deposits, and settling all balances to zero with the fewest transfers.
Clarifying questions
- Is an amount of 0 valid, and does it count as a transaction?
- If a malformed line shares an id with a valid line, does it use up that id?
- When a duplicate id comes with different contents, which copy wins: the first in the log, or the first in timestamp order?
- In Part 3, does the platform account also show up in the statement if its balance is zero?
- Should the platform cover only the shortfall below zero, or the whole withdrawal?
What a strong answer covers
- Parses each line into a record, skipping malformed ones, and keeps the first record for each transaction id.
- Sorts the records by
(timestamp, log position)with a stable sort, then applies them in one pass to a dict of balances. That is O(n log n). - Part 2 checks the new balance before applying a withdrawal and records the rejected ids in the order they would have applied.
- Part 3 lets an account go to exactly zero, debits the platform by the shortfall, adds it to the coverage total, and never covers the platform's own transactions.
- Tests: the example, the out-of-order example, a duplicate id, malformed lines of each kind, equal timestamps applied in log order, and accounts that end at 0 and are left out.
Follow-ups
- Multiple currencies: keep balances per currency and decide how to show them.
- Repay the platform's coverage from the account's later deposits, before the account sees the money.
- Settle all balances to zero with the fewest transfers.
- Process the log as a stream where lines can arrive late, up to a time window.
Where it's asked
- Stripe · HackerRank assessment · Last asked May 2026Leveled HackerRank task; test cases map to the parts in order
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
