Memory Buffer with Access-Based Replacement

Company: OYO_SDE intern_Oncampus_24june

Difficulty: medium

Problem Statement

A memory buffer is a constrained storage system that maintains key-value associations, utilizing a replacement strategy to control its memory usage. The Least Frequently Used (LFU) buffer eliminates the entry with the lowest access count when the storage limit is exceeded. You are given N , the capacity of the buffer, and Q operations of the following type: 1, key, -1 : Get the value of the key from the cache. If the value does not exist in the cache, return -1 . Get operations do not change the access frequency of the key. 2, key, value : Update the value of the key if present, or insert the key if not already present. When updating an existing key, increment its access frequency by 1. When inserting a new key, set its frequency to 1. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting the new item. If two or more keys have the same frequency, the least recently used key should be removed. For each operation of type 1, pr