๐ Combined Stack & Queue Problems
Time to combine everything! These advanced problems use stacks and queues together to solve complex challenges. Master Queue using Stacks, Next Greater Element, Min Stack - THE most common stack/queue interview questions at Google, Amazon, Microsoft!
๐ฏ Interview Alert - Top 6 Problems!
These appear in 90% of stack/queue interviews:
- โ Queue using Stacks - O(1) amortized (Very Common!)
- โ Next Greater Element - Monotonic stack (Essential!)
- โ Min Stack - O(1) getMin (Classic!)
- โ Sliding Window Maximum - Monotonic deque
- โ Valid Parentheses - Stack matching
- โ Stack using Queues - Design problem
Problem 1: Queue using Two Stacks โญโญโญ
THE Most Common Design Question!
Challenge: Implement queue using only stacks
class QueueUsingStacks:
def __init__(self):
self.stack1 = [] # For enqueue
self.stack2 = [] # For dequeue
def enqueue(self, item):
self.stack1.append(item) # O(1)
def dequeue(self):
if not self.stack2:
# Transfer from stack1 to stack2
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop() # O(1) amortized
Time: O(1) amortized for both operations!
Key insight: Transfer only when stack2 is empty
See Problems in Action
Problem 2: Next Greater Element โญโญโญ
Monotonic Stack Pattern!
Challenge: Find next greater element for each element
def next_greater_element(arr):
result = [-1] * len(arr)
stack = [] # Stores indices
for i in range(len(arr)):
# Pop smaller elements
while stack and arr[stack[-1]] < arr[i]:
idx = stack.pop()
result[idx] = arr[i]
stack.append(i)
return result
Example: [4, 5, 2, 25] โ [5, 25, 25, -1]
Time: O(n), Space: O(n)
Problem 3: Min Stack โญโญ
O(1) getMin!
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = [] # Tracks minimums
def push(self, item):
self.stack.append(item)
if not self.min_stack or item <= self.min_stack[-1]:
self.min_stack.append(item)
def pop(self):
item = self.stack.pop()
if item == self.min_stack[-1]:
self.min_stack.pop()
return item
def get_min(self):
return self.min_stack[-1] # O(1)!
All operations O(1)!
The Complete Code
Click Run to execute your code
Interview Tips
๐ก How to Ace Combined Problems
- โ Queue using Stacks: Explain amortized O(1)
- โ Next Greater: Monotonic stack is KEY!
- โ Min Stack: Auxiliary stack pattern
- โ Practice these 6 problems - they're VERY common!
- โ Know time complexities - interviewers will ask!
Summary
Combined Problems Mastery
- Queue using Stacks: O(1) amortized with two stacks
- Next Greater Element: Monotonic stack pattern
- Min Stack: Auxiliary stack for O(1) getMin
- Sliding Window Max: Monotonic deque
- Valid Parentheses: Stack matching
- These are FAANG favorites! Practice them!
๐ Congratulations - Module 6 Complete!
You've mastered Stacks & Queues!
What you learned:
- โ Stack Basics (LIFO, operations)
- โ Stack Applications (balanced parentheses, expression eval)
- โ Queue Basics (FIFO, circular queue)
- โ Queue Variations (deque, priority queue)
- โ Combined Problems (advanced interview questions)
You're now ready for FAANG stack/queue interviews! ๐
Enjoying these tutorials?