KatInterview

Replay Bounded Cache Commands with Recency Eviction

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

Last updated

Simulate a fixed-capacity key-value store that evicts its stalest entry, replaying a command log in O(1) per step using a hash map and recency ordering.

Bounded Cache Replay

You are given a small in-memory store that holds at most capacity integer key/value pairs. When it is full and a brand-new key arrives, the store throws out the entry that has gone unused for the longest time.

Your job is to replay a log of commands against an initially empty store and report what each command produced.

Commands

Each command is a string in operations, and the entry at the same index in arguments holds its parameters.

  • "get" with arguments[i] = [key]
    • If key is in the store, the result is its stored value, and the key now counts as the most recently used.
    • If key is absent, the result is -1 and the store does not change.
  • "put" with arguments[i] = [key, value]
    • If key is already in the store, overwrite its value and mark it as the most recently used. Nothing is evicted.
    • Otherwise, if the store already holds capacity entries, first remove the least recently used entry, then insert the new pair as the most recently used.
    • The result of a put is None.

An entry is "used" whenever it is read by a successful get or written by a put. A get on a missing key does not count as a use of anything.

Output

Return a list with one element per recognized command, in order: the retrieved value (or -1) for each get, and None for each put. A command whose name is neither "get" nor "put" is skipped and contributes nothing to the output.

Stored values can be 0, so be careful to test for key presence rather than value truthiness.

Constraints

  • 1 <= capacity <= 100,000
  • 0 <= key <= 1,000,000
  • 0 <= value <= 1,000,000,000
  • operations and arguments have the same length.
  • Each command should be processed in O(1) average time.

Examples

Example 1

Input: replay_bounded_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: replay_bounded_cache(1, ['put', 'put', 'get', 'put', 'get'], [[1, 10], [1, 20], [1], [2, 30], [1]])

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

Example 3

Input: replay_bounded_cache(2, ['get', 'put', 'get', 'put', 'put', 'get', 'get'], [[1], [1, 100], [1], [2, 200], [3, 300], [1], [2]])

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

Hints

Hint 1

You need to look up a key instantly and also know which key was touched longest ago. One structure rarely does both well, so consider pairing two.

Hint 2

A hash map from key to a node in a doubly linked list lets you move a node to the 'most recent' end and drop the 'least recent' end in constant time. Dummy head and tail nodes remove edge cases.

Hint 3

Check whether the key already exists before checking whether the store is full: overwriting an existing key must never evict anything. Also, a successful get must refresh recency.