KatInterview

Versioned Key-Field Store with Expiry and Time Travel

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designdesignhash-mapbinary-search

Last updated

Simulate a timestamped two-level store with expiring values, conditional updates, sorted prefix scans and historical lookups, driven by a batch of operations.

Versioned Key-Field Store

You are simulating a small in-memory store. Each record is addressed by a key (string) and a field (string) and holds an integer value. Every operation carries a timestamp, and timestamps are strictly increasing across the whole sequence of operations.

You are given two parallel lists. operations[i] is the name of the i-th operation and arguments[i] is its argument list, whose first element is always the timestamp. Process the operations in order and return a list with exactly one result per operation.

Operations

OperationArgumentsBehaviorResult
set[t, key, field, value]Store value at key/field with no expiry, replacing any previous value and clearing any previous expiry.None
setWithTtl[t, key, field, value, ttl]Same as set, but the value expires at time t + ttl.None
get[t, key, field]Return the value visible at time t.the integer, or None if there is none
compareAndSet[t, key, field, expected, new_value]If the value visible at t equals expected, store new_value with no expiry.True if updated, else False
compareAndSetWithTtl[t, key, field, expected, new_value, ttl]Like compareAndSet, but on success the new value expires at t + ttl.True if updated, else False
compareAndDelete[t, key, field, expected]If the value visible at t equals expected, delete it.True if deleted, else False
scan[t, key]List every field under key that has a visible value at t.list of strings
scanByPrefix[t, key, prefix]Same as scan, restricted to fields whose name starts with prefix.list of strings
getWhen[t, key, field, at]Return the value that was visible for key/field at the earlier moment at, based on the writes performed so far.the integer, or None

Visibility rules

  • A value written at time w with a TTL is visible at time x only when x < w + ttl. It is visible at w + ttl - 1 and not visible at w + ttl.
  • A value written without a TTL never expires.
  • A deleted value is not visible. A deleted field can be set again later, and the deletion still counts as a write when answering getWhen.
  • A missing key or field has no visible value.
  • A comparison against expected only succeeds when a value is visible and equals expected. An expired, deleted or missing value never matches.
  • A failed compareAndSet, compareAndSetWithTtl or compareAndDelete changes nothing.

Scan output

  • Each entry has the form "field(value)", for example "age(21)".
  • Entries are sorted in ascending alphabetical (string) order of the field name.
  • Return an empty list if the key does not exist or no field qualifies.
  • With prefix = "", scanByPrefix behaves like scan.

getWhen

Look at the most recent write to this key/field whose write time is at most at. Return its value if it is visible at time at under the rules above. Return None if there is no such write, if that write was a deletion, or if it had already expired by at.

For example, if 10 is set at time 10 with a TTL of 2 and 18 is set at time 18, then asking about time 5 gives None, time 11 gives 10, time 15 gives None, and time 20 gives 18.

Constraints

  • len(operations) == len(arguments), and every operation name is one of the nine listed above.
  • Timestamps in the first position of each argument list are strictly increasing across the sequence.
  • Keys, fields and prefixes are strings. Values, expected, new_value and ttl are integers.
  • For getWhen, the at argument is an integer timestamp.
  • Aim for roughly logarithmic time per getWhen and at most a sort of one key's fields per scan.

Examples

Example 1

Input: run_versioned_store(['set', 'get', 'setWithTtl', 'get', 'get', 'set', 'getWhen', 'getWhen', 'getWhen', 'getWhen'], [[1, 'a', 'x', 10], [2, 'a', 'x'], [10, 'a', 'x', 10, 2], [11, 'a', 'x'], [12, 'a', 'x'], [18, 'a', 'x', 18], [19, 'a', 'x', 5], [19, 'a', 'x', 11], [19, 'a', 'x', 15], [20, 'a', 'x', 20]])

Output: [None, 10, None, 10, None, None, 10, 10, None, 18]

Example 2

Input: run_versioned_store(['set', 'compareAndSet', 'get', 'compareAndDelete', 'get'], [[1, 'user', 'age', 20], [2, 'user', 'age', 20, 21], [3, 'user', 'age'], [4, 'user', 'age', 21], [5, 'user', 'age']])

Output: [None, True, 21, True, None]

Hints

Hint 1

Keep two structures: one holding the latest record for each key/field, and one append-only list of every write per key/field. Because timestamps only increase, each list is already sorted by time.

Hint 2

Treat deletion as a special write (a tombstone with no value) rather than removing data, and write one visibility check, 'not a tombstone and (no expiry or t < expiry)', that every operation reuses. Do not evict anything eagerly; just test expiry at read time.

Hint 3

For getWhen, binary-search the per-field list of write times for the last write at or before `at`, then apply the same visibility check at time `at`. Keep a separate list of timestamps so the search stays logarithmic.