KatInterview

Batch Simulation of an Expiring LRU Key-Value Store

Data Structure DesignAuto-graded · 24 test cases
MediumAuto-gradedData Structure Designhash maplinked listLRU cache

Last updated

Replay a batch of timestamped get and set operations against a fixed-capacity store that combines per-key expiry with least-recently-used eviction.

Problem Statement

You are given a fixed-capacity key-value store, a list of operations to replay in order, and the arguments for each operation. Simulate the store and return the result of every get operation.

Each operation is either "set" or "get". The argument list at the same index holds that operation's arguments:

  • "set" takes [timestamp, key, value, ttl]. It stores value under key with an expiration time of timestamp + ttl.
  • "get" takes [timestamp, key]. It asks for the value currently stored under key.

Rules for a live key. A key is live at time t when t < expires_at. At t == expires_at the key is already dead.

Rules for get(t, key):

  • If key is not stored, the result is -1.
  • If key is stored but t >= expires_at, remove the key from the store and the result is -1.
  • Otherwise the result is the stored value. The key also becomes the most recently used entry.

Rules for set(t, key, value, ttl):

  • If key is already stored (even if it has expired but has not yet been removed), overwrite its value and expiration with the new ones and make it the most recently used entry. This never causes an eviction.
  • If key is not stored and the store already holds capacity entries, first evict the least recently used entry, then insert the new key as the most recently used entry.
  • Expired entries are not special during eviction. Eviction always removes the least recently used entry, whether or not it has expired. An expired entry keeps occupying a slot until a get removes it or eviction reaches it.

Output. Return a list with one element per operation, in order. For a "set" operation, the element is None. For a "get" operation, the element is the returned value (-1 when missing or expired).

Timestamps are non-decreasing across consecutive operations, so you can rely on that ordering.

Function Signature

Implement the function run_expiring_lru(capacity, ops, args) as described in the starter code. The parameters are:

  • capacity: the maximum number of entries the store can hold.
  • ops: a list of "set" or "get" strings.
  • args: a list where args[i] holds the argument list for ops[i].

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= len(ops) == len(args) <= 2 * 10^5
  • Each ops[i] is either "set" or "get".
  • 0 <= timestamp <= 10^9
  • 1 <= ttl <= 10^6
  • 1 <= len(key) <= 30, and keys are alphanumeric strings.
  • 0 <= value <= 10^9
  • For consecutive operations, timestamp_i <= timestamp_{i+1}.
  • Each operation must run in O(1) average time. Scanning the whole store on every call is too slow.

Examples

Example 1

Input: run_expiring_lru(2, ['set', 'set', 'get', 'set', 'get', 'get', 'get'], [[1, 'a', 10, 5], [2, 'b', 20, 10], [3, 'a'], [4, 'c', 30, 5], [7, 'a'], [7, 'b'], [7, 'c']])

Output: [None, None, 10, None, -1, -1, 30]

Example 2

Input: run_expiring_lru(1, ['set', 'get', 'set', 'get'], [[0, 'k', 100, 10], [10, 'k'], [11, 'k', 200, 5], [12, 'k']])

Output: [None, -1, None, 200]

Example 3

Input: run_expiring_lru(2, ['set', 'set', 'get', 'get'], [[0, 'x', 1, 5], [1, 'y', 2, 5], [5, 'x'], [5, 'y']])

Output: [None, None, -1, 2]

Hints

Hint 1

Pair a hash map (key to value and expiry) with a recency order. Python's OrderedDict provides both hash lookup and O(1) move-to-end and pop-from-front.

Hint 2

Expiry is lazy. Check `t >= expires_at` only when a key is read with `get`. Never scan for expired keys.

Hint 3

In `set`, an existing key is an in-place update that never evicts. Only a brand-new key can trigger eviction, and that eviction always removes the least recently used entry.