LFU Cache
The cache that evicts what you use least often — and, on ties, least recently. Harder than LRU because you must track frequency and recency together, yet still hit O(1) per operation. Replay a sequence of get/put ops and return the outputs. Solve it in Python or TypeScript, with hidden tests.
The problem
Design an LFU (least-frequently-used) cache with a fixed capacity. On get(key), return the value (and bump its use count) or -1 if absent. On put(key, value), insert/update it; if the cache is full, evict the least-frequently-used key, breaking ties by evicting the least-recently-used among those. You are given a list of ops like ["put",1,1] and ["get",1]; return the list of outputs from every get, in order. Every operation must be O(1).
cap = 2, ops = [["put",1,1],["put",2,2],["get",1],["put",3,3],["get",2],["get",3]][1, -1, 3]- 1 ≤ capacity; a
getand aputof an existing key both count as a use (raise its frequency). - Ties in frequency are broken by recency: evict the least-recently-used key among those with the minimum frequency.
- All operations must be O(1) — track a per-frequency ordered bucket and the current minimum frequency; do not scan.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement lfu_ops(cap, ops): keep value/frequency maps plus, for each frequency, an insertion-ordered bucket of keys. Track min_freq. A use moves a key up one bucket; eviction pops the oldest key from the min_freq bucket. Return the outputs of every get.
- You need two things per key: its value and its frequency. And per frequency, the keys at that frequency in use order (oldest first).
- An ordered dict (Python
OrderedDict) or a Map (JS preserves insertion order) makes each frequency bucket an O(1) LRU queue. - On a use: remove the key from its current frequency bucket, add it to the (freq+1) bucket. If you just emptied the
min_freqbucket, incrementmin_freq. - On eviction (cache full, inserting a new key): pop the oldest key from the
min_freqbucket, then insert the new key at frequency 1 and setmin_freq = 1.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.