KatInterview

Build an In-Memory Key-Value Store with Savepoints

Data Structure DesignModel answer
MediumModel answerData Structure Designtransactionsundo-logkey-value-store

Last updated

Design a transactional key-value store supporting nested transactions, named savepoints, and full rollback using an undo-log approach.

In-Memory Key-Value Store with Transactions and Savepoints

You are building an in-memory key-value store that supports multi-level transactions. Each transaction can contain named checkpoints (savepoints) and can be rolled back either fully or to any named checkpoint.

Implement a class KVStore with the following interface:

  • get(key: str) -> Optional[str] — Returns the current value for key, or None if absent.
  • set(key: str, value: str) -> None — Writes key = value in the current context.
  • delete(key: str) -> None — Removes key if it exists; does nothing otherwise.
  • begin() -> None — Opens a new transaction. Transactions may be nested.
  • checkpoint(name: str) -> None — Records the current state under name within the active transaction. Reusing a name overwrites the previous checkpoint with that name.
  • revert_to(name: str) -> bool — Restores the store to the state it was in when name was recorded. Returns True if the checkpoint was found, False otherwise. The checkpoint itself remains active after the revert.
  • revert() -> bool — Undoes all changes made in the innermost open transaction and closes it. Returns True on success, or False if no transaction is open.
  • commit() -> bool — Finalises all open transactions, permanently applying every pending change. Returns True on success, or False if no transaction is open.

Constraints

  • get, set, and delete must run in O(1) average time regardless of transaction depth.
  • Writes outside any transaction take effect immediately and permanently.
  • Be prepared to explain the space/time trade-offs between an undo-log design and a layered-map design.

Example

kv = KVStore()
kv.set("x", "1")

kv.begin()
kv.set("x", "2")
kv.checkpoint("cp1")
kv.set("y", "3")
kv.delete("x")

assert kv.get("x") is None
assert kv.get("y") == "3"

# Restore to cp1
assert kv.revert_to("cp1") is True
assert kv.get("x") == "2"   # restored
assert kv.get("y") is None  # write after cp1 was undone

# Discard the whole transaction
assert kv.revert() is True
assert kv.get("x") == "1"   # back to pre-transaction value
assert kv.get("y") is None

Edge Cases to Handle

  • A key created inside a transaction and then deleted within the same transaction — after rollback it should be fully absent.
  • Calling commit, revert, or revert_to when no transaction is open.
  • A checkpoint name that appears in multiple nested transaction levels — the innermost match should win.
  • Calling revert_to on the same checkpoint more than once.

Hints

Hint 1

Keep a single live dictionary as the source of truth. Before each write, record what the key looked like before the change — that's your undo record. Reversing writes then means replaying those records in reverse order.

Hint 2

Represent each open transaction as a (log_start_index, savepoints_dict) pair on a stack. A checkpoint is just the current log length stored under its name. Rolling back means popping undo records off the log until you reach the saved index.

Hint 3

Watch out for stale checkpoint positions: after a revert_to, any checkpoints that pointed past the new log length would be wrong. Prune them. Also decide what _MISSING sentinel to use so that 'key was absent' is distinguishable from any real string value.