KatInterview

Replay a Sequence of Recency-Evicting Cache Commands

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designlru-cachehash-maplinked-list

Last updated

Simulate a fixed-capacity cache that evicts the least recently used entry, replaying a list of get/put commands and returning each command's result.

Replay a Recency-Evicting Cache

You are given the size limit of a small key-value store and a list of commands to run against it, in order. The store keeps at most capacity entries. When it is full and a brand-new key arrives, it throws out the entry that was used least recently.

Implement a function that runs all the commands on an initially empty store and reports the outcome of each one.

Commands

Each command is named in commands, and the entry at the same index in params holds its integer parameters.

  • "get" with params = [key]: Look up key.
    • If it is stored, the result is its value, and the key now counts as the most recently used.
    • If it is not stored, the result is -1 and nothing changes.
  • "put" with params = [key, value]: Save value under key.
    • If key is already stored, replace its value and mark it as the most recently used. Nothing is evicted.
    • If key is new and the store already holds capacity entries, first remove the least recently used entry, then insert the new key as the most recently used.
    • If key is new and there is room, simply insert it as the most recently used.
    • The result of a put is None.

An entry's "use" is any successful get or any put on its key.

Output

Return a list with one element per command, in order: the retrieved value (or -1) for each get, and None for each put.

Constraints

  • 1 <= capacity <= 100000
  • commands and params have the same length.
  • Every command is either "get" or "put", and its params list has the matching number of integers.
  • Each command should run in O(1) average time.
  • A failed get (missing key) does not affect recency of any entry.

Examples

Example 1

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

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

Example 2

Input: replay_cache_commands(2, ['put', 'put', 'put', 'get', 'get', 'get'], [[1, 10], [2, 20], [1, 15], [1], [2], [3]])

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

Example 3

Input: replay_cache_commands(1, ['get', 'put', 'get', 'put', 'get'], [[7], [7, 70], [7], [8, 80], [7]])

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

Hints

Hint 1

You need constant-time lookup by key and constant-time reordering by recency. Think about combining a hash map with an ordering structure that lets you move any item to the 'newest' end cheaply.

Hint 2

Keep entries ordered from least to most recently used. A successful get, or a put on an existing key, moves that key to the newest end; eviction removes from the oldest end.

Hint 3

Only evict when inserting a key that is not already present and the store is already at capacity. Updating an existing key must never trigger an eviction. In Python, collections.OrderedDict gives you move-to-end and pop-oldest in O(1).