KatInterview

Simulate a Frequency-Eviction Cache (LFU Cache)

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdata structurescachesimulation

Last updated

Tests ability to simulate a Least-Frequently-Used cache by processing a sequence of get/put operations and returning the complete history of return values.

Frequency-Eviction Cache Simulator

You are building a cache that evicts the least-frequently-used entry when it is full and a new key must be inserted.

Cache Rules

  1. put(key, value) — Insert or update the entry for key.

    • If key already exists, update its value and increment its access frequency.
    • If key does not exist and the cache is not full, insert it with frequency 1.
    • If key does not exist and the cache is full, first evict the entry with the lowest frequency. Ties in frequency are broken by evicting the least-recently-used (i.e., the entry whose last access time is the oldest). Then insert the new entry with frequency 1.
    • put always returns None (represented as -1 in the output list).
  2. get(key) — Retrieve the value for key.

    • If key exists in the cache, return its value and increment its access frequency.
    • If key does not exist, return -1.

Access time

Every get and put operation advances a global logical clock by 1, starting from 0. The operation at index i in the list has timestamp i. This timestamp is used to break eviction ties.

Your task

Implement the function simulate_lfu_cache which accepts:

  • capacity — maximum number of entries the cache can hold (always ≥ 1).
  • operations — a list of [op, key] or [op, key, value] entries where op is either "get" or "put".

Return a list of integers: the return value of each operation in order. put operations always contribute -1 to the output.

Constraints

  • 1 ≤ capacity ≤ 500
  • 1 ≤ len(operations) ≤ 2000
  • Each operation is ["get", key] or ["put", key, value]
  • 0 ≤ key ≤ 1000
  • 0 ≤ value ≤ 10000
  • An operation list may contain only gets, only puts, or any mix.
  • If a key is evicted and later re-inserted, its frequency resets to 1.

Examples

Example 1

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

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

Example 2

Input: simulate_lfu_cache(1, [['put', 5, 100], ['get', 5], ['put', 6, 200], ['get', 5], ['get', 6]])

Output: [-1, 100, -1, -1, 200]

Example 3

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

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

Hints

Hint 1

Track each cached entry's frequency and last-access timestamp separately — a dict keyed by cache key works well.

Hint 2

For eviction, you need the entry with the minimum frequency; among ties, the one with the smallest timestamp. A simple linear scan over the cache entries is O(capacity) per eviction and is fast enough given the constraints.

Hint 3

Remember that both get and put count as accesses: after a put on an existing key, increment frequency and update the timestamp, just as you would for a get.