KatInterview

Snapshot Reads from a Multi-Version Key-Value Log

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designbinary-searchhash-tabledesign

Last updated

Tests whether you can design a versioned key-value store that answers "what was the value at time T?" in sub-linear time using binary search on per-key version histories.

You are building a simple multi-version data store. Every write is recorded permanently alongside its timestamp, and reads ask for the state of a key at a particular point in time. Nothing is ever overwritten or deleted.

The store is driven by a sequence of operations processed from first to last. Your function receives the full list and returns one result per read operation, in the order the reads appear.

Operations

Each operation is a tuple in one of these two forms:

  • ("set", key, value, timestamp) — record value for key at time timestamp. Earlier versions of key are kept.
  • ("get", key, timestamp) — look up key as of time timestamp.

Read Rules

A get for key at time timestamp considers only the set operations for that same key that appear earlier in the operation list. Among those, it returns the value whose timestamp is the largest one that does not exceed timestamp.

If no qualifying version exists (the key has never been written, or every prior write for that key has a timestamp strictly greater than timestamp), return the empty string "".

Constraints

  • 1 <= len(operations) <= 2 * 10^5
  • Every key and value is a non-empty string of at most 100 lowercase alphanumeric characters.
  • 1 <= timestamp <= 10^7 for every operation.
  • The timestamps of set operations strictly increase in the order they appear across the entire operation list (regardless of key). Timestamps of get operations may appear in any order.
  • Return a list containing one string per get operation, in the order those gets appear. Return an empty list if there are no get operations.

Examples

Example 1

Input: run_versioned_store([['set', 'price', '10', 5], ['get', 'price', 4], ['get', 'price', 5], ['set', 'price', '12', 9], ['get', 'price', 8], ['get', 'price', 9], ['get', 'price', 100]])

Output: ['', '10', '10', '12', '12']

Example 2

Input: run_versioned_store([['set', 'a', 'x', 1], ['set', 'b', 'y', 2], ['get', 'a', 2], ['get', 'b', 1], ['set', 'a', 'z', 3], ['get', 'a', 3], ['get', 'c', 3]])

Output: ['x', '', 'z', '']

Hints

Hint 1

For each key, think about what data structure lets you answer 'largest timestamp ≤ T' without scanning every version. A sorted sequence supports this with binary search.

Hint 2

The global guarantee that set timestamps strictly increase across the whole operation list has a useful consequence: when you append a new version to any key's history, that list is always already in sorted order.

Hint 3

Python's bisect module has two variants — bisect_left and bisect_right. Think carefully about which one correctly includes the case where the get timestamp exactly matches a stored timestamp, and what to do when the returned index is 0.