Build an In-Memory Key-Value Store With Nested Transactions
Last updated: November 29, 2026
Quick Overview
Implement a key-value store supporting get, set, delete and count(value) (number of keys holding that value), plus begin, rollback and commit where transactions can be nested. Rollback undoes only the innermost transaction; commit applies all open transactions.
Amazon
November 29, 20260
0
0 solved
Implement a key-value store supporting get, set, delete and count(value) (number of keys holding that value), plus begin, rollback and commit where transactions can be nested. Rollback undoes only the innermost transaction; commit applies all open transactions.
A fundamentals question in the style of Amazon onsite rounds that resembles a tiny DynamoDB or Redis MULTI. The interviewer checks that every operation stays O(1) (no copying the whole store per transaction), that count stays consistent through rollbacks, and that nesting semantics are precise.
What the Interviewer Expects
- Keep a main dict plus a value-count index
- Record undo entries per transaction instead of copying data
- Make rollback restore both data and counts
- Define commit as discarding all undo logs
- Return clear errors for rollback or commit without a transaction
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you make this durable with a write-ahead log?
- How would you support concurrent transactions with isolation?
- How would you implement snapshot reads at a point in time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Keys and values are strings. get of a missing key returns None. count(value) is O(1). rollback or commit with no open transaction returns 'NO TRANSACTION'. Commit closes all open transactions (common ...
High-Level Design
data: dict,counts: dict[value, int].undo: list[dict], a stack with one dict per open transaction mapping key to its value before the first change in that transaction (a sentinel for 'absen...