š 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?