Implement an LRU cache with follow-ups
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of core data structures and algorithms for designing an efficient LRU cache as well as knowledge of concurrency and synchronization for thread-safe access.
Constraints
- capacity is fixed at construction time
- 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: (2, [['put', 2, 1], ['put', 2, 2], ['get', 2], ['put', 1, 1], ['put', 4, 1], ['get', 2]])
Expected Output: [2, -1]
Input: (1, [['get', 9], ['put', 9, 90], ['get', 9]])
Expected Output: [-1, 90]
Hints
- A dictionary plus a doubly-linked order or OrderedDict supports O(1) average operations.