Start learning free

LESSON 333 OF 333 · OBJECT-ORIENTED DESIGN - INTERVIEWS

LFU Cache in JavaScript

Practice O(1)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build LFUCache, a cache that holds a few keys with their values. When it is full, it drops the key used the fewest times.

Implement the class:

  • new LFUCache(capacity) makes an empty cache for at most capacity keys.
  • get(key) returns the value of key, or -1 when key isn't in the cache.
  • put(key, value) stores value under key.
  • uses(key) returns how many uses key has had since it came into the cache.

A call to get that finds the key counts as one use of it. A call that returns -1 changes nothing.

A put on a key in the cache replaces its value. It also counts as one use.

A put on a new key adds it with 1 use. That put is its first use.

When the cache is full, a new key first makes one key leave. The key that leaves has the fewest uses. On a tie, the key whose last use is the oldest leaves.

A key that leaves loses its uses. If it comes back later, it starts again at 1 use.

A call to uses is not a use. It returns 0 for a key that isn't in the cache.

The calls run in order on one new LFUCache. A call to put writes nothing. A call to get or uses writes the number it returns.

 

Example 1:

Input:
new LFUCache(2)
1. put(1, 10)
2. put(2, 20)
3. get(1)
4. put(3, 30)
5. get(2)
6. get(3)
7. uses(1)
8. uses(3)
Output:
10
-1
30
2
2
Explanation:
Call 3 is the second use of key 1. Key 2 has only 1 use.
The cache is full when key 3 comes in.
Key 2 has the fewest uses, so it leaves.
That is why call 5 returns -1.
Key 3 has 2 uses: its put and call 6.

Example 2:

Input:
new LFUCache(3)
1. put(5, 50)
2. put(6, 60)
3. put(7, 70)
4. get(6)
5. get(5)
6. put(7, 77)
7. put(8, 80)
8. get(6)
9. get(7)
10. uses(5)
11. uses(8)
Output:
60
50
-1
77
2
1
Explanation:
Call 6 changes the value of key 7. It adds no key.
After call 6, keys 5, 6 and 7 have 2 uses each.
Key 6 was last used in call 4, the oldest of the three.
So key 6 leaves when key 8 comes in.
Call 9 is the third use of key 7.

Example 3:

Input:
new LFUCache(2)
1. put(4, 40)
2. get(4)
3. get(4)
4. put(9, 90)
5. put(3, 30)
6. uses(9)
7. put(9, 99)
8. uses(9)
9. uses(4)
10. uses(4)
Output:
40
40
0
1
3
3
Explanation:
After call 3, key 4 has 3 uses.
Key 9 came in last, but it has only 1 use.
So key 9 leaves in call 5, and call 6 returns 0.
In call 7, key 9 comes back and key 3 leaves.
Key 9 starts again at 1 use.
A call to uses is not a use. So calls 9 and 10 both return 3.

 

Constraints:

  • capacity is from 1 to 10,000.
  • Each key is a whole number from 0 to 1,000,000,000.
  • Each value is a whole number from 0 to 1,000,000,000.
  • At most 100,000 calls follow the constructor.

Note:

Each call to get, put and uses should run in O(1) time. Looking through every key for the one to drop would take O(n) time, for n keys.

STEP 1 OF 34
    Ctrl + Enter to check

    Lfu Cache in JavaScript

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free