KatInterview

Versioned Key-Value Log with Point-in-Time Reads

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designbinary searchhash mapversioning

Last updated

Tests per-key version tracking and efficient predecessor search to retrieve the most recent value written at or before any queried timestamp.

Versioned Key-Value Log with Point-in-Time Reads

You are building a simple versioned data store. Every write records a string value under a string key at a specific integer timestamp. Reads ask for a snapshot view: given a key and a point in time, what was the most recently written value for that key at or before that moment?

Implement a function that processes a mixed sequence of write and read operations in order and returns the answer to every read.


Operations

Each element of the input list is one of two forms:

  • ["set", key, value, timestamp] — stores value under key at timestamp. Produces no output.
  • ["get", key, timestamp] — retrieves the value associated with key from the write whose timestamp is the largest one that does not exceed timestamp. If no such write exists, produces "".

key and value are strings; timestamp is a positive integer.


Rules

  • Operations are executed strictly in list order. A get can only observe set operations that precede it in the list.
  • Across all set operations in the entire list, timestamps are strictly increasing. No two writes (even to different keys) share a timestamp.
  • A get timestamp can fall anywhere in the allowed range — before all writes to that key, between writes, exactly on a write, or after all writes.
  • The return value is a list containing one string per get, in the order those gets appear. If the input contains no get operations, return an empty list.

Constraints

  • 1 <= len(operations) <= 2 * 10^5
  • Every key and value is a non-empty string of at most 20 lowercase English letters and digits.
  • 1 <= timestamp <= 10^9
  • Write timestamps are strictly increasing across the full operation list.

Examples

Example 1

Input: replay_versioned_store([['set', 'color', 'red', 3], ['get', 'color', 2], ['get', 'color', 3], ['get', 'color', 5], ['set', 'color', 'blue', 8], ['get', 'color', 7], ['get', 'color', 8], ['get', 'color', 100]])

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

Example 2

Input: replay_versioned_store([['set', 'a', 'x', 1], ['set', 'b', 'y', 2], ['get', 'a', 2], ['get', 'b', 1], ['get', 'c', 5]])

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

Example 3

Input: replay_versioned_store([['set', 'k', 'v1', 10], ['get', 'k', 50], ['set', 'k', 'v2', 20], ['get', 'k', 50], ['get', 'k', 15]])

Output: ['v1', 'v2', 'v1']

Hints

Hint 1

For each key separately, think about what structure naturally holds all of its write timestamps in sorted order — and consider whether you even need to sort explicitly given the global ordering guarantee on writes.

Hint 2

Once you have a sorted list of timestamps for a key, a get is asking for the rightmost timestamp that does not exceed the query value. That is a classic predecessor search, which binary search handles in O(log n).

Hint 3

Be careful about which bisect variant you choose. A get at exactly a write's timestamp should return that write's value, so make sure your index arithmetic lands on the equal element rather than the one before it.