Tutorialspoint
Problem
Solution
Submissions

LFU Cache Implementation

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

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

  • LFUCache(int capacity): Initializes the LFU Cache with positive size capacity.
  • Get(int key): Returns the value of the key if present, otherwise returns -1.
  • Put(int key, int value): Updates the value of the key if present, or inserts the key if not present. When the cache reaches its capacity, it should invalidate the least frequently used item. If there is a tie (i.e., two or more keys with the same frequency), the least recently used key should be invalidated.
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 cache = new LFUCache(2); // Cache capacity is 2
    • cache.put(1, 1);
    • cache.put(2, 2);
    • cache.get(1); // returns 1
    • cache.put(3, 3); // evicts key 2
    • cache.get(2); // returns -1 (not found)
    • cache.get(3); // returns 3
    • cache.put(4, 4); // evicts key 1
    • cache.get(1); // returns -1 (not found)
    • cache.get(3); // returns 3
    • cache.get(4); // returns 4
Example 2
  • Input:
    ["LFUCache", "put", "put", "put", "get", "get"]
    [[2], [1, 1], [2, 2], [3, 3], [2], [1]]
  • Output:
    [null, null, null, null, 2, -1]
  • Explanation:
    • LFUCache cache = new LFUCache(2); // Cache capacity is 2
    • cache.put(1, 1);
    • cache.put(2, 2);
    • cache.put(3, 3); // evicts key 1
    • cache.get(2); // returns 2
    • cache.get(1); // returns -1 (not found)
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)
Linked ListHash MapWiproAirbnb
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 and linked lists
  • Track both frequency and recency for each key
  • Organize keys by frequency and keep them in order of recency within each frequency
  • Implement efficient lookup and update mechanisms
  • Handle edge cases like capacity of 0 and updates to existing keys

Steps to solve by this approach:

Step 1: Use a dictionary to map keys to nodes for O(1) lookup.
Step 2: Use another dictionary to map frequencies to linked lists of nodes to maintain order.
Step 3: Implement a method to update the frequency when a key is accessed or updated.
Step 4: Implement a method to remove the least frequently used item when the cache is full.
Step 5: Track the minimum frequency to quickly find the least frequently used item.



Submitted Code :