🚀 Day 17 of #DevDSA Today I solved Remove Element (LeetCode 27) — a simple-looking problem that taught me a powerful lesson about edge cases & pointer safety. 🧠 Problem Summary Given an array, remove all occurrences of a value in-place and return the count of remaining elements. ⚡ My Approach (Two Pointer Technique) I used: left pointer → start of array right pointer → end of array 💡 Idea: Move right backward to skip unwanted values Swap when left hits the target value Shrink the valid window ❌ Challenge I Faced Initially, my code failed for this edge case: nums = [3,3], val = 3 👉 Issue: right pointer went out of bounds (-1) Caused undefined values during swap ✅ Key Learning 🔥 Always guard your pointers while (right >= left && nums[right] === val) { right--; } This small condition fixed everything! 📊 Complexity Time Complexity: O(n) Space Complexity: O(1) (in-place) 💡 Bonus Insight Sometimes, a simpler approach works better: let k = 0; for (let i = 0; i < nums.length; i++) { if (nums[i] !== val) { nums[k] = nums[i]; k++; } } 👉 Cleaner, safer, and interview-friendly! 📌 Takeaway Edge cases are not rare — they are where most bugs live. #DSA #100DaysOfCode #JavaScript #CodingJourney #LeetCode #ProblemSolving #Developers #TechGrowth
Remove Element Challenge: Edge Cases & Pointer Safety
More Relevant Posts
-
🚀 Day 12 of My LeetCode Journey — Linked List Patterns Getting Stronger Today’s problems: 🔹 Intersection of Two Linked Lists (LeetCode 160) 🔹 Remove Linked List Elements (LeetCode 203) 💡 Problem 1: Intersection of Two Linked Lists Explored two approaches: ✅ Brute Force Traverse headA and check every node in headB ⏱️ Time: O(m × n) ✅ Using Set Store all nodes of headB in a Set Traverse headA and check for intersection ⏱️ Time: O(m + n) | 📦 Space: O(n) 👉 Helped me understand how hashing can optimize comparisons. 💡 Problem 2: Remove Linked List Elements Solved using a Sentinel (Dummy) Node 🔥 👉 Create a dummy node before head 👉 Use prev pointer to skip unwanted nodes: prev.next = prev.next.next This approach simplifies edge cases like: Removing head node Multiple consecutive deletions 🧠 What I Learned: Sentinel nodes make linked list problems cleaner Hashing can reduce time complexity significantly Thinking in terms of nodes (not values) is key 🔥 Key Takeaways: Always look for ways to reduce nested loops Dummy nodes = lifesaver in linked list problems Practice is making pointer manipulation more intuitive Grateful for the guidance from Namaste DSA and Akshay Saini 🚀 Day 13 loading… 💪 #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #SoftwareEngineering #Programming #InterviewPrep #JavaScript #CodingLife #TechGrowth #ProblemSolving #Developers #LearnToCode #LinkedList #DSA #NamasteDSA #AkshaySaini
To view or add a comment, sign in
-
Day 19 of My Learning Journey Revisiting the Basics 🔁 Today I found myself going back to something we all use, but rarely think deeply about — Error Handling with try...catch. As developers, it’s easy to write try...catch and move on… But revisiting it reminded me — it’s not just syntax, it’s control over failure. try...catch isn’t there to hide errors. It’s there to make sure our application behaves predictably, even when things go wrong. 💭 A few things that stood out (again): Catch only what you can actually handle — don’t swallow errors blindly Overusing try...catch can make debugging harder Sometimes letting the error bubble up is the better design Pairing it with finally is underrated for cleanup logic And the most important reminder: Robust systems aren’t built by avoiding errors… They’re built by handling them intentionally. Funny how revisiting basics always brings deeper clarity than learning something new. #Day19 #JavaScript #FrontendDeveloper #ErrorHandling #CleanCode #CodingJourney #100DaysOfCode #LearnInPublic #WebDevelopment #ContinueousLearner #LearningNeverStops #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 13 of My LeetCode Journey — Refining Linked List Skills Today’s problems: 🔹 Remove Duplicates from Sorted List (LeetCode 83) 🔹 Remove Nth Node From End of List (LeetCode 19) 💡 Problem 1: Remove Duplicates from Sorted List Since the list is already sorted: 👉 Just compare current node with next node 👉 If curr.val === curr.next.val → skip the duplicate 👉 Else → move forward Simple logic, but very effective due to the sorted property! 💡 Problem 2: Remove Nth Node From End of List Solved using two approaches: ✅ Two-Pass Approach First pass → calculate length Second pass → remove (length - n) node ⏱️ Time: O(n) ✅ One-Pass Approach (Optimized) 🔥 Use two pointers (fast & slow) Move fast ahead by n steps Move both together → when fast reaches end, slow is at target 👉 This approach is cleaner and more efficient! 🧠 What I Learned: Sorted data can simplify problems significantly Two-pointer technique continues to be super useful There’s always a way to reduce passes in linked list problems 🔥 Key Takeaways: Look for patterns (sorted, reversed, etc.) Optimize from two-pass → one-pass when possible Linked Lists are all about pointer precision Big thanks to Namaste DSA and Akshay Saini 🚀 for the guidance Day 14 loading… 💪 #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #SoftwareEngineering #Programming #InterviewPrep #JavaScript #CodingLife #TechGrowth #ProblemSolving #Developers #LearnToCode #LinkedList #TwoPointers #DSA #NamasteDSA #AkshaySaini
To view or add a comment, sign in
-
🚀 Day 19 of #DevDSA Today’s problem: Add Digits (LeetCode 258) 👉 Problem Statement: Given an integer, keep adding its digits until the result becomes a single digit. 💡 My Approach (Recursive Thinking): Convert number → string Loop through digits and calculate sum If result has more than 1 digit → repeat Base case: when length becomes 1 🧠 Key Learning: This problem looks simple but teaches: Recursion fundamentals Base case importance Breaking problems into smaller subproblems 📊 Complexity: Recursive approach → O(log n) 🔥 Takeaway: Strong basics like recursion help you solve bigger problems easily. #Day19 #DSA #LeetCode #CodingJourney #JavaScript #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 30 of #devDsa 💡 I got stuck on a simple recursion problem… and the bug was just ONE character. I was solving sum of array using recursion, and everything looked correct… but the code just wouldn’t work. No errors. No output. Just confusion. After digging deeper, I found the culprit: 👉 "n--" 🔴 What I wrote (Buggy version): function sum(arr, n) { if (n < 0) return 0; return arr[n] + sum(arr, n--); } 🧠 I thought: “n-- means decrease n… so recursion should move forward” ❌ Reality: - "n--" returns the current value first - So "sum(arr, n--)" becomes → "sum(arr, n)" - Value doesn’t change in the function call 💥 Result: Infinite recursion / stuck logic 🟢 Correct version: function sum(arr, n) { if (n < 0) return 0; return arr[n] + sum(arr, n - 1); } ✅ Now each recursive call gets a new value → and moves toward the base case ⚖️ Quick Comparison: Expression| What it actually does "n--"| Use value → then decrease "--n"| Decrease → then use value "n - 1"| Explicit & safest for recursion 💭 Big takeaway: Recursion doesn’t care about what you intend… It only follows what you pass. 🚀 This tiny mistake taught me more than the whole problem. Have you ever been stuck because of something this small? #JavaScript #Recursion #DSA #Debugging #LearnInPublic #100DaysOfCode
To view or add a comment, sign in
-
🚀 Day 25 of #DevDSA Today’s problem: 👉 Find the element that appears only once in an array 💡 Approach I used : Created an object to store frequency of elements Traversed the array and counted occurrences Iterated over the object to find the element with frequency = 1 🧠 Key Learning: In JavaScript, an object works like a HashMap and is very useful for: Frequency counting Tracking occurrences Solving “unique element” problems efficiently ⏱ Complexity: Time → O(n) Space → O(n) 🔍 Example: Input → [1,2,2,3,3] Output → 1 Consistency > perfection 💯 Day by day, getting better. #Day25 #DSA #JavaScript #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 29 of #Dev #DSA Journey Today I revisited one of the most fundamental concepts in programming — Recursion 🔁 And what better way to practice than solving the classic Factorial problem. 👉 The idea is simple: Break a big problem into smaller subproblems until you reach a base condition. In factorial: 5! = 5 × 4 × 3 × 2 × 1 Which can be written as: n * factorial(n-1) 💡 Key Learning: Recursion is powerful, but only when: You clearly define a base case You ensure the problem is getting smaller each time One small mistake (like wrong decrement or missing base case) can lead to: ❌ Infinite recursion ❌ Stack overflow ⚡ Today’s takeaway: “Recursion is not magic — it’s just trusting the function to do the same job on a smaller input.” 📈 Slowly but surely, building strong fundamentals. #Day28 #100DaysOfCode #DSA #JavaScript #Recursion #CodingJourney #SoftwareEngineer #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 3/100 – #100DaysOfDSA Another day, more learning, and deeper understanding of in-place algorithms! 🔹 Problems Solved: Move Zeroes Merge Sorted Array 💡 Key Learnings: 👉 Problem 1: Move Zeroes Applied Two Pointer Technique One pointer tracks position for next non-zero element Another pointer iterates through the array Swap only when needed to maintain order ✅ Maintains relative order ✅ In-place (O(1) space) ✅ O(n) Time Complexity 👉 Problem 2: Merge Sorted Array Solved using reverse two-pointer approach Start filling from the end of nums1 (to avoid overwriting elements) Compare elements from nums1 and nums2, and place the larger one at the end ✅ Efficient merge without extra array ✅ O(m + n) Time ✅ In-place solution 🔥 What I learned today: Sometimes solving from the end instead of the beginning makes the problem much simpler and avoids unnecessary complexity. Consistency is building momentum. Day 3 done ✅ #100DaysOfCode #DSA #LeetCode #ProblemSolving #CodingJourney #SoftwareEngineer #JavaScript #TechGrowth #LearningInPublic #Developers
To view or add a comment, sign in
-
🚀 Day 11 of My LeetCode Journey — Deep Dive into Linked Lists Today was all about mastering patterns in Linked Lists: 🔹 Linked List Cycle (LeetCode 141) 🔹 Palindrome Linked List (LeetCode 234) 💡 Problem 1: Linked List Cycle Tried two approaches: ✅ Using Set Store visited nodes If node already exists → cycle detected ⏱️ Time: O(n) | 📦 Space: O(n) ✅ Fast & Slow Pointer (Floyd’s Algorithm) 🔥 Slow → 1 step Fast → 2 steps If they meet → cycle exists ⏱️ Time: O(n) | 📦 Space: O(1) 👉 This approach is elegant and optimal! 💡 Problem 2: Palindrome Linked List Again explored two approaches: ✅ Convert to Array Store values in array Compare from both ends ⏱️ Time: O(n) | 📦 Space: O(n) ✅ Optimal Approach (In-place) 🔥 Find middle node Reverse second half of linked list Compare both halves ⏱️ Time: O(n) | 📦 Space: O(1) 👉 This one really tested pointer manipulation skills! 🧠 What I Learned: One problem can have multiple valid approaches Optimal solutions often reduce space complexity Linked Lists = pointer mastery + careful thinking 🔥 Key Takeaways: Always try brute force first → then optimize Fast & slow pointer is a game-changing technique In-place solutions are highly valued in interviews Big thanks to Namaste DSA and Akshay Saini 🚀 for the guidance Day 12 loading… 💪 #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #SoftwareEngineering #Programming #InterviewPrep #JavaScript #CodingLife #TechGrowth #ProblemSolving #Developers #LearnToCode #LinkedList #TwoPointers #DSA #NamasteDSA
To view or add a comment, sign in
-
🚀 Day 2/100 – #100DaysOfDSA Continuing the journey with two interesting problems today! 🔹 Problems Solved: Best Time to Buy and Sell Stock Reverse String 💡 Key Learnings: 👉 Problem 1: Best Time to Buy & Sell Stock Used a greedy + two pointer mindset Track the minimum price so far (buy) and calculate profit at each step Update max profit whenever a better opportunity appears ✅ O(n) Time ✅ O(1) Space 👉 Problem 2: Reverse String Classic Two Pointer Technique One pointer at start, one at end Swap characters and move inward ✅ In-place solution ✅ No extra memory used 🔥 What I learned today: Sometimes the best solutions are not complex — just about tracking the right values at the right time. Consistency > Perfection. See you on Day 3 💪 #100DaysOfCode #DSA #LeetCode #ProblemSolving #CodingJourney #JavaScript #Developers #TechGrowth #SoftwareEngineer #LearningInPublic
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