🧠 Day 37 / 100 – Implement Queue using Stacks (LeetCode #232) Today’s challenge was about building a Queue using two Stacks, and it really made me appreciate the power of thinking creatively with basic data structures. A queue works in FIFO (First In, First Out) order, while a stack follows LIFO (Last In, First Out) — but by using two stacks, I could recreate queue behavior perfectly. This problem taught me how sometimes you don’t need a new data structure — just a new perspective. By transferring elements only when necessary, I learned how to make operations both efficient and intuitive. 🔍 Key Learnings Two stacks can together simulate queue behavior efficiently. Use one stack for incoming elements and the other for outgoing ones. Always transfer only when needed to maintain time efficiency. 💭 Thought of the Day Creativity in coding often comes from repurposing what you already know. This challenge reminded me that mastering fundamentals allows you to solve complex problems with simple logic. 🔗 Problem Link:https://lnkd.in/g7my77rr #100DaysOfCode #Day37 #LeetCode #Python #Queue #Stack #DataStructures #ProblemSolving #AlgorithmPractice #CodingJourney #LearnByDoing #TechGrowth #CleanCode #PythonProgramming
Implementing Queue using Stacks on LeetCode #232
More Relevant Posts
-
🔍 Day 30 | Shift Happens (and That’s Beautiful) Today’s LeetCode challenge was #848 – Shifting Letters 🌀 At first glance, it looks like a simple “shift characters” task — but under the hood, it’s a neat example of prefix-sum logic meets modular arithmetic. 💡 The Problem: For each index i, shift the first i+1 letters by shifts[i] times — wrapping around from 'z' to 'a'. 🧠 The Trick: If we start from the end of the string and move backward, we can keep a cumulative shift sum that already accounts for all later shifts. This avoids repeatedly looping over the prefix and keeps our time complexity O(n) instead of O(n²). ⚙️ Approach: Traverse from right to left Keep adding each shift to a running total (totalShift) Apply totalShift % 26 to shift the current character efficiently Update the string using a StringBuilder 💨 Why It’s Optimal: ✅ Single pass — linear time ✅ Constant extra space ✅ Clean and scalable even for large inputs (up to 10⁵ length! Sometimes the cleanest solutions come from flipping your perspective — literally shifting your direction Shishir chaurasiya and PrepInsta #LeetCode #CodingChallenge #Java #Algorithms #ProblemSolving #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 31 of #100DaysOfCode — LeetCode + HackerRank Edition! Today’s challenge was all about tracking structure and resilience — decoding the longest valid parentheses substring in a given string. 🔗 Problem: longestValidParentheses(s) (LeetCode) 📌 Challenge: Given a string of '(' and ')', find the length of the longest well-formed (valid) parentheses substring. 🔍 Approach: → Used a stack to track indices of unmatched '(' characters → Introduced a base index to reset when encountering unmatched ')' → On each valid match, calculated the distance to the last unmatched index → Continuously updated the maximum valid length found so far 💡 What made it click: → Realized that tracking indices, not just characters, is key to measuring valid spans → Using stack[-1] after a pop gives the start of the current valid substring → Resetting with a base index ensures recovery after mismatches 📚 What I learned: ✅ Stack-based tracking is powerful for nested structures ✅ Index math reveals patterns that raw character matching misses ✅ Defensive coding (checking if stack is empty) prevents crashes and improves robustness ✅ Visual walkthroughs and guided hints make debugging intuitive and rewarding Have you tackled longestValidParentheses before? Did you go with dynamic programming, two-pass scanning, or stack-based logic like I did? Let’s swap strategies 💬 #Day30 #LeetCode #StackAlgorithms #ParenthesesMatching #Python #ProblemSolving #CleanCode #CodeEveryDay #LearnToCode #CodeNewbie #SoftwareEngineering #TechJourney #100DaysOfCode
To view or add a comment, sign in
-
-
📦 Day 276: Moving Things Around with shutil Let’s admit it — we all love organizing our messy folders at least once before chaos returns 😅. Python’s shutil module makes that cleanup painless — it’s like a virtual assistant that moves, copies, or deletes files for you. 👉 Here’s how: import shutil # Copy a file shutil.copy('source.txt', 'destination.txt') # Move a file shutil.move('notes.txt', 'backup_folder/') Whether you’re creating backups, reorganizing projects, or automating downloads, shutil quietly does the heavy lifting. 💡 Pro tip: Combine it with os.walk() to perform bulk operations (like copying only .pdf files). 🔹 Challenge: Write a “backup bot” that copies all .txt files from one folder to another. #Python #Automation #Shutil #DeveloperLife
To view or add a comment, sign in
-
🚀 Day 35 of #100DaysOfCode — LeetCode + HackerRank Edition! Today’s challenge was all about merging two sorted arrays in-place — no extra space allowed, just clean pointer logic. 📌 Challenge: Merge nums2 into nums1, assuming nums1 has enough trailing space. The final array must be sorted — and the merge must happen in-place. 🔍 Approach: → Used a two-pointer strategy from the back to avoid overwriting values in nums1 → Compared the largest elements from both arrays and placed them at the end → Moved pointers backward (p1, p2, p) until all elements were merged → Handled edge cases like empty nums2 or all elements being smaller than nums1 → Avoided sorting after merge — the logic itself ensures order 💡 What made it click: → Realized that merging from the front causes overwrites — merging from the back preserves data → Visualized the pointer movement with a dry run — that helped me catch off-by-one bugs → Learned that in-place algorithms often rely on reverse traversal and smart indexing 📚 What I learned: ✅ Two-pointer techniques are powerful for in-place operations ✅ Always think about overwrite risks when merging arrays ✅ Dry runs and visual walkthroughs are key to debugging pointer logic Have you tried merging sorted arrays in-place before? Did you use reverse traversal or a different trick? Let’s swap strategies 💬 #Day35 #LeetCode #ArrayProblems #TwoPointerTechnique #ProblemSolving #Python #DebuggingJourney #CleanCode #LearnToCode #CodeNewbie #SoftwareEngineering #TechJourney #100DaysOfCode
To view or add a comment, sign in
-
-
💡 Day 19 of My LeetCode Journey — 3Sum Closest Today’s problem was “3Sum Closest”, a slight twist on the classic 3Sum challenge. 🧩 Problem Statement: Given an integer array nums and a target value, find three integers whose sum is closest to the target. Return the sum of those three numbers. Example: Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 Explanation: (-1 + 2 + 1 = 2) ⚙️ Approach: Sort the array for easy pointer movement. Use a for loop to fix one element at a time. Apply the two-pointer technique to find the closest sum. Compare differences using Math.abs() to track which combination is nearer to the target. 📘 Key Learning: 🔹 Sorting simplifies pointer logic. 🔹 Math.abs() is crucial for measuring closeness. 🔹 Sometimes, you don’t need the exact answer — just the closest one. #30DaysOfCode #LeetCode #Java #CodingJourney #ProblemSolving #DataStructures #Algorithms #TwoPointers #TechLearning
To view or add a comment, sign in
-
-
💡 Day 40 / 100 – Letter Combinations of a Phone Number (LeetCode #17) Today’s challenge was about generating all possible letter combinations from a string of digits on a phone keypad. This problem blends recursion and backtracking, testing both logic and creativity. Each digit maps to a set of letters (like on an old mobile keypad), and the goal is to explore every possible letter combination that could be formed. The key is to use recursion effectively — adding one letter at a time and exploring deeper until the combination is complete. 🔍 Key Learnings Recursion builds combinations step-by-step with clean logic. Backtracking helps explore multiple paths efficiently without redundant work. Using base conditions effectively keeps recursion under control and prevents infinite loops. 💭 Thought of the Day Sometimes, complex problems can be solved by thinking recursively — one small step at a time. This challenge reminded me that big results are just the outcome of many small, consistent actions — just like our 100 days of code journey. 🔗 Problem Link: https://lnkd.in/gUuS6Zp6 #100DaysOfCode #Day40 #LeetCode #Python #Recursion #Backtracking #ProblemSolving #CodingChallenge #Algorithms #DataStructures #CleanCode #PythonProgramming #LogicBuilding #KeepLearning #CodingJourney
To view or add a comment, sign in
-
-
Day 55 of My DSA Challenge Problem: Count the number of triplets in an array that can form a valid triangle. Concept Recap: For any three sides to form a triangle, the sum of any two sides must be greater than the third side. Optimized Approach: Instead of checking every triplet (which takes O(N³)), I sorted the array and used a two-pointer technique to bring it down to O(N²): Sort the array. Fix the largest side (arr[k]) and use two pointers (i, j) to find pairs where arr[i] + arr[j] > arr[k]. If the condition holds, all elements between i and j can form valid triangles. Count and move pointers accordingly. Time Complexity: O(N²) Space Complexity: O(1) Takeaway: Sorting combined with the two-pointer approach often transforms brute-force solutions into elegant and efficient ones. #Day55 #DSAChallenge #TwoPointerTechnique #Sorting #Optimization #Java #ProblemSolving #DataStructures #Algorithms #CodingChallenge #GeeksforGeeks #LeetCode #100DaysOfCode #ProgrammersJourney #TechLearning
To view or add a comment, sign in
-
-
🌳 Day 60 of 100: Maximum Depth of Binary Tree 🌳 Today’s challenge was LeetCode 104 – Maximum Depth of Binary Tree 🌲 The task? Given the root of a binary tree, find the maximum depth — the number of nodes along the longest path from the root to a leaf node 🍃 💡 Intuition: A binary tree’s depth can be thought of as its “height”. To find it, we just need to know the depth of the left and right subtrees — and the answer is the greater of the two, plus one for the current node. This is a classic example of recursion done right — breaking a big problem into smaller ones that mirror the original. ⏱ Time Complexity: O(n) – visit every node once 🗂 Space Complexity: O(h) – h is the height of the tree (recursion stack) ✨ Key Takeaway: This problem beautifully highlights the power of divide and conquer — by solving smaller subproblems (left and right trees), we can elegantly solve the bigger one. #100DaysOfCode #Day60 #LeetCode #Java #CodingJourney #BinaryTree #Recursion #DataStructures #CodingPractice #ProblemSolving #WomenWhoCode #CodeNewbie #LearnToCode
To view or add a comment, sign in
-
-
🚀 Day 32 of #100DaysOfCode — LeetCode + HackerRank Edition! Today’s challenge was all about frequency mapping and subarray logic — solving the Picking Numbers problem from HackerRank. 🔗 Problem: pickingNumbers(a) (HackerRank) 📌 Challenge: Given an array of integers, find the length of the longest subarray where the absolute difference between any two elements is ≤ 1. 🔍 Approach: → Used Python’s Counter to map frequencies of each number → For each number num, calculated freq[num] + freq[num + 1] → Tracked the maximum such sum to find the longest valid subarray → Avoided brute-force by leveraging frequency patterns instead of scanning all subarrays 💡 What made it click: → Realized that valid subarrays must contain only num and num + 1 → Frequency mapping reveals hidden structure in the data → Clean logic with max() and get() made the solution elegant and readable 📚 What I learned: ✅ Frequency-based thinking can simplify subarray problems ✅ Counter is a powerful tool for pattern detection ✅ Avoiding brute-force leads to scalable, efficient solutions ✅ Sharing dry runs and visual walkthroughs helps others grasp the intuition faster Have you tackled Picking Numbers before? Did you go with sorting, brute-force, or frequency mapping like I did? Let’s swap strategies 💬 #Day31 #HackerRank #SubarrayLogic #FrequencyMapping #Python #ProblemSolving #CleanCode #CodeEveryDay #LearnToCode #CodeNewbie #SoftwareEngineering #TechJourney #100DaysOfCode
To view or add a comment, sign in
-
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