Day 83/100 🚀 | #100DaysOfDSA Solved LeetCode 225 – Implement Stack using Queues today. The problem was to implement stack operations (LIFO) using only queue operations (FIFO). Approach: Used a single queue with rotation technique. • For push(x): Added the element to the queue Rotated the queue by moving previous elements to the back This ensures the newest element comes to the front (stack top) • For pop(): Simply removed from the front of the queue • For top(): Returned the front element • For empty(): Checked if queue is empty This way, the queue always maintains stack order. Time Complexity: • push → O(n) • pop → O(1) • top → O(1) Space Complexity: O(n) Key takeaway: By reordering elements during insertion, we can simulate stack behavior using a single queue efficiently. #100DaysOfDSA #LeetCode #DSA #Java #Queue #Stack #ProblemSolving #Consistency
Implement Stack using Queues in Java
More Relevant Posts
-
Day 84/100 🚀 | #100DaysOfDSA Solved LeetCode 232 – Implement Queue using Stacks today. The problem was to implement queue operations (FIFO) using stack operations (LIFO). Approach: Used two stacks (inStack & outStack) to simulate queue behavior. • push(x) → Push element into inStack • pop(): If outStack is empty, move all elements from inStack to outStack (this reverses order) Pop from outStack • peek(): Same logic as pop, but return top of outStack • empty(): Check if both stacks are empty This ensures correct FIFO order using LIFO structures. Time Complexity: • push → O(1) • pop → Amortized O(1) • peek → Amortized O(1) Space Complexity: O(n) Key takeaway: By reversing order using a second stack only when needed, we achieve efficient queue operations with amortized constant time. #100DaysOfDSA #LeetCode #DSA #Java #Stack #Queue #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 69/100 🚀 | #100DaysOfDSA Solved LeetCode 25 – Reverse Nodes in k-Group today. The problem was to reverse nodes of a linked list in groups of size k, while leaving the remaining nodes (if less than k) as it is. Approach: Used recursion + iterative reversal for each group. • Reversed the first k nodes using the standard linked list reversal technique. • Kept track of the next group’s starting node. • If the number of nodes in the current group is less than k, reversed it back to maintain original order. • Recursively called the function for the remaining list. • Connected the current reversed group with the result of the next recursion. This ensures only valid k-sized groups are reversed. Time Complexity: O(n) Space Complexity: O(n/k) (due to recursion stack) Key takeaway: In linked list problems, combining local reversal (k nodes) with recursion for the remaining list is a powerful pattern for segment-based operations. #100DaysOfDSA #LeetCode #DSA #Java #LinkedList #Recursion #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 68/100 🚀 | #100DaysOfDSA Solved LeetCode 374 – Guess Number Higher or Lower today. The problem was to find a hidden number between 1 and n using the provided guess() API, which tells whether the guessed number is too high, too low, or correct. Approach: Used Binary Search to efficiently find the target number. • Initialized left = 0 and right = n. • Repeatedly calculated mid = left + (right - left) / 2. • Called the guess(mid) API: If it returns 0, found the number → return mid. If it returns 1, the number is higher → move left = mid + 1. If it returns -1, the number is lower → move right = mid - 1. • Continued until the correct number is found. Time Complexity: O(log n) Space Complexity: O(1) Key takeaway: Whenever the search space is sorted or bounded with a condition, Binary Search is the most optimal approach to reduce time complexity drastically. #100DaysOfDSA #LeetCode #DSA #Java #BinarySearch #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 76/100 🚀 | #100DaysOfDSA Solved LeetCode 1848 – Minimum Distance to the Target Element today. The problem was to find the minimum distance between a given start index and any occurrence of a target value in the array. Approach: Used a simple linear scan. • Iterated through the array • Whenever nums[i] == target, calculated distance = |i - start| • Kept track of the minimum distance using a variable • Returned the minimum distance at the end Since all elements need to be checked, this straightforward approach works efficiently. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: Sometimes the most optimal solution is a simple traversal—don’t overcomplicate when constraints are straightforward. #100DaysOfDSA #LeetCode #DSA #Java #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 61/100 — #100DaysDSAChallenge Today I worked on a classic problem: Implement Queue using Stacks. 💡 The key idea We use two stacks: 1. One stack for input (push operations) 2. One stack for output (pop/peek operations) When we need to remove an element: If the output stack is empty, we move all elements from input stack to output stack This reverses the order Now the oldest element is on top and ready to be removed ⚙️ Complexity ⏱️ Amortized Time: O(1) 💾 Space: O(n) 🧠 What I learned today Today’s lesson was about reversing data flow to achieve a different behavior. #Java #DSA #Stack #Queue #LeetCode #ConsistencyCurve
To view or add a comment, sign in
-
-
🚀Day 40 LeetCode 1161 – Maximum Level Sum of a Binary Tree Ever wondered how to find the level of a binary tree with the maximum sum of node values? 🤔 Here’s the idea: We traverse the tree level by level (BFS) and compute the sum at each level. The trick is to keep track of the maximum sum and return the smallest level where this occurs. 💡 Key Insight: Level-order traversal (using a queue) naturally processes nodes one level at a time, making it perfect for this problem. 🔧 Approach: ✔ Use a queue for BFS ✔ For each level: • Calculate sum of nodes ✔ Track maximum sum and corresponding level 🔥 Takeaway: Whenever a problem involves levels in a tree, think BFS first — it often leads to the cleanest solution. #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #BFS #ProblemSolving
To view or add a comment, sign in
-
-
Day 44🚀 Solved Number of Islands — a classic DFS/BFS problem that really tests grid traversal and thinking in connected components. 💡 Key Takeaways: Learned how to treat the grid like a graph Used DFS to explore and mark visited land Strengthened understanding of recursion + boundary handling ⚡ Result: ✅ All test cases passed ⏱️ Optimized runtime 📈 Improving problem-solving speed day by day Consistency > Motivation. Showing up daily is the real win. #Day44 #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
Day 73/100 🚀 | #100DaysOfDSA Solved LeetCode 219 – Contains Duplicate II today. The problem was to check if there are two equal elements in the array such that their indices differ by at most k. Approach: Used a sliding window + HashSet. • Iterated through the array while maintaining a window of size k • For each element: Checked if it already exists in the set → if yes, duplicate found within range → return true Added the current element to the set • If the set size exceeds k, removed the element that goes out of the window (nums[i - k]) This ensures we only track elements within the allowed index distance. Time Complexity: O(n) Space Complexity: O(k) Key takeaway: When constraints involve a fixed index range (k), think of using a sliding window with HashSet to efficiently track elements. #100DaysOfDSA #LeetCode #DSA #Java #SlidingWindow #HashSet #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 103 - LeetCode Journey Solved LeetCode 232: Implement Queue using Stacks ✅ Classic problem where you reverse the thinking. Queue is FIFO, but stacks are LIFO… So the trick is to use two stacks to simulate queue behavior. Approach used: Push → reverse elements using second stack Pop/Peek → operate directly from main stack Key learnings: • Understanding stack vs queue behavior • Using two stacks to reverse order • Designing data structures from scratch • Thinking in terms of operations, not just code ✅ All test cases passed ⚡ Efficient and clean implementation This is one of those problems that builds strong fundamentals 💪 #LeetCode #DSA #Stack #Queue #Java #CodingJourney #ProblemSolving #InterviewPrep
To view or add a comment, sign in
-
-
Day 70/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Reverse Linked List II A neat variation of linked list reversal where only a specific portion of the list is reversed. Problem idea: Reverse a linked list from position left to right, keeping the rest of the list unchanged. Key idea: In-place reversal using pointer manipulation. Why? • We don’t reverse the whole list, only a segment • Need to reconnect the reversed part correctly • Must carefully track boundaries (left and right) How it works: • Use a dummy node to handle edge cases • Move a pointer to the node just before left • Start reversing nodes one by one within the range • Adjust links to insert nodes at the front of the sublist • Reconnect the reversed portion with remaining list Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Partial reversal in linked lists requires precise pointer updates, not extra space. This builds strong intuition for advanced linked list problems. 🔥 Day 70 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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