Account Ledger With Nested Begin Commit and Rollback
Last updated: November 29, 2026
Quick Overview
Implement an in-memory ledger with deposit, withdraw (fails on insufficient funds) and balance, plus begin, commit and rollback that can be nested. Rollback undoes only the innermost open transaction; commit folds it into its parent.
SoFi
November 29, 20260
0
0 solved
Implement an in-memory ledger with deposit, withdraw (fails on insufficient funds) and balance, plus begin, commit and rollback that can be nested. Rollback undoes only the innermost open transaction; commit folds it into its parent.
Money movement at SoFi, such as moving funds between checking, savings vaults and investing, must be all or nothing. This design-a-data-structure question checks whether you can implement nested transactions efficiently with undo logs instead of copying the whole state, and reason precisely about commit semantics.
What the Interviewer Expects
- Uses a stack of undo logs, not full copies of the balance map
- Records only the first prior value of each account per layer
- Merges an inner layer into its parent on commit without overwriting the parent's older snapshot
- Restores missing accounts correctly on rollback
- Returns clear results for rollback or commit with no open transaction
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would this map onto database savepoints?
- How would you make it thread-safe for concurrent members?
- What would you change to support 'commit all' versus 'commit innermost'?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Balances in integer cents. withdraw returns False instead of going negative. begin opens a layer; rollback reverts every change since the matching begin; commit keeps the changes but, if nes...
High-Level Design
Copying the entire map on each begin is O(accounts) per transaction. Instead keep the live map plus a stack of undo logs. When an account is first written inside a layer, store its previous value (o...