KatInterview

Replay a Log on a Budgeted Recency Cache

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designlru-cacheordered-dicthash-map

Last updated

Simulate a least-recently-used cache whose budget is the sum of per-entry sizes, replaying an operation log and returning each result, using hashing plus recency ordering.

Budgeted Recency Cache Simulator

A cache stores entries, each identified by an integer key and carrying an integer value and a positive integer size. Unlike a normal LRU cache, which limits how many entries it holds, this cache limits the sum of the sizes of all entries it holds. An entry of size 3 uses 3 units of the budget.

You are given a log of operations to replay, in order, on one such cache. Return the result of every operation.

Operations

Each operation is a string, paired with a list of integer arguments at the same position in arguments:

  • "WeightedLRUCache" with [budget]: creates a new, empty cache whose total size may never exceed budget. Any previous contents are discarded. The result is None.
  • "get" with [key]: if the key is stored, the result is its value and the key becomes the most recently used. If it is not stored, the result is -1 and nothing changes, including recency.
  • "put" with [key, value, size]: the result is None. It works as follows:
    1. If the key is already stored, replace its value and size with the new ones. Otherwise, add it as a new entry.
    2. In both cases, the key becomes the most recently used.
    3. Then, while the total size of all stored entries is greater than the budget, remove the least recently used entry.

A removed key is completely gone. A later get on it gives -1, and a later put adds it as a brand-new entry.

Task

Implement weighted_lru_cache(operations, arguments), which processes the log in order and returns a list with one result per operation: None for "WeightedLRUCache" and "put", and the retrieved value or -1 for "get".

Because the key being written is made most recently used before any removal happens, it is always the last candidate for removal.

Constraints

  • 1 <= budget <= 10^9
  • 1 <= size <= budget for every put
  • 0 <= key, value <= 10^9
  • operations and arguments have the same length.
  • At most 2 * 10^5 get and put operations in total.
  • The first operation is always "WeightedLRUCache".
  • Aim for amortized O(1) time per operation.

Examples

Example 1

Input: weighted_lru_cache(['WeightedLRUCache', 'put', 'put', 'get', 'put', 'get', 'get'], [[5], [1, 10, 2], [2, 20, 2], [1], [3, 30, 3], [2], [1]])

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

Example 2

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

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

Hints

Hint 1

You need O(1) lookup by key and O(1) removal of an arbitrary entry plus O(1) removal of the oldest entry. Think about a hash map combined with a recency-ordered list, or Python's OrderedDict.

Hint 2

Keep a running total of sizes instead of recomputing it. When overwriting an existing key, subtract its old size before adding the new one.

Hint 3

Move the written key to the most-recent end before running the eviction loop, and use a while loop since one large put may evict several entries. Since size <= budget, the loop never removes the key just written.