KatInterview

Run a Command Log on a Recency Cache with Live Mean

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

Last updated

Simulate a fixed-capacity least-recently-used cache from a command log, answering lookups and the mean of stored values, each in expected constant time.

Run a Command Log on a Recency Cache with Live Mean

You are simulating a small in-memory cache that holds at most capacity entries. Each key is a string and each value is a signed number. The cache evicts the entry that was used least recently whenever it grows too large. It can also report the mean of the values it currently holds.

You are given the cache's capacity and a log of commands. Process the commands in order, starting from an empty cache, and return one result per command.

Commands

operations[i] is the name of the i-th command, and arguments[i] is its list of arguments.

  • "get" with arguments[i] = [key]
    • If key is in the cache, the result is its value, and key becomes the most recently used entry.
    • If key is not in the cache, the result is None. The cache is not changed.
  • "put" with arguments[i] = [key, value]
    • If key is already present, its value is replaced. Otherwise a new entry is added.
    • In both cases key becomes the most recently used entry.
    • If the cache now holds more than capacity entries, the least recently used entry is removed. Only a newly added key can push the cache over capacity, so replacing an existing key never causes an eviction.
    • The result is always None.
  • "get_average" with arguments[i] = []
    • The result is the arithmetic mean (sum divided by count) of the values of the entries currently in the cache. Entries that were evicted or overwritten earlier do not count.
    • If the cache is empty, the result is None.
    • The cache is not changed, and recency is not affected.

Return a list of results, one for each command, in the same order as the log.

Requirements

Every command should run in expected O(1) time, so the whole log is processed in O(n) for n commands. Scanning the entries, or relying on a list whose removal from the middle is linear, will be too slow on large inputs.

Constraints

  • 1 <= capacity, and the capacity is fixed for the whole log.
  • len(operations) == len(arguments).
  • Every command name is one of "get", "put", "get_average".
  • Keys are strings, and values are integers (positive, zero, or negative).
  • A missed get does not alter recency or the average.
  • Updating an existing key replaces its value and refreshes its recency.
  • Averages are computed over the current contents only, as sum / count.

Examples

Example 1

Input: simulate_cache_log(2, ['put', 'put', 'get', 'put', 'get', 'get_average'], [['a', 1], ['b', 2], ['a'], ['c', 3], ['b'], []])

Output: [None, None, 1, None, None, 2]

Example 2

Input: simulate_cache_log(1, ['get_average', 'put', 'get_average', 'put', 'get', 'get_average'], [[], ['x', -4], [], ['x', 6], ['x'], []])

Output: [None, None, -4, None, 6, 6]

Example 3

Input: simulate_cache_log(3, ['put', 'put', 'get_average', 'get', 'get_average'], [['a', 2], ['b', 4], [], ['a'], []])

Output: [None, None, 3, 2, 3]

Hints

Hint 1

Pair a hash map from key to node with a doubly linked list ordered by recency, so you can unlink a node and move it to the 'newest' end without scanning.

Hint 2

Keep a running total of all stored values. Subtract the old value on replacement, add the new value on insertion, and subtract the removed value on eviction. The mean is then total divided by the entry count.

Hint 3

Sentinel head and tail nodes remove most special cases when linking and unlinking. Remember that replacing an existing key must not evict anything, and a missed get or an average query must not touch the order.