KatInterview

Simulate a TTL-Aware LRU Cache with Operation Log

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdata structurescachesimulation

Last updated

Tests ability to simulate a fixed-capacity LRU cache where entries expire after a per-entry TTL, processing a sequence of GET/PUT/DELETE operations.

TTL-Aware LRU Cache Simulation

You are given a cache capacity and a list of operations to replay against a TTL-aware LRU cache. The cache stores string keys mapped to integer values. Each entry has a time-to-live (TTL) measured in abstract time units.

Cache Rules

  1. Capacity: The cache holds at most capacity live entries. An entry that has expired does not count toward capacity.
  2. Time: Each operation carries a non-negative integer timestamp t. Operations are given in non-decreasing timestamp order.
  3. Expiry: An entry inserted (via PUT) at time t with TTL ttl is live during the interval [t, t + ttl). At time t + ttl and beyond it is considered expired and must not be returned by GET.
  4. LRU eviction: When a PUT would exceed capacity (counting only live entries), evict the least recently used live entry. "Recently used" means the last time that key was the target of a successful GET or a PUT. Expired entries are removed silently before eviction is considered.
  5. GET semantics: Returns the value for the key if the key exists and is live at time t; otherwise returns -1. A successful GET (key found and live) updates that key's LRU position.
  6. PUT semantics: Inserts or updates the key with the new value and new TTL starting at time t. This counts as a "use" for LRU purposes. If the key already exists (live or expired), it is replaced in-place (its LRU rank is refreshed).
  7. DELETE semantics: Removes the key immediately if present (live or expired); does nothing otherwise. Returns nothing meaningful.

Input

capacity: int          # 1 ≤ capacity ≤ 100
operations: list       # each element is one of:
    ["GET", t, key]
    ["PUT", t, key, value, ttl]
    ["DELETE", t, key]
  • 1 ≤ capacity ≤ 100
  • 0 ≤ t ≤ 10_000 (non-decreasing within the list)
  • Keys are non-empty strings of lowercase letters, length 1–20
  • Values are integers in [-10_000, 10_000]
  • 1 ≤ ttl ≤ 10_000
  • 1 ≤ len(operations) ≤ 2_000

Output

Return a list of integers — one entry per GET operation (in order), each being the value returned by that GET (or -1 if the key is absent or expired).

Constraints

  • 1 ≤ capacity ≤ 100
  • 0 ≤ t ≤ 10_000, non-decreasing
  • 1 ≤ len(operations) ≤ 2_000
  • 1 ≤ ttl ≤ 10_000

Examples

Example 1

Input: simulate_ttl_lru_cache(2, [['PUT', 0, 'a', 10, 5], ['PUT', 0, 'b', 20, 5], ['GET', 1, 'a'], ['GET', 1, 'b'], ['PUT', 1, 'c', 30, 10], ['GET', 2, 'a'], ['GET', 2, 'b'], ['GET', 2, 'c']])

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

Example 2

Input: simulate_ttl_lru_cache(2, [['PUT', 0, 'x', 1, 3], ['PUT', 0, 'y', 2, 3], ['GET', 1, 'x'], ['PUT', 2, 'z', 3, 10], ['GET', 2, 'x'], ['GET', 2, 'y'], ['GET', 2, 'z'], ['GET', 4, 'x']])

Output: [1, 1, -1, 3, -1]

Example 3

Input: simulate_ttl_lru_cache(3, [['PUT', 0, 'a', 5, 10], ['PUT', 0, 'b', 6, 10], ['PUT', 0, 'c', 7, 10], ['DELETE', 1, 'b'], ['GET', 1, 'b'], ['GET', 1, 'a'], ['PUT', 2, 'd', 8, 10], ['GET', 2, 'a'], ['GET', 2, 'c'], ['GET', 2, 'd']])

Output: [-1, 5, 5, 7, 8]

Hints

Hint 1

Use an OrderedDict (or equivalent doubly-linked structure) to track LRU order; move a key to the end on every successful GET or PUT.

Hint 2

Before any PUT that would exceed capacity, first sweep out all expired entries (by current time), then evict the front (LRU) of the OrderedDict if still needed.

Hint 3

Store (value, insert_time, ttl) per key so you can check liveness as `insert_time <= t < insert_time + ttl` at any query time.