1. Two Sum (#LeetCode – Easy) Today I revisited one of the most popular DSA problems: Two Sum. The goal is simple—find two indices in an array whose values add up to a given target but the key is doing it efficiently. * Approach: I used a hash map to store previously seen numbers and their indices. For each element: - Calculate its complement (target - current number) - Check if the complement already exists in the map - If yes, return both indices immediately - This reduces the time complexity from O(n²) to O(n) * Why this works well: - Single pass through the array - Efficient lookup using Map - Clean and readable logic #LeetCode #JavaScript #DSA #ProblemSolving #CodingPractice #SoftwareEngineering #LearningEveryDay
Two Sum Problem Solution with Hash Map
More Relevant Posts
-
When Trees Meet Two Pointers: A Clean Twist on Two Sum 🌳 Solved LeetCode - Two Sum IV (Input is a BST) today. The approach clicked once I combined two ideas: Inorder traversal to leverage the BST’s sorted order Two-pointer technique to efficiently search for the target sum By converting the tree into a sorted sequence, the problem reduces to a classic two-sum pattern. Key takeaway: Many tree problems become simpler when you translate them into array patterns, the trick is knowing when it’s safe to do so. Building stronger cross-pattern intuition in DSA. #LeetCode #DSA #BinarySearchTree #TwoPointers #TreeTraversal #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
Fixing an Unbalanced Tree the Systematic Way 🌳 Solved LeetCode 1382 - Balance a Binary Search Tree today. The approach was clean and systematic: -Use inorder traversal to extract nodes in sorted order -Pick the middle element as the root -Recursively build left and right subtrees This guarantees a height-balanced BST without complex rotations. Key takeaway: Sometimes the best way to fix a structure is to rebuild it with the right invariants, not patch it incrementally. This problem nicely connected traversal, sorting, and divide-and-conquer thinking. #LeetCode #DSA #BinarySearchTree #TreeTraversal #DivideAndConquer #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
The Hidden Danger of `var` in Old ECMAScript In early JavaScript, `var` was the only way to declare variables. While it worked, it also introduced some serious problems. The biggest issue? Scope confusion. `var` is function-scoped, not block-scoped. This means variables declared inside loops or conditionals can leak outside their intended scope, leading to: * Unexpected bugs * Overwritten values * Hard-to-debug behavior Another danger is hoisting. Variables declared with `var` are hoisted and initialized as `undefined`, which can cause logic errors if you assume they don’t exist yet. This is why modern JavaScript introduced `let` and `const` — safer, clearer, and easier to reason about. Understanding `var` is important for legacy code, but using it today without caution can be very risky. Learn it. Respect it. But don’t use it except you know what you're doing. #JavaScript #ECMAScript #WebDevelopment #Programming #Coding #SoftwareEngineering #LearningToCode #FrontendDevelopment
To view or add a comment, sign in
-
-
Two Trees, One Truth: Cracking Recursive Thinking 🌳 Solved LeetCode - 100. Same Tree today using recursion. The goal was simple: check whether two binary trees are structurally identical and have the same values. The solution clicked once I focused on the recursive base cases: -Both nodes are null → same -One is null → not same -Values differ → not same -Then recursively compare left and right subtrees. Key takeaway: Tree recursion becomes intuitive when you trust the base cases and let recursion handle the rest. Strengthening DSA fundamentals, one problem at a time. #LeetCode #DSA #BinaryTree #Recursion #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
📌 LeetCode | Binary Search & Insert Another Binary Search problem — Search a target in a sorted array. If the target doesn’t exist, return the index where it should be inserted. 🔹 Approach: Modified Binary Search 🔹 Time Complexity: O(log n) 🔹 Space Complexity: O(1) 🔹 Result: Accepted ✅ (All test cases passed) Key learning here: Binary Search isn’t just about finding elements — it’s also about identifying the correct position. Small twist, same core logic. These variations really sharpen problem-solving instincts. #DSA #BinarySearch #LeetCode #Coding #ProblemSolving #JavaScript #DailyProgress
To view or add a comment, sign in
-
-
When One Path Isn’t Enough: Tracking All Valid Paths 🌳 Solved LeetCode 113 - Path Sum II today. Unlike Path Sum I, this version requires returning all root-to-leaf paths that match the target sum. The key shift: -Use recursion to explore paths -Maintain a current path array -Apply backtracking (push before recursion, pop after) Key takeaway: Whenever a problem asks for all combinations or all paths, think backtracking + state cleanup. This one strengthened my understanding of recursion with state management. #LeetCode #DSA #BinaryTree #Backtracking #Recursion #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
✅ Solved LeetCode: First Bad Version (278) Used a clean binary search approach in JavaScript to find the first bad version efficiently. - Narrowed the search space using left & right pointers - Checked mid with isBadVersion() - Eliminated unnecessary versions on each step ⏱ Time: O(log n) 📦 Space: O(1) Simple, optimal, and efficient! 🚀 #LeetCode #BinarySearch #JavaScript #DSA #Coding
To view or add a comment, sign in
-
-
🚀 Day 4 of #LeetCodeDaily Problem: Remove Duplicates from Sorted Array (Easy) Remove duplicates in-place so each element appears only once and return the count of unique elements. 🔍 My Approach: Since the array is already sorted, duplicates are adjacent Used two pointers to overwrite duplicates while preserving order ⏱ Time: O(n) | 📦 Space: O(1) 💡 Learning: When input is sorted, many problems simplify into pointer-based solutions. #ProblemSolving #LeetCode #JavaScript #GeeksForGeeks #DSA #TwoPointers #30DaysDSAChallenge #LearningInPublic #InterviewPrep
To view or add a comment, sign in
-
-
I built a small tool called MultiJSON to simplify working with JSON. https://www.multijson.com 🙌 It helps you format, validate, and manage multiple JSON files in one place, and makes sharing clean JSON easy. The tool is fully client-side, so your data never leaves the browser. Built it to scratch a personal itch — sharing in case it helps others too. #JSON #WebDevelopment #SoftwareEngineering #DeveloperTools #PrivacyFirst #Coding #DataScience #JavaScript #WebDev
To view or add a comment, sign in
-
Call Stack & Memory Heap — JavaScript Basics You Must Know ⚙️ Ever wondered how JavaScript actually runs your code? Two core concepts make it happen: 🧠 Memory Heap Stores variables, objects, and data dynamically during execution. 📚 Call Stack Keeps track of function calls line by line using a Last In, First Out (LIFO) rule. Understanding these helps you: - Debug errors like stack overflow - Write better recursive functions - Avoid memory leaks - Understand why JavaScript is single-threaded I wrote a beginner-friendly breakdown with examples. Check the comment 👇 #JavaScript #WebDevelopment #Frontend #Programming #LearnJavaScript #SoftwareEngineering
To view or add a comment, sign in
-
More from this author
Explore related topics
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