Design cache with least-recently-used eviction
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates understanding of in-memory key–value cache design, eviction policies, and appropriate data-structure choices to achieve amortized O(1) get and put operations.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
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]
Explanation: Standard LRU example.
Input: (1, [["put","a",10],["put","b",20],["get","a"],["get","b"]])
Expected Output: [-1, 20]
Explanation: Capacity one evicts the previous key.
Hints
- Use an ordered map.
- Move keys to the most-recent position after get or put.