Tutorialspoint
Problem
Solution
Submissions

LFU Cache

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Design and implement a data structure for a Least Frequently Used (LFU) cache. The LFU cache should support the following operations: get and put.

- get(key): Get the value of the key if it exists in the cache, otherwise return -1.
- put(key, value): Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be evicted.

The cache must be implemented with a time complexity of O(1) for both operations.

Example 1
  • Input: ["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
    [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
  • Output: [null, null, null, 1, null, -1, 3, null, -1, 3, 4]
  • Explanation:
    LFUCache lfu = new LFUCache(2); // Cache capacity is 2
    lfu.put(1, 1); // cache is {1=1}
    lfu.put(2, 2); // cache is {1=1, 2=2}
    lfu.get(1); // return 1, cache is now {1=1, 2=2} with frequency of key 1 increased
    lfu.put(3, 3); // key 2 is evicted since it has lower frequency, cache is {1=1, 3=3}
    lfu.get(2); // returns -1 (not found)
    lfu.get(3); // returns 3, cache is {1=1, 3=3} with frequency of key 3 increased
    lfu.put(4, 4); // key 1 is evicted since both keys have same frequency but 1 is less recently used, cache is {4=4, 3=3}
    lfu.get(1); // returns -1 (not found)
    lfu.get(3); // returns 3, cache is {4=4, 3=3} with frequency of key 3 increased
    lfu.get(4); // returns 4, cache is {4=4, 3=3} with frequency of key 4 increased

Example 2
  • Input: ["LFUCache", "put", "put", "put", "get", "get"]
    [[2], [1, 1], [2, 2], [1, 3], [1], [2]]
  • Output: [null, null, null, null, 3, 2]
  • Explanation:
    LFUCache lfu = new LFUCache(2); // Cache capacity is 2
    lfu.put(1, 1); // cache is {1=1}
    lfu.put(2, 2); // cache is {1=1, 2=2}
    lfu.put(1, 3); // update the value of key 1, cache is {1=3, 2=2}
    lfu.get(1); // returns 3
    lfu.get(2); // returns 2
Constraints
  • 0 <= capacity <= 10^4
  • 0 <= key <= 10^5
  • 0 <= value <= 10^9
  • At most 2 * 10^5 calls will be made to get and put
  • Time Complexity: O(1) for both get and put operations
  • Space Complexity: O(capacity)
Hash MapCapgeminiShopify
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a HashMap to store the key-value pairs for O(1) lookup
  • Use a second HashMap to keep track of the frequency of each key
  • Group keys with the same frequency together using a HashMap of LinkedHashSets
  • Use a LinkedHashSet for each frequency to maintain the order of insertion (for tie-breaking)
  • Keep track of the minimum frequency for easy removal when the cache is full
  • Update the frequency of a key whenever it's accessed or updated
  • When evicting, remove the least recently used key with the minimum frequency

Steps to solve by this approach:

 Step 1: Initialize three main data structures: a HashMap to store key-value pairs, a HashMap to track frequency for each key, and a HashMap of LinkedHashSets to group keys by frequency.

 Step 2: For the get operation, retrieve the value if the key exists, update its frequency, and maintain the data structures.
 Step 3: When updating frequency, remove the key from its current frequency group and add it to the next frequency group.
 Step 4: Keep track of the minimum frequency for efficient eviction when the cache is full.
 Step 5: For the put operation, if the key exists, update its value and frequency; otherwise, add the new key-value pair.
 Step 6: If the cache is full during put, evict the least recently used key with the minimum frequency.
 Step 7: After eviction or for a new key, set its frequency to 1 and update the minimum frequency.

Submitted Code :