An LRU (Least Recently Used) Cache is a data structure that stores a limited number of items and evicts the least recently used item when full.
The trick: use a HashMap + Doubly Linked List:
Both get and put must run in O(1) time. This is one of the most popular interview questions at FAANG companies.
cache.get(1)
1
cache.get(2)
-1
cache.get(3)
3