Loading problem…
You're building a browser cache system that stores frequently accessed resources (images, API responses, etc.). To manage memory efficiently, you need to implement a Least Recently Used (LRU) cache that automatically evicts the least recently used item when the cache reaches its capacity limit.
Design and implement a data structure for a Least Recently Used (LRU) cache. It should support the following operations:
get(key): Return the value of the key if it exists, otherwise return -1put(key, value): Insert or update the value. If the cache exceeds its capacity, evict the least recently used itemget(key): Get value by key, return -1 if not foundput(key, value): Insert or update key-value pairThis problem models real browser caching systems where:
Example 1:
Example 2: