LRU Cache implementation
Problem Statement
Design and implement a Least Recently Used (LRU) Cache — a data structure that stores a limited number of key-value pairs and automatically evicts the least recently used item when the cache reaches its capacity.
This is a common system design building block used in databases, browsers, and distributed systems to improve performance and reduce expensive data retrieval operations.
🎯 Goal
Design the Low-Level Design (LLD) of an LRU Cache that supports:
✅ Functional Requirements
1. get(key)
2. put(key, value)
3. Eviction Policy
⚙️ Non-Functional Requirements
🧱 Key Classes to Design
ClassResponsibility
LRUCache
Main cache class managing capacity, insertions, and evictions.
Node
Represents a doubly linked list node storing a key-value pair.
DoublyLinkedList
Maintains access order — most recently used items at the front.
📐 Suggested Class Design
1
2
3LRUCache
4 ├── capacity: int
5 ├── map: HashMap<Key, Node>
6 ├── dll: DoublyLinkedList
7 ├── get(key)
8 ├── put(key, value)
9
10DoublyLinkedList
11 ├── head, tail (dummy nodes)
12 ├── addToFront(Node)
13 ├── removeNode(Node)
14 ├── removeLast()
15 ├── moveToFront(Node)
16
17Node
18 ├── key, value
19 ├── prev, next🔄 Example Flow
Capacity = 2
OperationCache State (Most → Least Recent)Output
put(1, A)
[1:A]
put(2, B)
[2:B, 1:A]
get(1)
[1:A, 2:B]
A
put(3, C)
[3:C, 1:A] (evicts 2:B)
get(2)
[3:C, 1:A]
-1 (not found)
get(3)
[3:C, 1:A]
C
⚡ Constraints
🧩 Design Hints
🚀 Optional Extensions
✅ Final Summary:
Design an LRU Cache (LLD) that efficiently stores key-value pairs with: