Design LRU Cache
Requirements
- put(key, value) -- insert key-value pair; evict least recently used entry at capacity
- get(key) -- retrieve value and promote to most recently used; return -1 if absent
- Fixed capacity set at initialization
- Thread-safe concurrent access
- O(1) time complexity for both get and put operations
Class Diagram

Classes, Interfaces and Enumerations
| Class/Interface |
Description |
| Node |
Doubly linked list node: key, value, prev, next pointers |
| LRUCache |
HashMap + doubly linked list with sentinel head/tail nodes; synchronized get/put for thread safety |
Helper methods within LRUCache:
- addToHead -- insert node right after the head sentinel
- removeNode -- detach a node from the list
- moveToHead -- remove then re-insert at head (mark as most recently used)
- removeTail -- evict the least recently used node (before tail sentinel)
Design Patterns Used
| Pattern |
Application |
| HashMap + Doubly Linked List |
HashMap provides O(1) lookup; doubly linked list maintains access order for O(1) eviction |
| Sentinel Nodes |
Dummy head and tail nodes simplify edge-case handling for insertions and deletions |
Code Implementations