Day 46 — Hashing & Collision Handling (Java) Implemented a hash table from scratch to understand how hashing actually works under the hood—no HashMap shortcuts. What the code does (clearly and correctly): Takes hash table size and elements as user input Applies a simple hash function (key % size) Demonstrates collisions when multiple keys map to the same index Resolves collisions using chaining (LinkedList) Prints the final hash table bucket-wise Complexity (straight facts): Average insert/search: O(1) Worst case (all collisions): O(n) Space: O(n) This isn’t about reinventing HashMap. It’s about understanding why collisions happen and how they’re handled, which is exactly what interviews test. Verified with input: 10 15 20 25 30 35, size = 5 → collisions at index 0. #Java #DSA #DataStructureAndAlgorithm #Hashing #CollisionHandling #LinkedList #CoreJava #ProblemSolving #CodingPractice
Java Hash Table Implementation with Collision Handling
More Relevant Posts
-
🚀 Day 10/15 – Linked List Challenge Today’s problem was one of the most conceptually rich linked list questions so far: 🔹 Remove Zero Sum Consecutive Nodes from Linked List (LeetCode 1171) 📌 Approach used: Prefix Sum + HashMap + Dummy Node 🔍 What I learned today: How prefix sums help detect zero-sum sublists Using a HashMap to track prefix sum occurrences Safely removing an entire sublist in one step Why a dummy node is crucial for handling head deletions Cleaning intermediate prefix sums to avoid incorrect links 🧠 Key Insight: If the same prefix sum appears twice, the nodes in between must sum to zero. ⚙️ Complexity: Time Complexity: O(N) Space Complexity: O(N) This problem significantly strengthened my understanding of combining hashing, prefix sums, and pointer manipulation—a pattern that frequently appears in advanced interview problems. Continuing my 15-Day Linked List Challenge with momentum 💪 On to Day 11! #Day10 #15DaysChallenge #LinkedList #DSA #Java #PrefixSum #HashMap #ProblemSolving #InterviewPreparation #CodingJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🧠 How HashMap works internally (Java interview favorite) HashMap looks simple on the surface, but internally it’s one of the most important classes to understand as a Java developer. Here’s what happens when you put a value into a HashMap: The hashCode() of the key is calculated That hash is converted into an index (bucket) If no collision → entry is stored directly If collision → entries are stored as a LinkedList From Java 8+, if collisions exceed a threshold, it converts into a Red-Black Tree (for better performance) Why this matters: Poor hashCode() implementations can kill performance Explains why HashMap operations are O(1) on average Frequently asked in Java interviews Understanding internals makes you a better problem solver, not just a syntax user. 👉 What’s your go-to explanation of HashMap in interviews? #Java #CoreJava #HashMap #JavaInterviews #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
Revising Core Java with an interview-first mindset. Today’s focus: • HashMap vs Hashtable — performance & thread-safety trade-offs • Internal working of HashMap (hashing, buckets, resizing) • Why HashMap allows one null key (design reasoning) Big takeaway: Strong interviews test *why a design exists*, not just syntax. Next up: ConcurrentHashMap — when and why it’s used in real systems. Tech: Java | Core Java | Collections Framework #Java #CoreJava #BackendEngineering #SoftwareEngineer
To view or add a comment, sign in
-
Day 28 Problem Statement: Given an array arr[] of postive and negative integers, the objective is to find the number of subarrays having a sum exactly equal to a given number k. Examples: Input : arr[] = [10, 2, -2, -20, 10], k = -10 Output : 3 Explanation: Subarrays: arr[0...3], arr[1...4], arr[3...4] have sum equal to -10. Input : arr[] = [9, 4, 20, 3, 10, 5], k = 33 Output : 2 Explanation: Subarrays: arr[0...2], arr[2...4] have sum equal to 33. Input : arr[] = [1, 3, 5], k = 2 Output : 0 Explanation: No subarrays with 0 sum. Approach : To count subarrays with sum k, we use a hash map to store the frequency of prefix sums. At each index, we calculate the current prefix sum currSum and check if (currSum - k) exists in the map. If it does, it means there are subarrays ending at the current index with sum k, and we add their count to the result. Then we update the frequency of currSum in the map. #HappyCoding #DSA #java #hashing
To view or add a comment, sign in
-
💻 Day 6 of #30DaysOfCodeChallenge Today’s problem: Reverse a String 🔹 Objective: Take an input string and reverse it. 🔹 Skills practiced: String manipulation, loops, basic algorithm 🔹 Key takeaway: Understanding string traversal and building logic from scratch strengthens your problem-solving skills. Example: Input: "Chetan" → Output: "natehC" ✅ Simple, but reinforces algorithmic thinking and attention to detail. #CodingChallenge #DSA #Java #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🌟 Day 26/30 – Rotate List Approach: Handle edge cases (empty list, single node, k = 0) Traverse once to get the list length and tail Convert the list into a circular list Optimize rotations using k % length Locate the new tail at position (length − k) Break the circle to form the rotated list Time Complexity: O(n) Space Complexity: O(1) Key Takeaway: Making the linked list circular simplifies rotation logic and avoids extra space. #Day26 #LeetCode #DSA #Java #LinkedList #30DayChallenge #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 Solved Continuous Subarray Sum using Prefix Sum & HashMap. ➤ Approach (O(n), O(n) space): — Maintain a running totalSum — Compute remainder = totalSum % k — Store the first index where each remainder appears — If the same remainder appears again and the distance between indices ≥ 2, a valid subarray exists ➤ Key Insight: If two prefix sums have the same remainder when divided by k, their difference is a multiple of k. #LeetCode #Java #DSA #PrefixSum #HashMap #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🚀 Day 7 of 10 – HashMap Internals & ConcurrentHashMap (Interview-Level Clarity!) Ever wondered what really happens inside a HashMap? Here’s a crisp breakdown every Java developer should know 👇 🔹 HashMap Internals Stores data as key–value pairs using an array of buckets Uses hashCode() → index calculation → equals() for collision resolution Collision handling: Java 7: Linked List Java 8+: Linked List → Red-Black Tree (when bucket size > 8) Time Complexity: Average: O(1) Worst case: O(log n) (thanks to treeification) Not thread-safe ❌ 🔹 Why ConcurrentHashMap? Designed for high concurrency & performance Thread-safe without locking the entire map Uses bucket-level locking (Java 8 uses CAS + synchronized blocks) Allows multiple threads to read & write simultaneously No ConcurrentModificationException 🚀 🔹 HashMap vs ConcurrentHashMap FeatureHashMapConcurrentHashMapThread-safe❌ No✅ YesPerformance (Single thread)FastSlightly slowerPerformance (Multi-thread)UnsafeHighly efficientNull key/value✅ Allowed❌ Not allowed 💡 Interview Tip: 👉 Use HashMap for single-threaded applications 👉 Use ConcurrentHashMap for multi-threaded environments 📌 Mastering internals = cracking interviews with confidence. 🔜 Day 8 coming soon… #Java #HashMap #ConcurrentHashMap #JavaInternals #InterviewPreparation #BackendDevelopment #100DaysOfCode #AjayKumarKB
To view or add a comment, sign in
-
🚀 Day 2 of #100DaysOfCode Solved Contains Duplicate on LeetCode ✅ 🧠 Key takeaway: Using a HashSet helps quickly check if any element appears more than once by leveraging constant-time lookups. ⚙️ Approach: 🔹Traverse the array 🔹Add elements to a set 🔹If an element already exists → duplicate found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #Java #HashSet #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
#day299 of #1001daysofcode problem statement (0543): Diameter of Binary Tree Solved Diameter of Binary Tree today. Instead of calculating height and diameter separately, combined both in a single DFS using a custom return object (Info). #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode #1001daysofcode
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