ShareChat Coding Question – Solved

4 Live
Question 2 Implement a Least Frequently Used (LFU) cache data structure of size cacheSize that handles two types of queries: GET and PUT. A GET query attempts to retrieve the value of a given key. If the key is present in the cache, it is returned. Otherwise, it returns -1. A PUT query updates or inserts a key-value pair into the cache. When the cache is full, the least frequently used key is removed to accommodate the new key-value pair. If there is a tie in the frequency of keys, then the least recently used key is removed. Return an array of integers where each i-th element is the answer for the i-th GET query. Example Suppose cacheSize = 1 q = 5 queries = ["PUT 1 1", "PUT 2 2", "GET 1"] Only cacheSize = 1 element is stored in the cache.

Asked in: ShareChat

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |