Tutorialspoint
Problem
Solution
Submissions

LFU Cache

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Design and implement a data structure for an LFU (Least Frequently Used) cache.

Operations
  • LFUCache(int capacity): Initialize the LFU cache with a positive capacity.
  • get(int key): Return the value of the key if it exists, otherwise return -1.
  • put(int key, int value):
    Update the value of the key if present, or insert the key-value pair if not.
    When the cache reaches its capacity, it should invalidate and remove the least frequently used key before adding a new item.
    If there is a tie for the least frequently used key, the least recently used key among them should be removed.
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:
    • Step 1: Initialize cache with capacity = 2.
    • Step 2: cache.put(1, 1): cache=[1,_], cnt(1)=1
    • Step 3: cache.put(2, 2): cache=[1,2], cnt(1)=1, cnt(2)=1
    • Step 4: cache.get(1): return 1, cache=[1,2], cnt(1)=2, cnt(2)=1
    • Step 5: cache.put(3, 3): cache=[1,3], cnt(1)=2, cnt(3)=1, 2 is evicted
    • Step 6: cache.get(2): return -1 (not found)
    • Step 7: cache.get(3): return 3, cache=[1,3], cnt(1)=2, cnt(3)=2
    • Step 8: cache.put(4, 4): cache=[4,3], cnt(4)=1, cnt(3)=2, 1 is evicted
    • Step 9: cache.get(1): return -1 (not found)
    • Step 10: cache.get(3): return 3, cache=[4,3], cnt(4)=1, cnt(3)=3
    • Step 11: cache.get(4): return 4, cache=[4,3], cnt(4)=2, cnt(3)=3
Example 2
  • Input: ["LFUCache", "put", "put", "put", "get", "get"] [[2], [1, 10], [2, 20], [3, 30], [1], [2]]
  • Output: [null, null, null, null, -1, 20]
  • Explanation:
    • Step 1: Initialize cache with capacity = 2.
    • Step 2: cache.put(1, 10): cache=[1,_], cnt(1)=1
    • Step 3: cache.put(2, 20): cache=[1,2], cnt(1)=1, cnt(2)=1
    • Step 4: cache.put(3, 30): cache=[3,2], cnt(3)=1, cnt(2)=1, 1 is evicted
    • Step 5: cache.get(1): return -1 (not found)
    • Step 6: cache.get(2): return 20, cache=[3,2], cnt(3)=1, cnt(2)=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 all operations
  • Space Complexity: O(capacity)
Hash MapMapeBaySwiggy
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 combination of hash maps to track frequencies and keys.
  • Maintain a counter for access frequency for each key.
  • Group keys by their access frequency.
  • Keep track of the minimum frequency for efficient eviction.
  • Update frequency counts when keys are accessed.

Steps to solve by this approach:

 Step 1: Create a class with required data structures to track keys, values, frequencies, and min frequency.

 Step 2: Implement the get method to return value and update frequency of the accessed key.
 Step 3: Implement the put method to either update an existing key or add a new key.
 Step 4: For new keys when cache is full, remove the least frequently used key.
 Step 5: When frequencies tie, evict the least recently used key by using the order in the frequency set.
 Step 6: Maintain the minimum frequency pointer for efficient removal.
 Step 7: Update frequency counts after each get or put operation.

Submitted Code :