Implement a Capacity-Bounded Cache
Company: Shopify
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design and implement an efficient capacity-bounded in-memory key-value cache, assessing understanding of performance constraints such as average-case constant-time get/put operations and eviction behavior.
Constraints
- get returns -1 when missing
Examples
Input: (2, [('put', 1, 1), ('put', 2, 2), ('get', 1), ('put', 3, 3), ('get', 2), ('get', 3)])
Expected Output: [1, -1, 3]
Explanation: Standard eviction.
Input: (0, [('put', 1, 1), ('get', 1)])
Expected Output: [-1]
Explanation: Zero capacity.
Input: (1, [('put', 1, 1), ('put', 1, 2), ('get', 1)])
Expected Output: [2]
Explanation: Update existing key.
Hints
- Use a hash map plus recency order.