KatInterview

Expiring Key-Value Store Driven by a Virtual Clock

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designdesignhash-mapheap

Last updated

Simulate a key-value store with per-key expiry on a manual clock, handling overwrites, deletes and cleanup of expired entries via a command list.

You are building a small in-memory cache where every entry carries its own lifetime. Instead of reading a real clock, the cache runs on a virtual clock that you move forward yourself, so behavior is fully deterministic.

Write a function that takes a list of commands and their arguments, replays them in order against an initially empty store, and returns the result of each command.

Virtual clock

  • The clock starts at 0.0.
  • It changes only through the advance command, which adds the given amount to the current time (use floating-point addition).

Entry lifetime

  • A put at time t with lifetime ttl gives the entry an expiry time of t + ttl.
  • An entry is live only while now < expiry. When now >= expiry, the entry is expired and must behave as if it does not exist.
  • Writing a key that already holds a live entry replaces both its value and its expiry. The old lifetime is discarded completely.
  • Writing a key whose previous entry has already expired simply creates a fresh entry.
  • A stored value may be None. A lookup of such a key and a lookup of a missing key both return None, and that is acceptable here.

Commands

Each command is a string, and the matching element of params is its argument list.

CommandArgumentsEffectResult recorded
"put"[key, value, ttl]Store the value with the given lifetime.None
"get"[key]Look up the key.The value if the key has a live entry, otherwise None
"delete"[key]Remove the key.True if a live entry was removed, otherwise False
"advance"[amount]Move the clock forward by amount.None
"cleanup"[]Physically discard every expired entry.None
"size"[]Count the entries that are currently live.The count (an integer)

Expired entries must never be observable. Before processing any command, entries whose expiry time is at or before the current time must already be treated as gone. Deleting a key whose entry has already expired returns False.

Task

Implement the function so it returns a list with one result per command, in order. Beyond correctness, a good solution should not rescan the whole store to find expired entries, and should not let stale bookkeeping grow without bound when keys are overwritten or deleted repeatedly.

Constraints

  • len(commands) == len(params).
  • Every command is one of the six listed above, with the argument shape shown.
  • Keys are strings. Values are arbitrary (including None).
  • ttl is a positive number. advance amounts are non-negative numbers.
  • The number of commands can be large, so aim for roughly O(log n) amortized work per command.

Examples

Example 1

Input: process_ttl_commands(['put', 'get', 'advance', 'get', 'delete', 'size'], [['a', 10, 5], ['a'], [5], ['a'], ['a'], []])

Output: [None, 10, None, None, False, 0]

Example 2

Input: process_ttl_commands(['put', 'put', 'get', 'advance', 'get', 'size'], [['x', 'old', 10], ['x', 'new', 3], ['x'], [2], ['x'], []])

Output: [None, None, 'new', None, 'new', 1]

Example 3

Input: process_ttl_commands(['put', 'get', 'advance', 'cleanup', 'size'], [['nullable', None, 4], ['nullable'], [4], [], []])

Output: [None, None, None, None, 0]

Example 4

Input: process_ttl_commands(['get', 'delete', 'put', 'advance', 'put', 'get'], [['missing'], ['missing'], ['k', 1, 2], [2], ['k', 2, 5], ['k']])

Output: [None, False, None, None, None, 2]

Hints

Hint 1

Keep a dictionary from key to its current entry (expiry, value). Because an expired entry must act as absent, run an expiry sweep before handling every command.

Hint 2

Use a min-heap ordered by expiry time so the sweep only touches entries that are actually due. Overwrites and deletes cannot cheaply remove items from the heap, so leave the old records there.

Hint 3

Give each put a unique version number and store it in both the dictionary entry and the heap record. When a heap record is popped, delete the key only if the dictionary still holds that same expiry and version; otherwise the record is stale and is ignored.

Hint 4

Stale heap records can pile up under many overwrites or deletes. When the heap grows well beyond the number of stored keys, rebuild it from the dictionary and heapify.