Implement an LRU cache
GoogleSoftware Engineer · OnsiteLast updated
This question evaluates competency in data structure design and algorithmic complexity by requiring a bounded key-value cache with O(1) average-time get and put operations.
Design a key-value cache with a fixed capacity. It must support:
get(key): return the value for the key if it exists, otherwise return-1.put(key, value): insert or update the key. If the cache is full, evict the least recently used key before inserting the new one.
Both operations should run in O(1) average time.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.