📌 Day 19/100 – Design HashMap (LeetCode 706) 🔹 Problem: Design a basic HashMap data structure without using built-in hash table libraries. Implement methods to put, get, and remove key-value pairs efficiently. 🔹 Approach: Used a direct array-based mapping for keys up to 1,000,000. Initialized all values to -1 to mark absence of mappings. Implemented: put(key, value) → stores/updates the value. get(key) → returns the mapped value or -1 if absent. remove(key) → resets the key’s value to -1. 🔹 Key Learning: Reinforced understanding of hashing fundamentals and key-value mapping. Learned how to simulate data structures without built-in utilities. Understood the trade-off between memory and simplicity in design. A simple yet powerful reminder that sometimes brute structure works best! 💡 #100DaysOfCode #Day19 #LeetCode #Java #DSA #ProblemSolving #HashMap #CodingJourney
AJAY KUMAR’s Post
More Relevant Posts
-
📌 LeetCode Day 60 — #106. Construct Binary Tree from Inorder and Postorder Traversal Problem Description: You are given two integer arrays inorder and postorder representing the inorder and postorder traversal of a binary tree. Your task is to reconstruct the original binary tree from these traversals. Approach: Identify Root: In postorder, the last element is always the root. Find Root Index in Inorder: Using a HashMap, find the position of the root in the inorder array to separate left and right subtrees. Recursive Construction: Build the left subtree from the left part of inorder and corresponding postorder range. Build the right subtree similarly from the right parts. Return Root: Recursively combine subtrees to reconstruct the full tree. Complexity Analysis: Time Complexity: O(n) — each node is processed once using the HashMap lookup. Space Complexity: O(n) — recursion stack + map for index lookup. Hashtags: #BinaryTree #Recursion #HashMap #LogicBuilding #LeetCode100Days #Day60 #Java #ProblemSolving
To view or add a comment, sign in
-
-
🎯 Day 82 of #100DaysOfCode – LeetCode 137: Single Number II Today’s challenge was about identifying the unique number in an array where every element appears three times except for one. 🧩 Problem Statement: Given an integer array nums, where every element appears exactly three times except for one, find that single element that appears only once. Example: Input: [2,2,3,2] Output: 3 💭 Approach 1: Using HashMap At first, I solved this using a HashMap to count the frequency of each number. Traverse the array and store each element’s occurrence. Return the element whose count equals 1. 🕒 Time Complexity: O(n) 💾 Space Complexity: O(n) 🚀 Key Takeaway: Sometimes, the best way to optimize is not by adding more data structures, but by understanding how data behaves at the bit level. #100DaysOfCode #Day82 #LeetCode #Java #ProblemSolving #DSA #CodingJourney #BitManipulation #KeepLearning
To view or add a comment, sign in
-
-
🚀 HashMap’s O(1) Trick — Simplified🤔 We want a data structure that can store key–value pairs and retrieve values instantly. No scanning, no searching, just jump directly to the data. Wait...that's HashMap. How it works ? 👇 1️⃣ HASHING → DIRECT O(1) JUMP 🔹Every key is converted into a number (hashCode) & is mapped to a bucket via a fast bitwise calc, giving HashMap its O(1) jump to the exact spot. 2️⃣ BUCKETS STORE ENTRIES 🔹Internal array is buckets, key–value pair sits in the bucket chosen by hash calc. 3️⃣ COLLISIONS ARE NORMAL 🔹Unique keys ≠ unique hashes, limited buckets mean collisions and its normal. 4️⃣ JAVA 8's SMART COLLISION HANDLING 🔹Crowded buckets switch from linked lists to Red-Black Trees to handle collisions. 5️⃣ LOOKUP = RECOMPUTE + MATCH 🔹get(key) recomputes the hash, jumps to the bucket, and finds the entry instantly. 💬 What do you think ? #Java #HashMap #JavaCollections #DataStructures #BackendDevelopment
To view or add a comment, sign in
-
🚀 Day 35/100 ✅ Today, I explored an interesting problem — Linked List Cycle Detection 🌀 The task was to determine whether a linked list contains a cycle or not. I solved it using the Floyd’s Tortoise and Hare Algorithm, which uses two pointers — a slow one and a fast one. The slow pointer moves one step at a time, while the fast pointer moves two. If they ever meet, it means there’s a cycle! 🐢🐇 🔑 Key Features: Uses two-pointer technique (fast & slow) Detects cycles efficiently Requires no extra memory ⚙️ Time Complexity: O(n) 💾 Space Complexity: O(1) This problem helped me understand how to handle linked list traversal efficiently and how pointer manipulation can make algorithms both fast and space-saving. Another step forward in mastering data structures! 💪 #Day35 #100DaysOfCode #LinkedList #DataStructures #CodingJourney #Java #LeetCode #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode 349 – Intersection of Two Arrays. 🟩 What was the problem? Given two integer arrays, the goal is to return the set of unique elements that appear in both. Order doesn’t matter and duplicates shouldn’t appear in the result. It’s basically a quick check of shared values between two lists. 🛠️ What was my approach? • Stored all numbers from the first array in a HashMap • Walked through the second array and checked if each number exists in the map • Added valid matches to a list • Removed the number from the map to keep the result unique • Converted the final list to an integer array The idea was to use constant-time lookups to keep it simple and efficient. 📚 What I learned Even easy problems are a good reminder that the right data structure can keep things clean and quick. HashMaps are great when you need fast membership checks. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
💻 Day 68 of #LeetCode100DaysChallenge Solved LeetCode 380: Insert Delete GetRandom O(1) — a brilliant problem that blends hashing with dynamic arrays to achieve constant-time operations. 🧩 Problem: Design a data structure that supports the following operations in O(1) average time: insert(val) → Adds val if not present. remove(val) → Removes val if present. getRandom() → Returns a random element from the set with equal probability. 💡 Approach — HashMap + ArrayList: 1️⃣ Use a HashMap to store each value and its index in the ArrayList. 2️⃣ For insert: Add the element to the list and record its index in the map. 3️⃣ For remove: Swap the target element with the last element in the list, update indices in the map, then remove the last element for O(1) deletion. 4️⃣ For getRandom: Use a random index to fetch an element directly from the list. ⚙️ Complexity: Time: O(1) — for all operations on average Space: O(N) — for storing elements and indices ✨ Key Takeaways: ✅ Reinforced constant-time data structure design. ✅ Learned the powerful combo of HashMap + ArrayList for dynamic access. ✅ Built intuition for designing random-access systems efficiently. #LeetCode #100DaysOfCode #Java #HashMap #ArrayList #RandomizedSet #DSA #CodingJourney #ProblemSolving #WomenInTech
To view or add a comment, sign in
-
-
Finding the Missing Number in an Array | Brute Force vs Optimized Approach I explored an interesting DSA problem — Finding the Missing Number in an Array. I started with the brute-force solution, where for each number from 1 to N, I checked if it exists in the array or not. 🔹 Time Complexity: O(N²) Then, I optimized it using a simple mathematical approach: The sum of the first N natural numbers = (N * (N + 1)) / 2 Subtracting the actual array sum from this total gives the missing number efficiently. 🔹 Time Complexity: O(N) 🔹 Space Complexity: O(1) It was a great reminder that sometimes a small shift in logic can make a huge difference in efficiency 📸 I’ve also shared this code in a carousel post for better visualization. You can check out the complete code here on my GitHub repository: https://lnkd.in/eSiQyN_m #Java #DSA #ProblemSolving #LearningInPublic #GitHub #CodeOptimization #Arrays
To view or add a comment, sign in
-
#100DaysOfCode – Day 76 Insert a Node in a Doubly Linked List Problem: Given a position p and a value x, insert a new node with data x after the p-th node (0-based indexing) in a doubly linked list. Example: Input: p = 2, x = 6 Output: 2 <-> 4 <-> 5 <-> 6 My Approach: Traversed to the p-th node using a simple loop. Updated pointers of the prev and next nodes carefully to maintain bidirectional linkage. Handled edge cases for insertion at the end of the list. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds a strong base for advanced data structure problems like deletion, reversal, and flattening of linked lists. #takeUforward #100DaysOfCode #DSA #Java #ProblemSolving #GeeksForGeeks #LinkedList #Pointers #CodingChallenge #CodeNewbie #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #102 — Binary Tree Level Order Traversal 📘 Problem: Given the root of a binary tree, return the level order traversal of its nodes' values — meaning, traverse the tree level by level from left to right. Example: Input → [3,9,20,null,null,15,7] Output → [[3], [9,20], [15,7]] 🧠 My Approach: I solved this using a recursive approach instead of the usual queue-based BFS. 1️⃣ First, I calculated the total number of levels in the tree using recursion. 2️⃣ Then, for each level, I called a helper function `nThLevel()` to collect all nodes at that level. 3️⃣ I stored each level’s nodes in a separate list and finally returned a list of lists as the result. This approach helped me deeply understand the relationship between recursion depth and tree levels. 💡 What I Learned: ✅ Different ways to perform level order traversal — BFS vs recursion. ✅ How to effectively use recursion to explore each tree level. ✅ Improved understanding of function calls, base conditions, and data storage in recursive algorithms. #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
Day 3 — Median of Two Sorted Arrays Today’s focus was on one of the most popular LeetCode problems : "Median of Two Sorted Arrays.” The goal: merge two sorted arrays and find their median. At first glance, it feels simple — but getting it right with edge cases is where the logic truly kicks in. Used a two-pointer merge approach — merging elements in order until both arrays are traversed. Once the merged array is ready, finding the median becomes straightforward. It’s one of those problems that looks basic but builds a strong foundation for array manipulation and binary search-based optimizations later on. Slow progress, steady learning.. Similar problems: 🔹 Merge Sorted Array — LeetCode #88 🔹 Kth Element of Two Sorted Arrays (variation of this problem, often asked in interviews) 🔹 Find the Median from Data Stream — LeetCode #295 #100DaysOfDSA #LeetCode #Java #Arrays #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