We've been looking this week at using a Two-Pointer pattern to merge sorted lists (not linked lists) in place. This works fine, as the first YouTube video showed, but there is a "hidden" performance issue. It looks like an O(M + N) solution, but every insertion in an in-place merge forces every subsequent element to shift to the right. That's actually O(M * (M + N)). Try rewriting this as a "reverse merge." By filling the destination list from back to front, you eliminate memory shifting. 💡 Why this wins the interview: * It shows you understand how lists and dynamic arrays handle insertions. * You reduce time complexity to true O(M + N) efficiency. #SoftwareEngineering #Java #CodingInterview #Algorithms #LeetCode #TechInterview #Optimization #TwoPointerPattern
More Relevant Posts
-
🚀 Problem Solved: Pow(x, n) – Fast Exponentiation Today I worked on LeetCode 50 (Medium) — implementing pow(x, n) efficiently. Instead of using the naive O(n) multiplication approach, I applied Binary Exponentiation (Fast Power Algorithm) to reduce the time complexity to O(log n). 🔎 Key Learnings: Handling negative powers correctly Preventing integer overflow (using long for edge cases) Understanding how dividing the exponent by 2 optimizes performance Applying mathematical logic in coding interviews Example: 2¹⁰ → 1024 2⁻² → 0.25 This problem was a great reminder that optimization isn’t optional — it’s essential. Slowly building strong fundamentals, one problem at a time. 💻✨ #LeetCode #ProblemSolving #Java #DataStructures #Algorithms #CodingJourney #InterviewPrep #BinaryExponentiation
To view or add a comment, sign in
-
-
🚀 Day 2 – DSA Practice Log Today was all about strengthening fundamentals and understanding *why* solutions work, not just coding them. 🔸 Rotate Array Learned how to optimize from brute force to the reversal technique (O(n), O(1)) 🔸Fibonacci Compared iterative vs recursive 👉 Realized why iterative is preferred in interviews 🔸 Kadane’s Algorithm 💡 Game changer! 👉 Logic: Extend or restart the subarray 👉 Helps find maximum subarray sum efficiently 🧠 What I learned today: Understanding the logic > memorizing code 📈 Progress update: Getting more comfortable with patterns and problem-solving Let’s keep going 🔥 #100DaysOfCode #DSAJourney #LeetCode #Java #KeepLearning
To view or add a comment, sign in
-
💻 Day 65 of my DSA Journey: Merge Intervals (Greedy + Sorting) 📊🧠🔥 📌 Key Insight This is a classic interval merging problem using sorting + greedy approach. 👉 Sort intervals based on start time 👉 Traverse the intervals 👉 If current interval overlaps with previous → merge them 👉 Otherwise → add as a new interval 🔍 Example Explanation Input: [1,3], [2,6], [8,10], [15,18] Step-by-step: [1,3] and [2,6] overlap → merge → [1,6] [8,10] → no overlap → keep as is [15,18] → no overlap → keep as is 🚀 Time & Space Complexity ⏱ Time: O(n log n) (sorting) 💾 Space: O(n) (output list) 🔖 Hashtags #DSA #Java #Greedy #Intervals #Sorting #ProblemSolving #CodingJourney #100DaysOfCode #Day65 #InterviewPrep #LeetCode #Algorithm
To view or add a comment, sign in
-
-
Solved LeetCode #5 — Longest Palindromic Substring. Problem: Given a string, return the longest substring that reads the same forward and backward. Approach Used: Expand Around Center Every palindrome has a center. For each index in the string, I expand outward to check two cases: • Odd-length palindromes (single center) • Even-length palindromes (two centers) While characters match, expand left and right and track the longest palindrome found. Complexity • Time Complexity: O(n²) • Space Complexity: O(1) Consistent problem solving builds pattern recognition for interviews. #LeetCode #DSA #Algorithms #CodingInterview #SoftwareEngineering #Java
To view or add a comment, sign in
-
-
🚀 Day 14 of My LeetCode Journey Today I solved Merge Intervals (LeetCode 56). Problem: Given a collection of intervals, merge all overlapping intervals and return the non-overlapping intervals that cover all the intervals. Approach: I used a Sorting + Linear Scan technique. Steps: • First sort the intervals based on their start time • Compare each interval with the last merged interval • If they overlap, merge them by updating the end value • Otherwise, add the interval to the result list Key Concepts: • Sorting • Interval manipulation • Greedy approach Time Complexity: O(n log n) Space Complexity: O(n) This is a classic interval problem frequently asked in coding interviews. #leetcode #datastructures #algorithms #java #codinginterview #softwareengineering
To view or add a comment, sign in
-
-
Most developers first see two pointers as just an interview trick. It is not. It is one of the simplest ways to make brute-force logic become efficient thinking. In this PDF, I broke down the Two Pointer Approach from the ground up: how it works why it works where to use it how to debug it what happens at a low level and why this technique shows up again and again in real problem solving What makes two pointers powerful is not just speed. It teaches you how to control a search space intelligently instead of checking everything blindly. That is a skill far bigger than one algorithm. Once you understand the pattern, you start seeing it in: sorted array problems pair matching duplicate removal palindrome checks sliding comparisons partition-style logic and many real-world data scanning tasks The biggest mistake many developers make is memorizing the code without understanding why each pointer moves. That is exactly where confusion starts. So I wanted this post to explain the intuition first, then the mechanics, then the practical usage. Which problem made you truly understand two pointers for the first time? #Java #DataStructures #Algorithms #TwoPointers #ProblemSolving #SoftwareEngineering #CodingInterview #Programming #DeveloperLearning #deutch
To view or add a comment, sign in
-
🔗 DSA Practice — Generate Parentheses (LeetCode 22) Today I solved Generate Parentheses, a classic backtracking problem that beautifully demonstrates how constraints guide recursion. 🔍 Key Learnings Backtracking helps explore all valid combinations efficiently. Maintaining constraints (open vs close count) avoids invalid states. Pruning early reduces unnecessary recursive calls. Recursive tree visualization makes the logic clearer. 🧠 Why This Problem Matters One of the best problems to understand backtracking patterns. Frequently asked in coding interviews. Builds a strong foundation for combinatorial problem-solving. 📌 Final Takeaway This problem shows how powerful recursion becomes when combined with well-defined constraints. Generate only what’s valid — not everything. #leetcode #dsa #backtracking #recursion #problemSolving #codingpractice #java #interviewpreparation #algorithms #100DaysOfCode #softwareengineering
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 16 Problem: 1878. Biggest Three Rhombus Sums in a Grid Topic: Array, Matrix, Sorting, Simulation 📌 Quick Problem Sense: You're given an m × n integer grid. A rhombus is a square rotated 45°, you sum only its border cells, not the inside. Find the top 3 distinct rhombus sums across all valid sizes and positions in descending order. A rhombus of size 0 is just a single cell, every cell counts! 🧠 Approach (Simple Thinking): 🔹 Every cell (i, j) can be the center of a rhombus of size r = 0, 1, 2, ... 🔹 Size 0 = just the cell itself, add it directly 🔹 For size r ≥ 1, walk the 4 diagonal sides of the rhombus border 🔹 Start at the top tip (i-r, j) and walk with directions: (+1,+1), (+1,-1), (-1,-1), (-1,+1) 🔹 Each side has exactly r steps, skip the last cell to avoid double counting corners 🔹 Use a TreeSet capped at size 3 to track top 3 distinct sums automatically 🔹 TreeSet handles sorting + deduplication, just poll the top 3 at the end! ⏱️ Time Complexity: Iterating all centers and sizes → O(m × n × min(m,n)²) Manageable for given constraints! 📦 Space Complexity: TreeSet holds at most 3 values → O(1) No extra matrix or prefix array needed! I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/ggewfAsx If you solved it using diagonal prefix sums or a different top-K trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #Java #ProblemSolving #DSA #CodingInterview
To view or add a comment, sign in
-
-
Day 17 – Diameter of Binary Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on finding the diameter of a binary tree, which is one of the most important tree recursion problems asked in interviews. 💡 Key Idea At every node, we calculate:Diameter through node = leftHeight + rightHeight. We keep updating the maximum value globally. Approach 1️⃣ Compute height of left subtree 2️⃣ Compute height of right subtree 3️⃣ Update:maxLen = max(maxLen, left + right) Time Complexity : O(N) . Space Complexity : O(H) . GitHub Code : - https://lnkd.in/g2QSmraF #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day17 #DeveloperJourney
To view or add a comment, sign in
-
-
🚀 Day 10/30– DSA Challenge 📌 LeetCode Problem – Valid Anagram 📝 Problem Statement Given two strings s and t, determine whether t is an anagram of s. A string is an anagram if it contains the same characters with the same frequency, but possibly in a different order. 📌 Example Input: s = "anagram" t = "nagaram" Output: true Explanation: Both strings contain the same characters with the same counts. Another Example Input: s = "rat" t = "car" Output: false Explanation: Characters are different. 💡 Initial Thought Process Brute force idea: Sort both strings Compare them If they are equal → anagram. Time Complexity: O(n log n) because of sorting. But we can do better. 🔥 Optimized Approach – Frequency Count 🧠 Key Insight Since strings contain lowercase English letters, we can store character frequencies using an array of size 26. Steps: Count frequency of characters in s Decrease frequency using characters in t If any value becomes negative → not an anagram 🚀 Algorithm 1️⃣ If lengths differ → return false 2️⃣ Create array count[26] 3️⃣ Traverse string s count[s[i] - 'a']++ 4️⃣ Traverse string t count[t[i] - 'a']-- 5️⃣ If any count < 0 → return false 6️⃣ Otherwise → return true ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) (fixed array of 26) 📚 Key Learnings – Day 10 ✔ Frequency counting is powerful for string problems ✔ Avoid sorting when counting works ✔ Hashing / frequency arrays appear often in interviews ✔ Think about character constraints Two strings. Simple logic. Efficient solution. Day 10 completed. Consistency continues 💪🔥 #30DaysOfCode #DSA #Java #InterviewPreparation #ProblemSolving #CodingJourney #Strings #LeetCode
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