LRU Cache implementation

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-18

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:


•O(1) time complexity for both get() and put() operations.
•Efficient eviction of the least recently used entry when capacity is full.


✅ Functional Requirements


1. get(key)


•Returns the value associated with the key if it exists in the cache.
•Marks the key as most recently used.
•If the key doesn’t exist, return -1 or null.


2. put(key, value)


•Inserts or updates the key-value pair in the cache.
•If the key already exists, update its value and mark it as most recently used.
•If the cache is full, evict the least recently used key before inserting the new one.


3. Eviction Policy


•When capacity is exceeded, the least recently used entry should be removed.
•“Recently used” is determined by the most recent call to get() or put().


⚙️ Non-Functional Requirements


•Must support O(1) get and put operations.
•Must handle concurrency safely (optional enhancement).
•Must be memory efficient.
•Should be easily extensible (e.g., for MRU or LFU variations).


🧱 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


•1 <= capacity <= 10^5
•Keys and values are integers or strings.
•get() and put() should execute in constant time O(1).


🧩 Design Hints


•Use a HashMap for fast lookups.
•Use a Doubly Linked List to track usage order.
•Front (head) → most recently used.
•End (tail) → least recently used.
•On every get() or put(), move accessed node to the front.
•When full, remove node at tail.


🚀 Optional Extensions


•Add thread safety with synchronization or concurrent structures.
•Add time-based eviction (TTL).
•Support persistence for missed cache data.
•Support metrics (hit/miss rate).


✅ Final Summary:


Design an LRU Cache (LLD) that efficiently stores key-value pairs with:


•O(1) time complexity for get/put,
•Automatic eviction of least recently used elements,
•A clear and modular class-based design using HashMap + Doubly Linked List.


Solution

Comments