Welcome back, coding explorer!
Last week, we traveled through Linked Lists, where data moves like a train; connected, flexible, and dynamic.
This week in DSA Bites, we’ll explore two everyday heroes of computer science Stacks and Queues, the structures that help us manage order and sequence in data processing.
We’ll go from simple to practical, and see how these concepts appear all around us,from your browser’s back button to task scheduling on your phone.
Part 1: What is a Stack?
A Stack works on the principle of LIFO: Last In, First Out.
Think of a stack of plates, the last plate you put on top is the first one you take off.
Analogy:
Adding data = Push
Removing data = Pop
Looking at the top = Peek
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
print(stack.pop()) # removes 30
let stack = [];
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.pop()); // removes 30
Output:
30
Operations and Complexity:
Operation | Description | Time |
|---|---|---|
Push | Add item to top | O(1) |
Pop | Remove top item | O(1) |
Peek | View top without removing | O(1) |
Part 2: Uses of Stacks
Stacks appear everywhere once you notice them 👀
1. Browser Back Button
When you visit pages, each URL is pushed onto a stack.
Clicking “Back” pops the last one and shows the previous page.
2. Undo / Redo in Word Processors
Each action is stored in a stack. Undo pops the last action, redo pushes it back.
3. Expression Evaluation (Compiler)
Compilers use stacks to manage parentheses and operator precedence during parsing.
4. Recursion Calls
Every time you call a function recursively, it’s pushed to the call stack. Once done, it pops off.
Part 3: What is a Queue?
A Queue follows FIFO: First In, First Out.
The first person to get in line is served first.
Analogy:
Think of a queue at Starbucks, the first customer to order is the first to get coffee.
from collections import deque
queue = deque()
queue.append(10)
queue.append(20)
queue.append(30)
print(queue.popleft()) # removes 10
let queue = [];
queue.push(10);
queue.push(20);
queue.push(30);
console.log(queue.shift()); // removes 10
Output:
10
Operations and Complexity:
Operation | Description | Time |
|---|---|---|
Enqueue | Add item to rear | O(1) |
Dequeue | Remove front item | O(1) |
Peek | View front item | O(1) |
Part 4: Uses of Queues
Queues are everywhere in systems that need order and fairness.
1. Print Jobs
Printers use queues to ensure documents print in the order they were sent.
2. Task Scheduling
Operating systems use queues to schedule CPU tasks , the first task enters first, gets served first.
3. Customer Service Systems
Tickets, messages, or chat requests are handled in queue order.
4. Breadth-First Search (BFS)
In graph traversal, BFS uses a queue to explore nodes level by level.
Part 5: Variants You’ll Meet
Type | Description | Example Use |
|---|---|---|
Circular Queue | Reuses space efficiently after dequeues | Buffers in routers |
Priority Queue | Each item has a priority | Task scheduling |
Deque (Double-Ended Queue) | Insert/remove from both ends | Palindrome checkers |
Monotonic Stack | Keeps elements in sorted order | Finding “next greater element” |
Part 6: Common Interview Patterns
Valid Parentheses (using Stack)
stack = []
for ch in "()()":
if ch == '(':
stack.append(ch)
elif stack and stack[-1] == '(':
stack.pop()
else:
print("Invalid")
print("Valid" if not stack else "Invalid")
Reverse a String (using Stack)
function reverseString(str) {
let stack = [];
for (let ch of str) stack.push(ch);
let reversed = '';
while (stack.length) reversed += stack.pop();
return reversed;
}
Implement a Queue using Two Stacks
Classic interview problem , use one stack for enqueue, another for dequeue.
Sliding Window Maximum (using Deque)
Used in performance monitoring or sensor readings , maintain max in each window efficiently.
Part 7: Mini Challenges
1️⃣ Implement a Stack using a Linked List
➡️ Input: Push 10, 20, 30 → Pop
➡️ Output: 30
2️⃣ Check Balanced Parentheses
➡️ Input: “(a+b) + (c+d)”
➡️ Output: Valid
3️⃣ Reverse a Queue
➡️ Input: 1 → 2 → 3 → 4
➡️ Output: 4 → 3 → 2 → 1
4️⃣ Implement Circular Queue
➡️ Input: Enqueue 1,2,3 → Dequeue → Enqueue 4
➡️ Output: Queue: 2,3,4
Expert Challenge:
Design a Queue that supports O(1) time for getMin()
(Hint: Maintain an auxiliary queue to track minimums.)
🤖 AI Tip
Try asking AI:
“Generate 5 visualization examples showing how the call stack changes during recursion or how a queue processes print jobs in order.”
Visualizing stacks and queues helps you truly “see” how order flows through your programs.
✅ Sign-Off
That’s your Stacks & Queues Bite , from Real Life to Code! 🎉
You now know how these data structures manage order behind the scenes, from your browser history to your CPU scheduler.
Next up: Trees 🌳
Where data starts to branch out , literally.
Keep coding and stay curious,
Fahim | DSA Bites