Implement command-driven in-memory key-value database
LyftSoftware Engineer · OnsiteLast updated
This question evaluates a candidate's ability to design efficient in-memory data structures and implement transactional semantics for operations like SET/GET/DELETE/COUNT, including handling nested transactions, rollbacks, and commits.
Implement a command-driven in-memory key–value database. Supported commands (one per line):
- SET key value
- GET key → print value or NULL
- DELETE key
- COUNT value → print how many keys currently map to this value
- BEGIN → start a transaction
- ROLLBACK → undo changes in the most recent open transaction; if none, print NO TRANSACTION
- COMMIT → make all open transaction changes permanent. Constraints: up to 100,000 commands; keys and values are ASCII strings; average O(
- per operation; memory must remain within reasonable bounds. Design data structures to support COUNT efficiently and implement transactional semantics using an undo log or equivalent. Specify I/O format precisely and discuss edge cases (nested transactions, deleting non-existent keys, multiple keys sharing a value).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.