Implement Cache and Key-Value Store
Company: Lyft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design efficient in-memory data structures and caching mechanisms, focusing on core key-value operations, eviction behavior, and performance constraints.
Part 1: Design an LRU Bounded Cache
Constraints
- 0 <= capacity <= 100000
- 0 <= len(operations) <= 200000
- Each operation is either ('put', key, value) or ('get', key)
- -10^9 <= key, value <= 10^9
Examples
Input: (2, [('put', 1, 10), ('put', 2, 20), ('get', 1), ('put', 3, 30), ('get', 2), ('put', 1, 15), ('get', 1), ('get', 3)])
Expected Output: [10, -1, 15, 30]
Explanation: After get(1), key 1 becomes most recent, so putting key 3 evicts key 2. Updating key 1 changes its value to 15 and keeps it recent.
Input: (0, [('put', 1, 1), ('get', 1), ('put', 2, 2), ('get', 2)])
Expected Output: [-1, -1]
Explanation: Edge case: capacity is 0, so the cache can never store anything.
Input: (1, [('put', 5, 50), ('put', 5, 55), ('get', 5), ('put', 6, 60), ('get', 5), ('get', 6)])