KatInterview

Replay a Bounded Recency Store from an Operation Log

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designhash-maplinked-listsimulation

Last updated

Simulate a fixed-size key-value store that discards the least recently touched entry when full, by replaying a log of operations and returning each result.

Bounded Recency Store Replay

A bounded key-value store holds at most capacity entries. When a new key would push it past that limit, the entry that was touched longest ago is thrown away.

You are given a log of commands and their arguments, and you must replay them in order and report what each command produces.

Commands

CommandArgumentsEffectOutput
"LRUCache"[capacity]Starts a brand-new, empty store with the given capacity. Any earlier store and its contents are discarded.None
"put"[key, value]Saves value under key. If key is already present, its value is overwritten. Either way, key becomes the most recently touched entry. If a new key was added and the store now holds more than capacity entries, the least recently touched entry is removed.None
"get"[key]If key is present, returns its value and marks key as the most recently touched. If absent, returns -1 and changes nothing.the value, or -1

Rules

  • An entry is "touched" by a put on its key (new or overwrite) or by a successful get. A get that misses touches nothing.
  • Overwriting an existing key never changes the number of entries, so it never removes anything.
  • A key of 0 and a value of 0 are ordinary valid data.
  • The first command in every log is "LRUCache".

Task

Implement replay_recency_store(commands, arguments). Both lists have the same length, and arguments[i] holds the arguments for commands[i]. Return a list of the same length where position i is the output of command i (None where the table says None).

Constraints

  • 1 <= capacity <= 3000
  • 0 <= key <= 10^4
  • 0 <= value <= 10^5
  • The log contains at most 2 * 10^5 commands.
  • Aim for O(1) average time per get and put.

Examples

Example 1

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

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

Example 2

Input: replay_recency_store(['LRUCache', 'put', 'put', 'get', 'get', 'put', 'get', 'put', 'get', 'get'], [[1], [1, 10], [1, 20], [1], [2], [2, 30], [1], [3, 40], [2], [3]])

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

Hints

Hint 1

Looking up a key is easy with a hash map. The harder part is knowing which entry was touched longest ago, and updating that ordering cheaply every time something is touched.

Hint 2

Keep the entries in an order from least to most recently touched. Touching a key means pulling it out of its current spot and placing it at the 'most recent' end, in constant time. A doubly linked list plus a hash map from key to node does this, and so does an ordered dictionary.

Hint 3

Evict only after adding a brand-new key and only if the size exceeds capacity. Remember that a missed `get` must not change the order, and that a new `LRUCache` command resets everything.