"Designing LRU Cache with HashMap and LinkedList"

🚀 DSA Challenge – Day 94 Problem: Design LRU (Least Recently Used) Cache ⚡📦 This was one of those classic data structure design problems that truly tests your understanding of hash maps and linked lists working together seamlessly for O(1) performance! 🧠 Problem Summary: Implement a data structure that behaves like an LRU Cache, supporting: ✅ get(key) → Retrieve value in O(1). ✅ put(key, value) → Insert/update value in O(1). ✅ Automatically evict the least recently used key when capacity is exceeded. ⚙️ My Approach: To achieve O(1) operations, I combined two powerful structures: 1️⃣ HashMap (keyMap) → For constant-time key lookups. 2️⃣ Doubly Linked List → To maintain the order of usage (most recent at the front). 🔹 When a key is accessed or updated: Move it to the front (most recent). 🔹 When inserting a new key: If full → remove the least recently used node (from the tail). Insert the new key-value pair at the front. The linked list allows O(1) addition/removal, and the hashmap ensures O(1) lookup and update. 📈 Complexity: Time: O(1) → For both get and put. Space: O(capacity) → For the hashmap and list nodes. ✨ Key Takeaway: This problem elegantly demonstrates how data structures complement each other — the linked list maintains order, and the hashmap ensures constant-time access. A perfect synergy of logic and structure! ⚙️💡 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #LRUCache #DataStructures #HashMap #LinkedList #Python #Algorithms #SystemDesign #CodingChallenge #EfficientCode #InterviewPrep #TechCommunity #CodeEveryday #LearningByBuilding

  • text

To view or add a comment, sign in

Explore content categories