Key-Value Store With Nested Begin, Rollback, and Commit
Last updated: November 29, 2026
Quick Overview
Build an in-memory key-value store that supports SET, GET, DELETE, COUNT(value) and transaction commands BEGIN, ROLLBACK and COMMIT, where transactions can nest. ROLLBACK undoes only the innermost open transaction and COMMIT makes all open transactions permanent.
0
0
0 solved
Build an in-memory key-value store that supports SET, GET, DELETE, COUNT(value) and transaction commands BEGIN, ROLLBACK and COMMIT, where transactions can nest. ROLLBACK undoes only the innermost open transaction and COMMIT makes all open transactions permanent.
A frequent style of Google onsite question that turns into a state management exercise. The interviewer checks that GET and COUNT stay O(1) during transactions, that rollback restores exact prior state including deletes, and that nested blocks compose.
What the Interviewer Expects
- Keep a main map plus a value-count map for O(1) COUNT
- Record undo entries per transaction level instead of copying the whole map
- Store the old value (or absence) only the first time a key changes in a level
- Make ROLLBACK and COMMIT return an error when no transaction is open
- Explain the cost of each command
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 you add snapshot isolation for concurrent readers?
- How would you persist committed state with a write-ahead log?
- What changes if COMMIT should only commit the innermost transaction?
- How would you bound memory for a very long transaction?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Commands: SET k v, GET k (value or NULL), DELETE k, COUNT v (number of keys whose value equals v), BEGIN, ROLLBACK (undo innermost; NO TRANSACTION if none), COMMIT (close all; `NO TR...
High-Level Design
State: data dict, counts dict from value to number of keys, and undo, a stack of dicts. Each dict maps key to its value before the first change in that level (None meaning absent). A private `...