KatInterview

Simulate an Expiring Key-Value Store with Read-Through Loads

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designsimulationhash-mapcache

Last updated

Simulate a time-aware key-value store with expiry, stale entries that stay stored, and a read-through fetch whose loader outcome is given as data, with strict input validation.

Simulate an Expiring Key-Value Store with Read-Through Loads

You are given a list of commands to run, in order, against an initially empty key-value store whose entries can expire. Write a function that returns one result list per command.

Commands

Each command is a list. The first element is the command name and the second is the timestamp time_ms.

CommandFormat
put["put", time_ms, key, value, ttl_ms]
get["get", time_ms, key]
delete["delete", time_ms, key]
size["size", time_ms]
fetch["fetch", time_ms, key, ttl_ms, loader_status, loader_payload]

Keys are strings. Stored values are integers. Timestamps are nonnegative integers and must never decrease from one command to the next (equal timestamps are allowed).

Expiry rules

  • If ttl_ms > 0, an entry written at time t expires at t + ttl_ms. It is usable only while the current time is strictly less than that expiry, so at exactly t + ttl_ms it is already unusable.
  • If ttl_ms == 0, the entry never expires.
  • An expired entry is not removed automatically. It stays stored until it is removed by delete or overwritten by put or a successful fetch. Only delete and overwriting remove it. get, size, and fetch never remove anything.

Call an entry valid at time t if its key is stored and it has not expired at t.

Results

  • put: store value under key with the given TTL, replacing any existing entry (including its expiry), and return ["ok"].
  • get: return ["ok", value] if the key holds a valid entry, otherwise ["ok", null].
  • delete: remove the key if it is stored, and return ["ok", true] if it was stored (even if expired) or ["ok", false] if it was not stored.
  • size: return ["ok", count], where count is the number of stored keys, including expired ones.
  • fetch: the loader is not a function. Its outcome is supplied as data in loader_status and loader_payload.
    • If the key holds a valid entry, return ["ok", cached_value] and do not look at the loader fields.
    • Otherwise (key missing or expired), look at loader_status:
      • "ok": loader_payload is an integer. Store it under key, replacing any existing entry, with expiry computed from the current time and ttl_ms using the same rules as put. Return ["ok", loader_payload].
      • "error": loader_payload is a string reason. Leave the store completely unchanged (an expired entry stays stored) and return ["error", reason].

Invalid input

Raise ValueError if any of the following occurs:

  • The command list is not a list, or a command is not a non-empty list.
  • The command name is not a string or is not one of the five names above.
  • A command has the wrong number of elements for its name.
  • A timestamp is not an integer, is negative, or is smaller than the previous command's timestamp.
  • A key is not a string.
  • A put value or TTL, or a fetch TTL, is not an integer, or a TTL is negative.
  • On a fetch that does not hit a valid entry: loader_status is neither "ok" nor "error", an "ok" payload is not an integer, or an "error" payload is not a string.

Booleans are not accepted as integers. On a fetch that hits a valid entry, the loader fields are not examined.

Constraints

  • 0 <= len(commands) <= 200000
  • Timestamps are nonnegative integers in nondecreasing order.
  • TTL values are nonnegative integers.
  • Values and "ok" payloads are integers. Keys and error reasons are strings.
  • Results are compared exactly.

Examples

Example 1

Input: run_expiring_store([['put', 0, 'u', 1, 5], ['get', 4, 'u'], ['get', 5, 'u'], ['size', 5], ['fetch', 6, 'u', 10, 'ok', 2], ['fetch', 7, 'v', 0, 'error', 'db_down'], ['size', 7], ['delete', 8, 'u'], ['size', 8]])

Output: [['ok'], ['ok', 1], ['ok', None], ['ok', 1], ['ok', 2], ['error', 'db_down'], ['ok', 1], ['ok', True], ['ok', 0]]

Hints

Hint 1

Separate two ideas: a key being stored (what size and delete care about) and a stored entry being valid right now (what get and fetch care about).

Hint 2

A fetch has two stages: first check for a valid entry and return it immediately; only if that fails, act on the supplied loader status.

Hint 3

Represent no-expiry as a sentinel such as None, and treat an entry as valid when the sentinel is present or the current time is strictly less than the expiry. Remember that Python's bool is a subclass of int, so check the type exactly.