Implement an LruCache class with a fixed capacity passed to the constructor. The get method returns the value for a key or null, the put method stores a key-value pair, and when the cache overflows, the element that hasn't been accessed for the longest time is evicted. Both operations must run in constant time.
~15 min by voice
How to approach it
This checks whether you can build a data structure from two primitives to get O(1) on both operations, a hash table for lookup and a doubly linked list for usage order, and carefully handle the edge cases: updating an existing key, eviction without an off-by-one error, a missing key.
The interviewer will ask this question and listen. Think out loud and ask for a hint if you need one. When you're done, pause or tap “That's my answer”. In about a minute, your score and the points you covered show up here.