KatInterview

Build a Fixed-Capacity Recency-Ordered Cache

Data Structure DesignAuto-graded · 17 test cases
EasyAuto-gradedData Structure Designhash-maplinked-listcache

Last updated

Tests whether you can design a cache that evicts the least-recently-touched entry in O(1) time using a hash map paired with an ordered structure.

Fixed-Capacity Recency-Ordered Cache

You need to build a cache that holds at most capacity key-value pairs and always discards the entry that was touched least recently when space runs out.

Operations

The cache must support two operations.

put(key, value)

  • If key is already in the cache, overwrite its value and mark it as the most recently used entry.
  • If key is not in the cache, insert the pair.
  • After inserting a brand-new key, if the number of stored entries now exceeds capacity, remove whichever entry was used least recently.

get(key)

  • If key exists, mark it as the most recently used entry and return its value.
  • If key does not exist, return -1 without changing anything.

Both operations must run in O(1) average time.

Simulation Interface

Rather than instantiating a class directly, implement a single function that replays a sequence of operations and collects their return values.

run_recency_cache(capacity, operations, arguments)
  • capacity — maximum number of entries the cache may hold at any moment (positive integer).
  • operations — a list of strings, each either "put" or "get".
  • arguments — a parallel list; for "put" each element is [key, value], for "get" each element is [key].

Return a list with one entry per operation:

  • For "get": the value found, or -1.
  • For "put": None.

Constraints

  • 1 <= capacity <= 3000
  • 0 <= key <= 10^4
  • 0 <= value <= 10^5
  • 1 <= len(operations) <= 3 * 10^4
  • operations[i] is either "get" or "put".
  • arguments[i] has exactly one element for "get" and exactly two for "put".
  • A "get" on a missing key must return -1 and must not alter the recency order of any existing entry.
  • A "put" on an existing key updates the value and refreshes recency but never triggers an eviction, even if the cache was already at capacity.

Examples

Example 1

Input: run_recency_cache(2, ['put', 'put', 'get', 'put', 'get'], [[1, 1], [2, 2], [1], [3, 3], [2]])

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

Example 2

Input: run_recency_cache(1, ['put', 'get', 'put', 'get', 'get'], [[1, 10], [1], [2, 20], [1], [2]])

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

Hints

Hint 1

You need O(1) lookup by key and O(1) reordering by recency at the same time — think about what single Python built-in combines both properties.

Hint 2

Treat the ordered structure as a queue where the front is the least-recently-used end and the back is the most-recently-used end. Every successful access pops and reinserts the touched key so it lands at the back.

Hint 3

When a put arrives for a key that already exists, the cache size does not grow, so no eviction should occur — handle the update path separately before checking the size.