KatInterview

Fixed-Capacity Eviction Cache with Recency Tracking

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-maplinked-listdesign

Last updated

Tests whether you can design a cache that evicts the least recently accessed entry in O(1) time by combining a hash map with an ordered structure.

Problem

You need to build a fixed-size cache that always evicts the entry that was least recently touched when space runs out.

Implement a function that drives such a cache through a sequence of operations and returns the results.

def run_recency_cache(capacity: int, operations: List[str], arguments: List[List[int]]) -> List[Optional[int]]

Cache Behaviour

The cache holds at most capacity key-value pairs (both integers). It supports two operations:

get key

  • If key is present, return its value and mark it as the most recently used entry.
  • If key is absent, return -1. The recency order of all other entries stays unchanged.

put key value

  • If key already exists, overwrite its value and mark it as the most recently used entry. The total number of entries does not change, so nothing is evicted.
  • If key is new, insert the pair. If the cache now holds more than capacity entries, immediately evict the entry that was least recently used (i.e. the one untouched for the longest time).

A successful get and any put both count as "using" an entry. A failed get (key not found) does not affect recency.

Function Contract

  • capacity — the maximum number of entries the cache may hold at one time.
  • operations — a list of strings, each either "get" or "put". There is no constructor entry in this list.
  • arguments — parallel list of argument arrays. A "get" entry has one element (the key); a "put" entry has two elements (key, then value).
  • Return a list with one element per operation, in order. A "get" produces its integer result (-1 if missing). A "put" produces None. Any unknown operation name must raise ValueError.

Constraints

  • 1 <= capacity <= 3000
  • 0 <= key <= 10 000
  • 0 <= value <= 100 000
  • At most 2 × 10⁵ calls to get or put in total.
  • Each get / put must run in O(1) average time.

Examples

Example 1

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

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

Example 2

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

Output: [-1, None, 10]

Hints

Hint 1

A plain dict gives O(1) lookup but no ordering. A plain list gives ordering but O(n) lookup. Think about which Python built-in fuses both properties, or how you would fuse them yourself.

Hint 2

When you move a node to the 'most recent' position, you need O(1) access to its neighbours so you can stitch them together. What kind of linked list supports that?

Hint 3

Each node in the list must store its own key, not just its value. Ask yourself: when you evict the tail node, how do you know which map entry to delete?