LFU Cache

Question

LFU (Least Frequently Used) is a famous cache eviction algorithm.

For a cache with capacity k, if the cache is full and need to evict a key in it, the key with the least frequently used will be kicked out.

Implement set and get method for LFU cache.

Have you met this question in a real interview? Yes

Example

Given capacity=3

set(2,2)
set(1,1)
get(2)
>> 2
get(1)
>> 1
get(2)
>> 2
set(3,3)
set(4,4)
get(3)
>> -1
get(2)
>> 2
get(1)
>> 1
get(4)
>> 4

Analysis

Solution

Reference

Last updated

Was this helpful?