Web Analytics

šŸš€ Combined Stack & Queue Problems

Advanced ~30 min read ⭐⭐⭐ FAANG Favorites

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

Output
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

  1. Queue using Stacks: O(1) amortized with two stacks
  2. Next Greater Element: Monotonic stack pattern
  3. Min Stack: Auxiliary stack for O(1) getMin
  4. Sliding Window Max: Monotonic deque
  5. Valid Parentheses: Stack matching
  6. 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! šŸš€