Least Frequently Used (LFU) Cache
ByteDanceSoftware Engineer · OnsiteLast updated
This problem tests the ability to design an O(1) time complexity cache that evicts the least frequently used items, using recency as a tie-breaker.
Problem Statement
Design and implement a Least Frequently Used (LFU) cache class LFUCache.
Requirements
LFUCache(int capacity): Initializes the object with thecapacityof the data structure.int get(int key): Gets the value of thekeyif the key exists in the cache, otherwise returns -1. Accessing a key increases its frequency count.void put(int key, int value): Updates the value of thekeyif it exists, or inserts it if it is not already present. When the cache reachescapacityand an insertion is required, evict the key with the lowest frequency. If multiple keys have the same lowest frequency, evict the least recently used (LRU) one among them.
Constraints
0 <= capacity <= 10^50 <= key <= 10^90 <= value <= 10^9getandputmust run inO(1)average time complexity.
Examples
# Example usage:
lfu = LFUCache(2)
lfu.put(1, 1)
lfu.put(2, 2)
lfu.get(1) # returns 1, cache is {1: 1, 2: 2}, freq 1: {2}, freq 2: {1}
lfu.put(3, 3) # evicts key 2, cache is {1: 1, 3: 3}
lfu.get(2) # returns -1
lfu.get(3) # returns 3, cache is {1: 1, 3: 3}, freq 1: {}, freq 2: {1, 3}
Hints
Hint 1
Use a hash map to store the key-value pairs and another to track the frequency of each key.
Hint 2
Maintain a map where the key is the frequency count and the value is a doubly linked list of keys that share that frequency.
Hint 3
Keep track of the minimum frequency currently in the cache to quickly identify which frequency list to evict from.
Follow-up questions
- How would you modify this design if you needed to support TTL (Time-To-Live) expiration for each entry?
- What are the trade-offs of using an LRU cache versus an LFU cache in a high-traffic production system?
- How would you make this implementation thread-safe in a multi-threaded environment?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.