Design a Recency-Eviction Cache
Company: Pinduoduo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency with data structures and algorithmic design for implementing a fixed-capacity key-value cache that enforces recency-based eviction while meeting average O(1) get and put performance.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (2, [["put",1,10],["put",2,20],["get",1],["put",3,30],["get",2],["put",4,40],["get",1],["get",3],["get",4]])
Expected Output: [10, -1, -1, 30, 40]
Explanation: Standard LRU example.
Input: (1, [["put",1,1],["put",2,2],["get",1],["get",2]])
Expected Output: [-1, 2]
Explanation: Capacity one evicts previous key.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.