LRU Cache
Problem Statement
Design a data structure that supports the following two operations:
- : Inserts a pair into the data structure.
- : Returns the value associated with the given . If the is not found, returns .
The data structure should maintain a Least Recently Used (LRU) cache. When the cache reaches its capacity, the oldest item should be removed first.
Rules and Constraints
- The operation takes and as input, and returns .
- The operation takes as input and returns the associated if found, or otherwise.
- The cache has a fixed capacity .
- The data structure should use the most efficient time and space complexity.
- The operation should update the LRU order of the keys in the cache.
- The operation should return the value associated with the given if it exists in the cache, without modifying the LRU order.
- If the cache is empty or does not contain the given , should return .
Assumptions
- The and are integers.
- The is a positive integer.
- The input data is valid (i.e., the number of operations will not exceed the capacity).
Metrics for Success
- The data structure should implement the and operations efficiently (i.e., with low time complexity).
- The data structure should maintain the LRU order of the keys in the cache.
- The data structure should use a reasonable amount of space (i.e., linear or sublinear with respect to the input data).
Example
Input: {"capacity":2,"ops":["put","put","get"]}
Output: [null,null,1]