LRU Cache
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class with get(key) returning the value or -1 if not found, and put(key, value) to insert or update. When capacity is exceeded, evict the least recently used key.

30:00

LRU Cache
medium
Topics
Companies

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class with get(key) returning the value or -1 if not found, and put(key, value) to insert or update. When capacity is exceeded, evict the least recently used key.

Example 1:
Input: {"commands":["LRUCache","put","put","get","put","get","put","get","get","get"],"values":[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]}
Output: [null,null,null,1,null,-1,null,-1,3,4]
Constraints:
  • 1capacity30001 \leq \text{capacity} \leq 3000

  • 0key1040 \leq \text{key} \leq 10^4

  • 0value1050 \leq \text{value} \leq 10^5

  • At most 2×1052 \times 10^5 calls will be made to get and put.

Input
arr ={"commands":["LRUCache","put","put","get","put","get","put","get","get","get"],"values":[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]}

LRUCache()

Cache (LRU \u2190 \u2192 MRU) | Capacity: 2

Empty

Cache Map (0/2)

No entries

Initialize with capacity=2

Variables
VariableValue
capacity2
DepthFunction Call
Stack empty
0/10