Simplified LRU Cache (focus on class design, clean code, edge cases)

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

Problem Statement

Designing a simplified Least Recently Used (LRU) cache. The focus is on class design, clean code, and handling edge cases.

LRU cache evicts the least recently used item when capacity is exceeded.


✅ Functional Requirements


1.Cache Operations
•get(key): Return value associated with key. If not present, return -1 or null. Marks the item as recently used.
•put(key, value): Insert or update a key-value pair. If the cache exceeds its capacity, evict the least recently used item.
2.Capacity
•Fixed maximum capacity N.
3.Edge Cases
•get on non-existent key
•put when cache is empty or full
•Updating existing key
•Single-item cache
4.Optional
•Thread-safe operations (for concurrent access)


🔐 Constraints & Assumptions


•Focus on LLD, clean class design, and edge cases.
•Single-machine, in-memory cache.
•Keys and values are integers (can be extended to generic types).
•Aim for O(1) time complexity for get and put.


🧱 Key Components & Classes


1.Node Class
•Represents a doubly linked list node storing key and value.
1 2class Node { 3public: 4 int key; 5 int value; 6 Node* prev; 7 Node* next; 8 Node(int k, int v) : key(k), value(v), prev(nullptr), next(nullptr) {} 9};
1.LRUCache Class
•Combines hash map for fast access and doubly linked list for LRU order.
1 2class LRUCache { 3private: 4 int capacity; 5 unordered_map<int, Node*> cache; // key -> Node 6 Node* head; // Most recently used 7 Node* tail; // Least recently used 8 9 void moveToHead(Node* node); // Helper: move node to head 10 void removeNode(Node* node); // Helper: remove node from list 11 void addNode(Node* node); // Helper: add node at head 12 13public: 14 LRUCache(int cap); 15 16 int get(int key); 17 void put(int key, int value); 18};
1.Helper Methods
1 2void LRUCache::moveToHead(Node* node) { 3 removeNode(node); 4 addNode(node); 5} 6 7void LRUCache::removeNode(Node* node) { 8 node->prev->next = node->next; 9 node->next->prev = node->prev; 10} 11 12void LRUCache::addNode(Node* node) { 13 node->next = head->next; 14 node->prev = head; 15 head->next->prev = node; 16 head->next = node; 17}


🔄 Example Workflow


1.Initialize cache with capacity = 2
2.put(1, 1) → cache: {1=1}
3.put(2, 2) → cache: {1=1, 2=2}
4.get(1) → returns 1, moves key 1 to head
5.put(3, 3) → capacity exceeded → evict LRU (key 2) → cache: {1=1, 3=3}


🧪 Expected Deliverables


•Class diagram showing LRUCache and Node relationships.
•Implementation of get and put with O(1) time complexity.
•Test cases:
•Accessing non-existent keys
•Adding/updating keys
•Capacity eviction
•Single-item cache edge case


💡 Discussion Points / Trade-offs


1.Data Structure Choice
•HashMap + Doubly Linked List → O(1) for get and put
•Alternative: TreeMap → O(log N), simpler eviction but slower access
2.Edge Cases Handling
•Empty cache
•Updating existing key → move to head
•Eviction when capacity = 1
3.Thread Safety
•Optional mutex or synchronized block for concurrent access
•Fine-grained locking for higher throughput
4.Extensibility
•Generic key-value types
•Time-based eviction (TTL)
•Metrics for cache hits/misses


Solution

Comments