KatInterview

Replay a Bounded Recency Store from an Operation Log

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapdoubly-linked-listlru-cache

Last updated

Simulate a fixed-size store that discards its stalest entry on overflow, answering lookups in O(1) each by pairing a hash map with a doubly linked list.

Bounded Recency Store Replay

A store holds at most capacity integer key-value pairs. When it is full and a brand-new key arrives, it throws away the entry that was used least recently.

You are given a log of operations to run, in order, on an initially empty store. Return what each operation produces.

Operations

  • "get" with arguments [key]: if key is in the store, return its value and mark that key as the most recently used. Otherwise return -1 and leave the store unchanged.
  • "put" with arguments [key, value]:
    • If key is already present, replace its value and mark it as the most recently used. Nothing is evicted.
    • If key is absent, insert it as the most recently used entry. If the store then holds more than capacity entries, remove the single least recently used entry.
    • A put always produces None.

"Used" means any successful get or any put on that key.

Input

  • capacity: the maximum number of entries the store can hold.
  • operations: a list of strings, each either "get" or "put".
  • arguments: a list of the same length as operations. The entry at index i holds the arguments for operations[i]: [key] for a get, [key, value] for a put.

Output

A list with one element per operation, in the same order: the returned value (or -1) for each get, and None for each put.

Aim for O(1) average time per operation, so the whole replay is linear in the number of operations.

Constraints

  • capacity is a positive integer (at least 1).
  • Keys and values are integers.
  • len(operations) == len(arguments).
  • Re-putting an existing key counts as a use and makes it the most recent.
  • A get on a missing key does not change the store or the recency order.

Examples

Example 1

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

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

Example 3

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

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

Hints

Hint 1

You need two fast capabilities: finding an entry by key, and finding or reordering the least recently used entry. A dictionary gives the first; think about what gives the second.

Hint 2

Keep entries in a doubly linked list ordered by recency, with sentinel nodes at both ends so unlinking and appending never need special cases. Let the dictionary map each key to its node.

Hint 3

Store the key inside each node so that when you evict the oldest node you can also delete it from the dictionary. Only evict when a new key is inserted, never when updating an existing key.