LFU Cache
Explore how to implement an efficient Least Frequently Used (LFU) Cache data structure in JavaScript. This lesson guides you through designing get and put methods that run in constant time, handling cache capacity and eviction based on usage frequency. Gain practical skills to build custom data structures for specialized problem-solving in coding interviews.
We'll cover the following...
Statement
Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the LFUCache class. Here is how it should be implemented:
LFUCache(capacity): This function initializes the object with the capacity of the data structure.
Get(key): This function gets the value of the key if it exists in the cache. Otherwise, it returns -1.
Put(key, value): This function updates the value of the key if present, or inserts the key if it’s not present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there’s a tie, that is, two or more keys have the same frequency, the least recently used key is invalidated.
To determine the least frequently used key, a UseCounter is maintained for each key in the cache. The key with the smallest UseCounter is the least frequently used key. When a key is first inserted into the cache, its UseCounter is set to 1 (due to the Put() operation). The UseCounter for a key in the cache is incremented and either a Get() or Put() operation is called on it.
The Get and Put functions should both run with an average time complexity of
Constraints:
capacity key value At most
calls will be made to Get() and Put().
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
LFU Cache
What is the output if the cache size is 2 and the following keys and values are given as input?
put →[1, 1]
put → [2, 2]
get → [2]
put → [3, 3]
get → [1]
get → [3]
put → [4, 4]
put → [5, 5]
get → [1]
get → [3]
get → [5]
[NULL, NULL, 2, NULL, 1, 3, NULL, NULL, -1, 3, 5]
[NULL, NULL, 2, NULL, -1, 3, NULL, NULL, -1, 3, 5]
[NULL, NULL, 2, NULL, -1, 3, NULL, NULL, 1, 3, 5]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.
Try it yourself
Implement your solution in lfu_cache.js in the following coding playground. We have provided a useful code template in the other file that you may build on to solve this problem.
// Definition for a Doubly Linked List node// class Node {// constructor(key, val) {// this.key = key;// this.val = val;// this.useCounter = 0;// this.next = null;// this.prev = null;// }// }import Node from './dll_node.js'import DLL from './dll.js'class LFUCache{constructor(capacity){// Write your code here}get(key){// Write your code here}put(key, value){// Write your code here}}export default LFUCache;