Build a cache that evicts the least frequently used key when full. If or more keys share the lowest frequency, evict the least recently used among them. Amazon asks cache design problems to test frequency and recency management.
get(key) returns the value if the key exists, or otherwise. put(key, value) inserts or updates, evicting first if full.
With capacity = 2: after put(1,1) and put(2,2), get(1) returns and bumps key 's frequency to . Now put(3,3) evicts key (frequency ). get(2) returns .
Both operations must run in time. What data structures let you track frequencies and recency together?