🚀 Day 96 of #100DaysOfCode Today’s challenge was all about implementing an LRU (Least Recently Used) Cache in Java — a problem that truly tests your understanding of data structures and memory optimization. 💡 🧩 Problem Statement: Design a data structure that supports: get(key) → Returns value if key exists, else -1 put(key, value) → Inserts or updates value Both should work in O(1) time complexity. It should automatically remove the least recently used entry when the cache exceeds its capacity. ⚙️ What I Implemented: ✅ Used LinkedHashMap with access order enabled. ✅ Overrode removeEldestEntry() to maintain fixed capacity. ✅ Ensured get() and put() operations are constant time. ✅ Tested the cache with sample inputs to confirm LRU behavior. 📘 Sample Output: When I executed the program, I got: {3=30, 1=10, 4=40} This confirmed that the oldest (least recently used) entry was correctly removed when the cache was full. 🧠 Concepts Strengthened: Understanding LinkedHashMap internals Efficient memory management O(1) data retrieval and replacement logic Practical use of overriding and encapsulation 🔥 Each day of this challenge pushes me closer to mastering clean, efficient, and optimized Java code. This one made me appreciate how real-world caching systems like browsers and databases manage performance. 💬 Next Goal: To explore advanced multithreading problems and optimize concurrent data structures. ✨ Almost at the finish line — Day 96/100! Every line of code is another step toward growth. 💪 #100DaysOfCode #Java #DataStructures #CodingChallenge #ProblemSolving #LinkedHashMap #SoftwareEngineering #LearningJourney #DeveloperCommunity
Implemented LRU Cache in Java with LinkedHashMap
More Relevant Posts
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱️ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
📌 Day 27/100 - Generate Parentheses (LeetCode 22) 🔹 Problem: Given n pairs of parentheses, generate all combinations of well-formed (balanced) parentheses. 🔹 Approach: Use backtracking to build strings character-by-character. Keep two counters: open = number of '(' used, close = number of ')' used. You may add '(' while open < n. You may add ')' while close < open (to maintain balance). When the current string length reaches 2*n, add it to the result list. 🔹 Complexity: Time: proportional to the number of valid combinations (Catalan number) — roughly O(4ⁿ / n^(3/2)). Space: output-sensitive — O(n * Cn) for storing results, and O(n) extra for recursion depth. 🔹 Key Learning: Backtracking is ideal when you need to enumerate valid combinations subject to constraints. Always enforce constraints early (here: close < open) to prune invalid branches. Think in terms of state (open/close counts) rather than raw string manipulation. #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA
To view or add a comment, sign in
-
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙️ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Progress Update: Invert Binary Tree (Problem 226) 🌳 About the Problem: The task was to invert a binary tree — flipping it by swapping every left and right child node. It’s a classic problem that tests recursion and tree traversal logic. 🧠 My Approach: I went with a recursive approach. At each node, I swapped the left and right subtrees, then called the same logic for both sides. Once the node became null, the recursion stopped automatically. 💡 What I Learned: ✅ How recursion travels through a tree structure ✅ Why defining a clear base condition matters ✅ How simple logic can transform an entire data structure 🎯 Takeaway: This problem reinforced that not every challenge needs complexity — sometimes, the cleanest recursive idea is the smartest one. #LeetCode #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
💡 Day 12 of my 𝗝𝗮𝘃𝗮 𝗕𝗮𝗰𝗸𝗲𝗻𝗱 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝗼𝗳 𝘁𝗵𝗲 𝗗𝗮𝘆 𝘀𝗲𝗿𝗶𝗲𝘀: 🧠 Question: In Java, how does the 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿 (𝗚𝗖) work, and what strategies can you use to optimize GC performance in a high-throughput backend application? ✅ Answer: Java’s Garbage Collector automatically removes unused objects from the heap to free memory - preventing memory leaks. It works mainly through mark and sweep phases: 𝗠𝗮𝗿𝗸: Identify all reachable (in-use) objects 𝗦𝘄𝗲𝗲𝗽: Clean up unreferenced objects GC Optimization Strategies: 1. Choose the right GC algorithm 𝗚𝟭 𝗚𝗖 → great for large heaps and low pause times 𝗭𝗚𝗖 / 𝗦𝗵𝗲𝗻𝗮𝗻𝗱𝗼𝗮𝗵 → ultra-low pause time for high-load systems 2. Avoid creating unnecessary objects Prefer reuse (e.g., StringBuilder over String concatenation in loops). 3. Tune heap size Set proper Xms and Xmx to reduce resizing overhead: -𝘟𝘮𝘴2𝘨 -𝘟𝘮𝘹2𝘨 4. Use object pooling wisely Helpful when creating lots of short-lived expensive objects. 5. Analyze GC logs Enable GC logging to detect pauses and memory pressure. 𝗚𝗼𝗮𝗹: Minimize GC pauses → maximize throughput → ensure consistent API performance. ✅ Smart GC tuning can significantly boost your backend performance under heavy workloads. ⚙️ See you tomorrow for Day 13 👋 #Java #GarbageCollector #JVM #PerformanceTuning #BackendDeveloper #HighPerformance #ContinuousLearning #QuestionOfTheDay
To view or add a comment, sign in
-
✨ Day 52 of 100: Copy List with Random Pointer ✨ Today’s challenge was LeetCode 138 – Copy List with Random Pointer 🧠 The task: Given a linked list where each node has two pointers — 🔹 next: points to the next node 🔹 random: points to any node in the list (or null) we need to create a deep copy of this list. 💡 Key Takeaways: 🔹 Learned how to handle complex data structures where nodes reference each other in non-linear ways. 🔹 Practiced creating a deep copy using HashMap to maintain the mapping between original and copied nodes. 🔹 Explored an optimized in-place approach — interleaving original and copied nodes to avoid extra space. 🔹 Strengthened understanding of pointer manipulation and object references in Java. 🚀 This problem deepened my appreciation for how data structure cloning works — not just duplicating values, but preserving relationships! #LeetCode #100DaysOfCode #Day52 #Java #LinkedList #DeepCopy #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development