Implement an LRU cache
Company: Disney
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of data structures and algorithmic optimization for constant-time cache operations, with emphasis on cache eviction policies and state management.
Constraints
- 0 <= capacity <= 10^5
- operations length <= 2 * 10^5
- keys and values are integers
Examples
Input: (2, [['put', 1, 1], ['put', 2, 2], ['get', 1], ['put', 3, 3], ['get', 2], ['put', 4, 4], ['get', 1], ['get', 3], ['get', 4]])
Expected Output: [1, -1, -1, 3, 4]
Input: (1, [['put', 5, 10], ['put', 5, 11], ['get', 5], ['put', 6, 12], ['get', 5], ['get', 6]])
Expected Output: [11, -1, 12]
Input: (0, [['put', 1, 1], ['get', 1]])
Expected Output: [-1]
Hints
- Use a hash map plus a recency order structure.
- Updating an existing key also makes it most recently used.