đ Level 1: The Basics (Start Here!)
What is a Stack? Think of a Stack of Plates! đŊī¸
Imagine a stack of plates in a cafeteria. You can only take the top plate, and when you add a new plate, it goes on top. That's exactly how a stack works - Last In, First Out (LIFO)!
Stack Operations:
- đĨ Push - Add to the top
- đ¤ Pop - Remove from the top
- đ Peek/Top - Look at the top without removing
- â isEmpty - Check if stack is empty
What is a Queue? Think of a Line at a Store! đ
A queue is like waiting in line at a grocery store. First person in line gets served first. That's First In, First Out (FIFO)!
â ī¸ Common Mistake #1: Stack Overflow
Why it happens: Each function call adds to the call stack. Without a base case, the stack grows until memory runs out!
â The Right Way
đŽ Try It Yourself: Stack Operations
Practice push and pop operations on a visual stack!
đ¤ Why Should You Care About Stacks & Queues?
Browser Back Button
Your browser history is a stack! When you click back, you pop the current page and return to the previous one.
Undo/Redo Operations
Every Ctrl+Z (undo) pops an action from the stack. Ctrl+Y (redo) pushes it to another stack!
Call Center Queue
Customer service calls are handled in a queue - first caller gets helped first (FIFO).
Print Queue
Documents sent to a printer wait in a queue. First document sent prints first!
Game Matchmaking
Players waiting for a match are in a queue. First to join gets matched first!
App Navigation
Mobile app screens use a stack. Press back to pop the current screen!
đ¨ Level 2: Common Patterns
Pattern 1: Balanced Parentheses (Stack)
Check if brackets are properly matched - a classic stack problem!
How it Works
Pattern 2: BFS with Queue
Breadth-First Search explores level by level using a queue!
đĒ Practice Problems (From Easy to Hard)
Problem 1: Implement Min Stack
Design a stack that supports push, pop, and retrieving the minimum element in O(1) time.
Think About It First! đ¤
Hint: What if you kept track of the minimum at each level?
Show Solution
Problem 2: Implement Queue using Stacks
Create a queue using only two stacks.
Strategy Hint đĄ
One stack for input, one for output. Transfer when needed!
Show Solution
đ Level 3: Advanced Techniques
Monotonic Stack
A stack where elements are always in increasing or decreasing order!
Circular Queue
A queue that wraps around - efficient use of fixed space!
đ Quick Reference Guide
Stack vs Queue Operations
Operation | Stack (LIFO) | Queue (FIFO) | Time |
---|---|---|---|
Add Element | Push (top) | Enqueue (rear) | O(1) |
Remove Element | Pop (top) | Dequeue (front) | O(1) |
View Next | Peek/Top | Front | O(1) |
Check Empty | isEmpty | isEmpty | O(1) |
đ¯ When to Use Stack
- â Matching parentheses
- â Function calls (recursion)
- â Undo operations
- â Expression evaluation
- â Backtracking algorithms
đ¯ When to Use Queue
- â BFS traversal
- â Task scheduling
- â Message passing
- â Handling requests
- â Level-order traversal
đ§ Common Patterns
- Monotonic Stack: Next greater/smaller
- Min/Max Stack: Track extremes
- Double-ended Queue: Sliding window
- Priority Queue: Heap implementation