KatInterview

Replay Operations on a Bounded Recency-Evicting Store

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designhash-maplinked-listordered-dict

Last updated

Simulate a fixed-size key-value store that evicts the stalest entry, replaying a list of get/put commands and returning each command's result.

Replaying Commands on a Bounded Store

You are given a store that holds at most capacity integer key-value pairs. When it runs out of room, it discards the entry that was touched longest ago. Instead of building a class, you must write a function that replays a whole list of commands against one fresh, initially empty store and reports what each command returned.

Commands

Each command is one of two kinds:

  • "get" with arguments [key]: look up key.
    • If the key is present, return its value and treat the key as just used.
    • If the key is absent, return -1. This changes nothing in the store.
  • "put" with arguments [key, value]: store value under key.
    • If the key is already present, replace its value and treat the key as just used. Nothing is evicted.
    • If the key is new and the store already holds capacity entries, first remove the least recently used entry, then insert the new key as just used.
    • If the key is new and there is room, simply insert it as just used.
    • A put returns None.

"Recently used" means the most recent successful get or any put on that key. A get that misses does not count as using anything.

Input

  • capacity: the maximum number of entries the store may hold.
  • operations: a list of strings, each "get" or "put".
  • arguments: a list of the same length as operations; arguments[i] is [key] for a get and [key, value] for a put.

Output

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

Each command should run in O(1) average time.

Constraints

  • capacity >= 1
  • len(operations) == len(arguments)
  • Keys and values are integers.
  • Every operations[i] is either "get" or "put", and its arguments[i] has the matching length.
  • A stored value of -1 is indistinguishable from a miss; this is expected.
  • Each operation should take O(1) average time.

Examples

Example 1

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

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

Example 2

Input: replay_bounded_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 3

Input: replay_bounded_store(1, ['put', 'get', 'put', 'get'], [[10, 7], [10], [20, 8], [10]])

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

Hints

Hint 1

You need two abilities at once: find an entry by key instantly, and know which entry is the stalest instantly. Think about which structures give each.

Hint 2

Keep entries ordered from least to most recently used. Every successful get and every put must move that key to the 'most recent' end, and eviction removes from the other end.

Hint 3

Be careful with a put on a key that already exists while the store is full: it must refresh the key and overwrite the value without evicting anything. Python's OrderedDict (pop then reinsert, popitem(last=False)) or a hash map plus doubly linked list both work.