🚀 100 Days DSA Coding Challenge | Day 39 📌 Problem: Implement Stack using Queues 🧠 What I learned today: • Difference between Stack (LIFO) and Queue (FIFO) • How to simulate a stack using queue operations • Using rotation technique to maintain stack order • Strengthening understanding of data structure design 🛠️ Approach: Use a single queue. When pushing an element: Add it to the queue Rotate the existing elements so the new element comes to the front This ensures: pop() removes the latest element top() returns the most recent element ⏱️ Complexity: Push: O(n) Pop: O(1) Top: O(1) Empty: O(1) 💻 Language: Java Today’s takeaway: Understanding the behavior of data structures allows us to build one structure using another. 🔄 On to Day 40 🚀 #100DaysOfCode #DSA #Java #Stack #Queue #DataStructures #ProblemSolving #CodingChallenge #LearningInPublic #LeetCode
Implementing Stack using Queue in Java
More Relevant Posts
-
🚀 100 Days DSA Coding Challenge | Day 40 📌 Problem: Implement Queue using Stacks 🧠 What I learned today: • Understanding how to simulate Queue (FIFO) using Stack (LIFO) • Using two stacks to reverse order and maintain queue behavior • Concept of amortized O(1) time complexity • Strengthening fundamentals of data structure design 🛠️ Approach: Use two stacks: in → for pushing elements out → for popping/peeking When out is empty: Transfer all elements from in to out This reverses the order, making it behave like a queue ⏱️ Complexity: Push: O(1) Pop: O(1) (amortized) Peek: O(1) (amortized) Empty: O(1) 💻 Language: Java Today’s takeaway: Understanding how data structures work internally helps us build one structure using another efficiently. 🔄 On to Day 41 🚀 #100DaysOfCode #DSA #Java #Stack #Queue #DataStructures #ProblemSolving #CodingChallenge #LearningInPublic #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 99 of DSA Problem Solving Today’s problem: Word Pattern (LeetCode 290) At first glance, this problem looked simple — just matching characters with words. But the real challenge was ensuring a bijective mapping (one-to-one relationship) between pattern characters and words. 🔍 Problem Idea: We need to check if each character in the pattern maps to exactly one word in the string, and each word maps back to only one character. 💡 Key Learning: One-directional mapping is NOT enough We must ensure two-way consistency (character → word AND word → character) HashMaps are powerful for maintaining this mapping efficiently 🧠 Concepts Practiced: HashMap usage String manipulation (split) Bijective mapping logic ⏱ Time Complexity: O(n), where n = number of words 💭 Real Journey Behind the Solution: Initially, I thought using just one HashMap would work, but that led to incorrect mappings. Then I realized the importance of maintaining two HashMaps to ensure no duplication from either side. This problem strengthened my understanding of mapping relationships — a small concept but very powerful in many DSA problems. 🔥 Slowly getting better every day… consistency is the real game. #Day99 #DSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 3 🧩 Problem Solved: Combination Sum III (LeetCode #216) 💭 What I Learned: Used backtracking to find all possible combinations of k numbers (1–9) that sum up to a target n. At each step: ✔️ Decided whether to include the current number ✔️ Reduced both the remaining sum (n) and count (k) ✔️ Ensured each number is used only once by moving to the previous index Applied constraints effectively to prune recursion when: Remaining sum becomes negative Required count becomes invalid This improved my understanding of handling multiple constraints (sum + size) simultaneously in recursion. ⏱ Time Complexity: O(C(9,k) 🧠 Space Complexity: O(k) (recursion depth) ⚡ Key Takeaways: ✔️ Backtracking with multiple constraints requires careful pruning ✔️ Fixed input size can significantly reduce complexity ✔️ Choosing + skipping pattern helps explore all valid combinations 💻 Language Used: Java ☕ 📘 Concepts: Backtracking · Recursion · Combinations · Constraint Handling #CodeEveryday #DSA #LeetCode #Java #Backtracking #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 Day 70 — LeetCode Practice ✅ Problem Solved: Minimum Operations to Make Array Sum Divisible by K 💡 What I learned today: • Understood how modulus (%) helps in solving optimization problems • Learned that instead of performing operations repeatedly, we can directly use math logic • Realized that the answer depends on sum % k • Improved thinking towards efficient (O(n)) solutions 📊 Approach: • Calculated total sum of the array • Found remainder using sum % k • That remainder itself gives the minimum operations required 🎯 Key Insight: Sometimes, problems look complex but can be solved using simple mathematical observations instead of brute force 🔥 Consistency is the key — showing up every day! #Day70 #LeetCode #DSA #CodingJourney #ProblemSolving #Consistency #Java
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 22 Today’s focus: Prefix Sum + Fixed Window optimization. Problem solved: • K Radius Subarray Averages (LeetCode 2090) Concepts used: • Prefix Sum • Fixed-size sliding window • Efficient range sum calculation Key takeaway: The goal is to calculate the average of subarrays centered at each index with radius k. A brute-force approach would recompute sums for every index, leading to O(n·k) time. Instead, using prefix sum, we can compute any subarray sum in O(1) time. For each index i, the valid window is: [i - k, i + k] If this window is within bounds, we calculate the sum using prefix sum and divide by (2k + 1). If not enough elements exist on either side, we return -1. This approach reduces the complexity to O(n) and avoids repeated calculations. This problem highlights how prefix sum helps in efficiently handling range-based queries. Continuing to strengthen fundamentals and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 89 – DSA Journey | Invert Binary Tree Continuing my daily DSA practice, today I worked on a classic problem that builds strong intuition around tree manipulation. 📌 Problem Practiced: Invert Binary Tree (LeetCode 226) 🔍 Problem Idea: Given a binary tree, invert it by swapping the left and right children of every node. 💡 Key Insight: At each node, simply swap its left and right subtrees. Recursively applying this across the tree results in a complete mirror of the original tree. 📌 Approach Used: • If the node is null → return null • Swap left and right children • Recursively apply the same operation on both subtrees • Return the root after inversion 📌 Concepts Strengthened: • Tree traversal • Recursion • Tree manipulation • Mirror transformation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Simple operations applied recursively can transform complex structures like trees efficiently. On to Day 90! 🚀 #Day89 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 72 – DSA Journey | Reversing a Sublist in Linked List Continuing my daily DSA practice, today I worked on a classic linked list problem that tested my understanding of pointer manipulation and in-place reversal. 📌 Problem Practiced: Reverse Linked List II (LeetCode 92) 🔍 Problem Idea: Reverse a portion of a linked list between given positions left and right, without affecting the rest of the list. 💡 Key Insight: Instead of reversing the entire list, the trick is to re-link nodes within the given range by carefully adjusting pointers. 📌 Approach Used: • Use a dummy node to handle edge cases • Traverse to the node just before left • Iteratively move nodes to the front of the sublist • Maintain connections with the remaining list 📌 Concepts Strengthened: • Linked list traversal • Pointer manipulation • In-place reversal • Handling edge cases ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 🔥 Today’s takeaway: Small pointer changes can drastically transform a data structure — understanding them deeply is key. On to Day 73! 🚀 #Day72 #DSAJourney #LeetCode #LinkedList #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Progress – Rotate Array Kicking off today’s DSA practice, I solved the “Rotate Array” problem on LeetCode. 🔍 Problem Overview: Given an integer array, rotate the array to the right by k steps, where k is non-negative. The rotation must be done in-place without using extra space. 💡 My Approach: Used the array reversal technique for optimal performance First reversed the initial part, then the remaining part, and finally the entire array Applied k = k % n to handle cases where k is greater than array size 📈 Key Takeaway: This problem reinforced my understanding of in-place array manipulation and how clever use of reversal algorithms can optimize both time and space complexity. Consistency is building confidence step by step 💯 #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #Learning
To view or add a comment, sign in
-
-
🚀 DSA Every Day – Day 2 📍 Problem: Minimum Distance to Target Element 💡 Platform: LeetCode 🧠 Approach: Brute Force with Bidirectional Traversal 🔍 Problem Summary: Given an array, a target value, and a starting index, find the minimum distance between the start index and any index where the target exists. ⚙️ Approach Explained: Traverse forward (start → end) Traverse backward (start → beginning) For each occurrence of target: Compute distance → |start - i| Maintain the minimum distance 📈 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) 🎯 Key Takeaways: Always think in terms of distance minimization problems Bidirectional traversal ensures complete coverage Can be optimized further using early stopping if needed Tell me your approach in comments ? Would love if people would like to join me in this habit for learning every day. 🔥 Progress: Day 2/ 60 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #SoftwareEngineering #60DaysOfCode
To view or add a comment, sign in
-
-
=> Day 20/90 DSA Journey Solved: Count Odd Numbers in an Interval (LeetCode 1523) Instead of using loops, I applied a simple mathematical formula to achieve an O(1) solution. 🔹 Key Idea: Count of odd numbers between low and high can be calculated directly using: -> ((high + 1) / 2) - (low / 2) 🔹 Why this works: -> It efficiently counts odds up to high and subtracts odds before low. 🔹 Example: Input: low = 3, high = 7 Output: 3 (Odd numbers → 3, 5, 7) -> Time Complexity: O(1) -> Space Complexity: O(1) #Java #DSA #LeetCode #ProblemSolving #Coding #Optimization
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