KatInterview

Build an Expiry-Aware LRU Cache Without Scanning Every Key

Data Structure DesignModel answer

Last updated

Design an in-memory cache that enforces capacity with LRU eviction and per-entry TTL, keeping expiration efficient without iterating all keys.

Expiry-Aware LRU Cache

Build an in-memory cache that enforces three policies simultaneously:

  • Capacity limit — when the cache is full, the least-recently-used entry is evicted to make room.
  • TTL expiration — every entry carries a deadline; once the current monotonic time reaches or passes that deadline, the entry is logically dead and must never be returned to callers.
  • Efficient expiry cleanup — background or lazy cleanup must not scan the entire key set on every cycle.

API contract

put(key, value, ttl_ms)   # insert or overwrite; ttl_ms > 0
get(key) → value | None   # returns None if missing or expired;
                          # a hit refreshes recency but does NOT extend TTL

A successful get moves the key to the most-recently-used position. An overwriting put resets both the value and the TTL deadline and also refreshes recency.

Constraints and edge-case rules

  • Capacity is measured in number of entries. An entry that has expired but has not yet been reclaimed still occupies a slot until cleaned up; it must be counted against capacity only if your chosen design would otherwise return a stale result. State your choice explicitly.
  • Expired entries must never be returned, even if background cleanup has not run yet.
  • Zero or negative TTL on a put is invalid; you may throw or return an error — just state your choice.
  • All public operations must be safe for concurrent access across threads. State what level of consistency you guarantee (linearizable vs. something weaker) and how you achieve it.

Part 1 — Core data structures and read/write behavior

Describe the structures you use to support:

  • O(1) average-case lookup, insertion, and LRU eviction.
  • Separate tracking of recency order and expiration order.
  • Atomic removal from every relevant structure when an entry is evicted or expired.

Walk through what happens during a get that finds an expired key, and during a put that triggers LRU eviction.

Part 2 — Expiration without a full-table scan

Propose a structure — a min-heap, timing wheel, or equivalent — that lets a background thread (or lazy get/put path) reclaim expired entries without iterating every key.

Address the following specifically:

  • How do you detect and discard stale expiry records when a key has been overwritten and its old deadline entry is still queued?
  • What are the memory costs and timing precision trade-offs of your chosen approach?
  • How does the cleanup path interact safely with concurrent reads and writes?

Hints

Hint 1

For O(1) LRU you need two structures working together: a hash map for lookup and a doubly-linked list for recency order. Think about what node the map should store a reference to, and why that makes moving a node to the front cheap.

Hint 2

A min-heap ordered by deadline lets you check only the front element to know whether anything has expired yet. The tricky part is overwritten keys: the heap may still hold the old deadline. Store a version counter or the expected deadline alongside each heap entry so you can skip records that no longer match the live entry.

Hint 3

Locking the entire cache on every operation is correct but contention-heavy. Consider whether you can separate the lock on the recency list from other state, or use a read-write lock for gets vs. puts, and be ready to explain the trade-offs of each.