KatInterview

LRU Store with Rolling Mean Over Recent Writes

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designlru-cachehash-mapdeque

Last updated

Simulate a batch of operations on an LRU key-value store that also tracks a rolling mean of the last k written values, with O(1) per operation.

Task

Simulate a single object that keeps two independent pieces of state and report the results of a list of operations.

  1. A bounded LRU store. It holds at most capacity key-value pairs. When a write would make it hold more than capacity keys, the least recently used key is removed.
  2. A rolling mean. It tracks the last k values that were ever written, and can report their mean.

You are given operations and arguments of equal length. Process them in order. operations[i] is one of the following, and arguments[i] holds its arguments.

OperationArgumentsEffectOutput entry
"put"[key, value]Store value under key, mark key as most recently used, and append value to the stream of written values. If the store now holds more than capacity keys, remove the least recently used key.None
"get"[key]If key is present, mark it as most recently used and return its value. Otherwise change nothing.The value, or -1 if the key is absent
"getMovingAverage"[]Compute the mean of the last k values written (or all of them, if fewer than k have been written).A float, or 0.0 if no value has been written yet

Return a list with exactly one output entry per operation, in order.

Rules to be careful about

  • Both put and a successful get count as a "use" of a key.
  • Writing to a key that already exists replaces its value and refreshes its recency. It never causes an eviction by itself.
  • The rolling mean is based on the stream of written values, not on the contents of the store. Every put contributes its value, including overwrites of existing keys. Evicting a key does not remove its value from the stream.
  • Before k values have been written, divide by the number of values written so far, not by k.
  • A get for a missing key does not alter any state.

Function

Implement stream_processor(capacity, k, operations, arguments) returning the list of outputs described above.

Constraints

  • capacity and k are positive integers.
  • operations and arguments have the same length.
  • Every operation name is one of "put", "get", "getMovingAverage".
  • Keys and values are integers.
  • Each operation should run in O(1) time.

Examples

Example 1

Input: stream_processor(2, 3, ['put', 'put', 'getMovingAverage', 'put', 'getMovingAverage', 'put', 'get', 'getMovingAverage'], [[1, 10], [2, 20], [], [3, 30], [], [4, 40], [1], []])

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

Example 2

Input: stream_processor(1, 2, ['getMovingAverage', 'put', 'put', 'get', 'getMovingAverage'], [[], [7, 5], [7, 9], [7], []])

Output: [0, None, None, 9, 7]

Hints

Hint 1

The two features only share the put operation. Keep the key-value store and the window of recent values as completely separate structures.

Hint 2

For the store, you need O(1) lookup plus O(1) reordering and removal of the oldest-used key. A hash map combined with a doubly linked list, or an ordered dictionary, does this.

Hint 3

For the mean, keep a queue of the last k values and a running sum. On each put, add the new value, and if the queue grows beyond k, remove the oldest value and subtract it. Divide by the current queue length, and return 0.0 if it is empty.