KatInterview

Replay an Operation Log to Answer Versioned Key Lookups

Data Structure DesignAuto-graded · 13 test cases
EasyAuto-gradedData Structure Designdata structuressimulationhash map

Last updated

Tests the ability to simulate a versioned key-value store by replaying a sequence of set/delete operations and answering point-in-time key lookups.

Versioned Key-Value Store

You are given a log of operations applied to a key-value store, along with a list of queries. Each query asks: "What was the value associated with a given key immediately after a specific operation was applied?"

Operations

The operation log is a list of entries. Each entry is a dict with one of two shapes:

ShapeMeaning
{"op": "set", "key": <str>, "value": <str>}Store value under key, overwriting any previous value.
{"op": "delete", "key": <str>}Remove key from the store. If the key does not exist, this is a no-op.

Operations are applied in order, indexed from 1 (the first operation has version 1, the second has version 2, and so on).

Queries

Each query is a dict:

{"key": <str>, "version": <int>}

It asks: after replaying operations 1 through version (inclusive), what is the value of key?

  • If key exists in the store at that version, return its string value.
  • If key does not exist (was never set, or was deleted and not re-set), return None.

Your Task

Implement a function that accepts the operation log and the list of queries, and returns a list of answers (strings or None) in the same order as the queries.

Queries are independent — each one is answered against the store state at the specified version, not a running state.

Constraints

  • 0 ≤ len(ops) ≤ 2000
  • 1 ≤ len(queries) ≤ 2000
  • Each query's version satisfies 1 ≤ version ≤ len(ops) (guaranteed; ops will be non-empty when queries exist).
  • Keys and values are non-empty strings consisting of lowercase letters, digits, and underscores.
  • Values in "set" operations are non-empty strings.
  • Duplicate keys across operations are allowed; the latest "set" before or at the queried version wins.

Examples

Example 1

Input: query_versioned_store([{'op': 'set', 'key': 'x', 'value': '10'}, {'op': 'set', 'key': 'y', 'value': '20'}, {'op': 'set', 'key': 'x', 'value': '99'}, {'op': 'delete', 'key': 'y'}], [{'key': 'x', 'version': 1}, {'key': 'x', 'version': 3}, {'key': 'y', 'version': 4}])

Output: ['10', '99', None]

Example 2

Input: query_versioned_store([{'op': 'set', 'key': 'a', 'value': 'hello'}, {'op': 'delete', 'key': 'a'}, {'op': 'set', 'key': 'a', 'value': 'world'}], [{'key': 'a', 'version': 1}, {'key': 'a', 'version': 2}, {'key': 'a', 'version': 3}])

Output: ['hello', None, 'world']

Example 3

Input: query_versioned_store([{'op': 'set', 'key': 'color', 'value': 'red'}, {'op': 'set', 'key': 'size', 'value': 'large'}, {'op': 'delete', 'key': 'color'}, {'op': 'set', 'key': 'color', 'value': 'blue'}], [{'key': 'color', 'version': 2}, {'key': 'color', 'version': 3}, {'key': 'size', 'version': 2}, {'key': 'color', 'version': 4}])

Output: ['red', None, 'large', 'blue']

Hints

Hint 1

For each query, you only need to consider operations up to and including the given version — replaying the prefix of the log up to that version is the simplest correct approach.

Hint 2

A dictionary keyed by operation key tracking the most recent set/delete up to each version is sufficient; you do not need a persistent data structure.

Hint 3

If you sort or group queries by version, you can answer all queries in a single forward pass through the operation log in O(len(ops) + len(queries)) time.