KatInterview

Simulate a Frequency-Based Eviction Cache

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

Last updated

Tests ability to simulate a sequence of get/put/delete operations on an LFU-like cache that evicts the least-frequently-used key on overflow, with tie-breaking by insertion order.

Frequency-Based Eviction Cache

You are building a simple Frequency-Based Eviction Cache (FBEC). The cache has a fixed capacity and supports three operations: get, put, and delete.

Eviction Rule

Whenever a put would exceed the cache's capacity, exactly one existing entry is evicted before the new entry is inserted. The eviction target is chosen as follows:

  1. Among all current entries, find the one(s) with the lowest access frequency (number of times the key has been successfully retrieved via get — put and delete do not count toward frequency).
  2. If there is a tie in frequency, evict the key that was inserted earliest (i.e., the one whose most recent put has the smallest index among tied keys, counting only successful insertions).

Operations

Each operation is a list of two or three elements:

FormatMeaning
["get", key]Return the value associated with key. If the key is not present, return -1. A successful get (key found) increments that key's frequency by 1.
["put", key, value]Insert or update key → value. If key already exists, update its value without changing its frequency or insertion-order rank. If key is new and the cache is at capacity, evict one entry first (see rule above), then insert the new key with frequency 0 and the current insertion rank.
["delete", key]Remove key from the cache. If the key is not present, this is a no-op.

Task

Given the cache capacity and a list of operations, simulate the cache and return a list containing the result of every get operation, in order.

Constraints

  • 1 ≤ capacity ≤ 500
  • 1 ≤ len(operations) ≤ 2000
  • Each operation is ["get", key], ["put", key, value], or ["delete", key]
  • key is always a non-empty string; value is always an integer
  • The cache starts empty
  • A put for a key that already exists (update) does not trigger eviction even if the cache is at capacity
  • After eviction, the newly inserted key has frequency 0 and takes the next insertion rank

Examples

Example 1

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

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

Example 2

Input: simulate_fbec(3, [['put', 'x', 10], ['put', 'y', 20], ['put', 'z', 30], ['get', 'x'], ['get', 'x'], ['get', 'y'], ['put', 'w', 40], ['get', 'z'], ['get', 'w']])

Output: [10, 10, 20, -1, 40]

Example 3

Input: simulate_fbec(1, [['put', 'm', 5], ['get', 'm'], ['put', 'n', 9], ['get', 'm'], ['get', 'n']])

Output: [5, -1, 9]

Hints

Hint 1

Track three things per cache entry: its value, its get-frequency, and a monotonically increasing insertion-rank (a counter you bump on every new/successful insert).

Hint 2

For eviction, scan all current entries and pick the one minimizing (frequency, insertion_rank) — Python's min() with a tuple key does this in one line.

Hint 3

An update put (key already exists) must not reset the frequency or the insertion rank — only the stored value changes.