KatInterview

Simulate a Fixed-Size Cache Under LRU or LFU Eviction

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapordered-dictlru

Last updated

Replay a list of get/put operations on a bounded cache that evicts by recency or by frequency (ties broken by recency), and return the result of each operation.

Simulate a Bounded Cache

A cache holds at most capacity integer key/value pairs. When a new key arrives and the cache is already full, one existing entry must be removed first. Which entry goes depends on the policy, which is either "LRU" or "LFU".

You are given a policy name and a list of operations to run in order. Return what each operation produces.

Operations

Operation i is operations[i] with its parameters in arguments[i].

  • "get" with arguments[i] = [key]: if the key is stored, return its value and count the lookup as a use of that key. If the key is absent, return -1 and change nothing.
  • "put" with arguments[i] = [key, value]: store the value under the key and return None.
    • If the key is already stored, overwrite its value. This counts as a use of the key. Nothing is evicted.
    • If the key is new and the cache is full, evict one entry first, then insert the new key. The new key counts as having been used once, and it is the most recently used key.
    • If the key is new and the cache has room, simply insert it with the same starting state.

Eviction rules

  • LRU (least recently used): evict the key whose most recent use is the oldest. Insertions, successful get calls, and overwriting put calls all count as uses.
  • LFU (least frequently used): each key has a use count. It starts at 1 on insertion and goes up by 1 for every successful get and every overwriting put. Evict the key with the smallest count. If several keys share that smallest count, evict the one among them whose most recent use is the oldest. A key that is evicted loses its count; if it is inserted again later it starts over at 1.

Always evict before inserting, so the incoming key can never be the one removed.

Output

Return a list with one entry per operation, in the same order: the retrieved value (or -1) for each get, and None for each put.

Constraints

  • capacity is a positive integer.
  • policy is either "LRU" or "LFU".
  • operations and arguments have the same length, and every operation is "get" or "put".
  • Keys and values are integers.
  • A get on a key that is not currently stored returns -1 and does not affect any usage information.
  • An efficient solution should handle each operation in O(1) average time.

Examples

Example 1

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

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

Example 2

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

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

Hints

Hint 1

Keep the values in an ordinary dictionary. Only the choice of eviction victim depends on the policy, so track that separately.

Hint 2

For LRU, an OrderedDict lets you move a key to the end on every use and pop the oldest key from the front, both in constant time.

Hint 3

For LFU, group keys into buckets by use count, with each bucket ordered by recency, and track the smallest count that has a non-empty bucket. Reset that minimum to 1 whenever a brand new key is inserted, and remember to evict before inserting.