The LRU (Least Recently Used) Cache is an iconic design problem. You must build a data structure with a specific capacity. It must support get(key) to retrieve a value, and put(key, value) to add or update a value. The critical rule is that if a put operation exceeds the capacity, the cache must evict the least recently used item. Both get and put operations must execute in average time complexity.
This is arguably the most famous system design coding question. It is universally asked because it perfectly tests a candidate's ability to combine two disparate data structures to achieve a time complexity that neither could achieve alone. It acts as a benchmark for your understanding of memory management, pointer manipulation, and dictionary operations.
The only way to achieve time for all operations is by combining a Hash Map with a Doubly-Linked List.
Initialize LRUCache(capacity=2).
put(1, 1): Map: {1: Node(1)}. List: [1].put(2, 2): Map: {1: Node(1), 2: Node(2)}. List: [2, 1] (2 is most recent).get(1): Returns 1. Node(1) becomes most recent. List becomes: [1, 2].put(3, 3): Over capacity! Evict least recent (tail of list, which is 2). Remove 2 from Map and List. Insert 3. Map: {1: Node(1), 3: Node(3)}. List: [3, 1].get(2): Returns -1 (not found).The most disastrous mistake is trying to use an Array or standard List instead of a Doubly-Linked List. Shifting elements in an array when you remove an item takes time, which immediately fails the prompt's requirement. Another common error is forgetting to remove the evicted node's key from the Hash Map, causing memory leaks and false positives on future lookups.
When building the LRU Cache in an interview, do not hesitate to write helper functions for your Doubly-Linked List! Write explicit addNodeToHead(node) and removeNode(node) methods before you write the main get and put logic. Using dummy head and tail nodes makes these helper functions completely immune to null-pointer exceptions, resulting in incredibly clean, bug-free code.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Authentication Manager | Medium | Solve | |
| LFU Cache | Hard | Solve | |
| All O`one Data Structure | Hard | Solve | |
| All O`one Data Structure | Hard | Solve | |
| Design Twitter | Medium | Solve |