About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

Design and Implement an LFU Cache

LFU (Least Frequently Used) is one of the cache algorithms (also called cache replacement algorithms or policies). When the cache is full, and a new element needs to be added, this algorithm evicts/purges the least frequently used element to free up room for adding the new one.

Cache has two operations:

  • put(key, value) does not return anything. It inserts or updates (if one is already present) the value for the given key in the cache. If the cache is at capacity and a key-value pair needs to be inserted, the least frequently used key-value gets evicted before adding a new one. If there are multiple LFU keys, the least recently used one of them gets evicted.
  • get(key) returns the value currently associated with the given key in the cache. If there is no value for that key, it returns -1.

Calling any one of these operations for a key increment that key's usage count by one.

Once a key-value pair is evicted from the cache, the key's usage count is reset/forgotten as if it was never in the cache.

Learn how LRU Cache works.

LFU Cache Problem Statement:

Given a capacity and a set of operations to perform on an LFU cache, return results of those operations.

Each operation will be given as either two or three numbers:

  • [0, key, value] means put(key, value), and
  • [1, key] means get(key).


"capacity": 2,
"operations": [
[0, 1, 2],
[0, 3, 6],
[1, 1],
[0, 4, 8],
[1, 3],
[1, 4],
[1, 1]


[2, -1, 8, 2]

The output contains results of all get operations, in order:

  • 2 was the value in the cache corresponding to key 1 at the time when get(1) was called for the first time,
  • -1 was returned because by the time get(3) was called, the cache did not contain a value for key 3 (it was evicted, as the least frequently used one while processing put(4, 8)),
  • 8 was the value for key 4, and
  • 2 was the value for key 1.


  • It is good to implement the cache as a class (or object or struct - depending on the language you use). In function implement_lfu_cache, you would then create one instance of that class/object/struct and call its methods/functions to process the operations.

The structure of the class may look like this:

class LFUCache {
    LFUCache(int capacity) {
        // This is a constructor.
        // Initialize the data structures of the cache.

    int get(int key) {
        // ...

    void put(int key, int value) {
        // ...


  • 0 <= cache capacity <= 104
  • 0 <= a key in the cache <= 105
  • 0 <= a value in the cache <= 109
  • 1 <= number of operations <= 105

Code for LFU Cache:

class LFUCache {


   const static int N = 1e5 + 5;

   int cache_value[N];

   int use_counter[N];

   list::iterator position[N]; // This will store the position of a key in the deque.

   list cache_deque[N]; // This will store all the key in least frequently order for each use_counter value.

   int min_use_count = 1, cache_size = 0;

   int capacity;

   LFUCache(int capacity_) {

       capacity = capacity_;

       memset(cache_value, -1, sizeof(cache_value));


   int get(int key) {

       if (!capacity || cache_value[key] == -1) {

           return -1;



       return cache_value[key];


   /* This function will update the use_counter corresponding to the passed `key` and will

   also make the necessary updates in `cache_deque`.


   void adjust(int key) {

       if (use_counter[key]) {


           if (cache_deque[min_use_count].empty()) {





       if (use_counter[key] == 1) {

           min_use_count = 1;



       position[key] = cache_deque[use_counter[key]].end();



   void put(int key, int value) {

       if (capacity == 0) {



       if (cache_size == capacity && use_counter[key] == 0) {

           // Cache is full and this is a new key.



       cache_value[key] = value;


       if (use_counter[key] == 1) {

           // This key is inserted for the first time.





   This function will purge the Least Frequently Used key from the cache.


   void invalidate() {

       int key = cache_deque[min_use_count].front();

       use_counter[key] = 0;

       cache_value[key] = -1;





vector implement_lfu_cache(int capacity, vector> operations) {

   LFUCache lfu(capacity);

   vector result;

   for (auto operation : operations) {

       if (operation[0] == 0) { // put

           int key = operation[1];

           int value = operation[2];

           lfu.put(key, value);

       } else { // get

           int key = operation[1];




   return result;


We hope that this solution to design and implement an LFU Cache problem will help you level up your Heap and Map coding skills. Companies such as Amazon, Adobe, Citigroup, Microsoft, and Google include Maximum Depth/Height of a Binary Tree interview questions in their tech interviews. 

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare. 

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, FAANG+ instructors, and career coaching to help you nail your next tech interview. 

We offer 17 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

To learn more, register for the FREE webinar.

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Recommended Posts

All Posts