KatInterview

Build a Time-Aware Key-Value Store with Expiration Logic

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designhash-tabletime-simulationexpiration

Last updated

Tests data structure design, time-based expiration semantics, and clean handling of missing or stale entries in an in-memory cache.

You are building an in-memory key-value store that supports automatic expiration of entries. Your store must handle write, read, and removal operations, along with the ability to simulate the passage of time for testing purposes.

Implement a function that processes a sequence of operations on the store and returns the result of each operation.

Operations

Your function receives two parallel lists: operations (operation names) and arguments (parameters for each operation). Process them in order and return a list of results.

Supported operations:

  • "set" with arguments [key, value] or [key, value, ttl]: Store the value under the given key. If ttl (time-to-live, in seconds) is provided, the entry expires after that duration from the current simulated time. If ttl is omitted, the entry never expires. Overwrites any existing entry for that key. Returns None.

  • "get" with arguments [key]: Retrieve the value stored under the key. If the key does not exist or has expired, return None. Expired entries should be removed when accessed.

  • "delete" with arguments [key]: Remove the entry for the key. Returns True if a non-expired entry was removed, False if the key did not exist or had already expired.

  • "advance_time" with arguments [seconds]: Move the simulated clock forward by the given number of seconds. Returns None.

Expiration Rules

  • Time starts at 0.0 seconds.
  • An entry set with ttl=T expires at time current_time + T.
  • An entry is considered expired if the current time is greater than or equal to its expiration timestamp.
  • Overwriting a key replaces both its value and expiration time.
  • Keys without a ttl never expire unless deleted or overwritten.

Constraints

  • 1 ≤ number of operations ≤ 1000
  • Keys are non-empty strings
  • Values can be any Python object (integers, strings, lists, dictionaries, etc.)
  • ttl when provided is a positive number (int or float)
  • seconds in advance_time is non-negative
  • All operations are valid (no unknown operation names)

Examples

Example 1

Input: run_cache_operations(['set', 'get', 'advance_time', 'get', 'delete', 'get'], [['a', 1, 5], ['a'], [5], ['a'], ['a'], ['a']])

Output: [None, 1, None, None, False, None]

Example 2

Input: run_cache_operations(['set', 'advance_time', 'set', 'advance_time', 'get'], [['x', 'old', 10], [4], ['x', 'new', 3], [3], ['x']])

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

Example 3

Input: run_cache_operations(['get', 'delete', 'set', 'delete'], [['missing'], ['missing'], ['k', {'v': [1, 2]}], ['k']])

Output: [None, False, None, True]

Hints

Hint 1

Store each entry as a tuple containing both the value and its expiration timestamp (or None if it never expires).

Hint 2

When retrieving a key, check if it has expired by comparing the current simulated time against the stored expiration timestamp.

Hint 3

Use lazy deletion: remove expired entries when they are accessed rather than scanning the entire store periodically.