KatInterview

Simulate a Bounded Cache That Forgets the Least-Used Entry

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-maplinked-listcache

Last updated

Tests O(1) design of a fixed-size recency-ordered cache: correct eviction order, recency refresh on reads and writes, and update-without-evict semantics.

You are building a cache with a fixed number of slots. When the cache is full and a new key arrives, the entry that was used least recently is discarded to make room. Both reading and writing count as "use."

Implement this behavior by processing a sequence of operations and returning the results of every read.

Function

def simulate_lru_cache(capacity: int, operations: list[str], arguments: list[list[int]]) -> list[int]:

Parameters

  • capacity — the maximum number of key-value pairs the cache may hold at one time.
  • operations — a list of strings, each either "get" or "put".
  • arguments — parallel list of argument lists:
    • A "get" entry contains one integer: [key].
    • A "put" entry contains two integers: [key, value].

Operation Rules

"get" with [key]

  • If key exists in the cache, record its value as output and promote it to most-recently-used.
  • If key is absent, record -1 and leave the cache exactly as it is.

"put" with [key, value]

  • If key is already cached, overwrite its value and promote it to most-recently-used. The cache size does not change and nothing is evicted.
  • If key is new and the cache is at capacity, first remove the least-recently-used entry, then insert the new key-value pair as most-recently-used.
  • If key is new and the cache is not yet full, insert the new pair as most-recently-used directly.

Return Value

A list of integers containing the result of every "get" operation, in the order the gets appear in operations.

Constraints

  • 1 <= capacity <= 10^4
  • 1 <= len(operations) == len(arguments) <= 2 × 10^5
  • Every string in operations is "get" or "put".
  • 0 <= key <= 10^9
  • 0 <= value <= 10^9
  • 0 is a valid value; absence is always indicated by -1, never by 0.

Examples

Example 1

Input: simulate_lru_cache(2, ['put', 'put', 'get', 'put', 'get', 'get', 'put', 'get', 'get'], [[1, 10], [2, 20], [1], [3, 30], [2], [3], [1, 11], [1], [3]])

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

Example 2

Input: simulate_lru_cache(2, ['put', 'put', 'put', 'get', 'put', 'get', 'get'], [[4, 1], [5, 2], [4, 3], [5], [6, 4], [4], [5]])

Output: [2, -1, 2]

Example 3

Input: simulate_lru_cache(1, ['get', 'put', 'put', 'get', 'get'], [[5], [5, 7], [6, 8], [5], [6]])

Output: [-1, -1, 8]

Hints

Hint 1

You need O(1) lookup by key and O(1) promotion to most-recently-used. Think about which standard data structure combines a hash map with an ordered sequence that supports constant-time reordering.

Hint 2

Python's collections.OrderedDict keeps keys in insertion order and provides move_to_end(key) and popitem(last=False) — exactly the two recency operations you need.

Hint 3

Be careful to distinguish an update (key already present) from an insert (key is new). An update must refresh recency but must never trigger eviction, even when the cache is full.