This question evaluates understanding of data structures and cache eviction policies, specifically frequency tracking for LFU caches and LRU tie-breaking among equal-frequency items.
Design and implement a Least Frequently Used (LFU) cache with a fixed capacity.
The cache supports the following operations:
get(key) -> value
key
if it exists in the cache.
-1
.
put(key, value) -> void
key
.
capacity == 0
,
put
does nothing and
get
always returns
-1
.
If capacity = 2:
put(1, 1)
put(2, 2)
get(1)
returns
1
(freq(1)=2)
put(3, 3)
evicts key
2
(freq(2)=1 is lowest)
get(2)
returns
-1
get(3)
returns
3