KatInterview

Snapshot-based key-value store with checkpoint and reload

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesignserializationkey-value store

Last updated

Simulate a key-value store that serializes its full contents on checkpoint and replaces live state on reload, testing state management and round-trip serialization.

Snapshot-Based Key-Value Store

You are building an in-memory key-value store that can save its contents to a simulated durable medium and later reload them. You will receive a sequence of commands and must return the result of each one.

The store supports four commands:

  • set with arguments [key, value]: store value under key, overwriting any previous value. Returns None.
  • get with arguments [key]: return the value stored under key, or None if the key is absent. Does not modify state.
  • shutdown with no arguments: serialize the entire current contents of the store into bytes (for example, UTF-8 encoded JSON) and write those bytes to the medium, replacing any earlier snapshot. The live in-memory data is not cleared, so later get calls still see it. Returns None.
  • restore with no arguments: discard the live in-memory data and rebuild it from the medium's most recent snapshot. If no snapshot has ever been written, the store becomes empty. Returns None.

Behavior details to honor:

  • Writes made after the last shutdown are lost when restore runs, because restore replaces the live data entirely.
  • Only the most recent shutdown snapshot is kept. Earlier snapshots are overwritten.
  • Keys are strings. Values are JSON-compatible (strings or integers in the tests).
  • If a command name is not one of the four above, raise ValueError.

Implement the function so that it processes the commands in order and returns a list with one result per command.

Input

  • commands: a list of command names.
  • params: a list of argument lists, where params[i] holds the arguments for commands[i].

Output

A list of the same length as commands. Each entry is the result of the corresponding command: None for set, shutdown, and restore, and the looked-up value (or None) for get.

Constraints

  • 0 <= len(commands) == len(params) <= 10^5
  • Each commands[i] is one of "set", "get", "shutdown", "restore", or an unknown string that must trigger ValueError.
  • For set, params[i] has exactly two elements: a string key and a JSON-compatible value.
  • For get, params[i] has exactly one element: a string key.
  • For shutdown and restore, params[i] is an empty list.
  • The total size of all stored keys and values is bounded so that a full snapshot fits in memory.

Examples

Example 1

Input: run_snapshot_store(['set', 'get', 'shutdown', 'set', 'get', 'restore', 'get'], [['name', 'Ada'], ['name'], [], ['name', 'Grace'], ['name'], [], ['name']])

Output: [None, 'Ada', None, None, 'Grace', None, 'Ada']

Example 2

Input: run_snapshot_store(['get', 'restore', 'get', 'set', 'shutdown', 'set', 'restore', 'get'], [['missing'], [], ['missing'], ['x', 7], [], ['x', 9], [], ['x']])

Output: [None, None, None, None, None, None, None, 7]

Hints

Hint 1

Keep two things separately: the live dictionary that `set` and `get` act on, and a stored snapshot that only `shutdown` writes and only `restore` reads.

Hint 2

Because `restore` replaces the live data, a snapshot must be an independent copy. Storing a serialized form (for example, JSON bytes) guarantees that later `set` calls cannot change it.

Hint 3

Handle `restore` before any `shutdown` by resetting to an empty dictionary, and make sure `get` returns `None` for missing keys rather than raising.