Account Balance Store With Nested Transactions and Rollback

Last updated: November 29, 2026

Quick Overview

Implement an in-memory balance store with `credit`, `debit` (which fails on insufficient funds), `balance`, and nested `begin`, `commit` and `rollback`. Rollback undoes only the innermost open transaction; commit folds it into its parent.

Stripe
Coding & Algorithms
Software Engineer
Stripe
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Implement an in-memory balance store with `credit`, `debit` (which fails on insufficient funds), `balance`, and nested `begin`, `commit` and `rollback`. Rollback undoes only the innermost open transaction; commit folds it into its parent.

Stripe's ledger code must make multi-step balance changes all-or-nothing, and nested transactions appear whenever one operation, such as a payout with fees, calls another that also wants atomicity. The interviewer checks a clear design for nesting, correct rollback semantics, O(1) reads and that a failed debit leaves no trace.

What the Interviewer Expects
  • Choose between an overlay-per-transaction design and an undo log, and justify it
  • Make reads O(1) regardless of nesting depth
  • On commit of a nested transaction, hand its undo entries to the parent so an outer rollback still undoes them
  • Fail a debit before mutating anything when funds are insufficient
  • Raise clear errors for commit or rollback with no open transaction
Key Topics to Cover
data structure design
undo log
transactions
stacks
hash maps
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add a `count_accounts_with_balance(x)` query in O(1)?
  • How would this map onto savepoints in a relational database?
  • How would you make it thread-safe, and what isolation level would you offer?
  • How would you persist it so a crash mid-transaction is recovered correctly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Amounts are integer cents and positive. Balances start at 0 and never go negative. Operations outside any transaction apply immediately. rollback restores every balance changed since the matching `b...

High-Level Design

Apply writes directly to a balances dict and keep a stack of undo logs, one per open transaction. Each write in a transaction first records (account, old_value) in the top log. rollback pops the...


Submit Your Answer
Markdown supported