š« Queue Basics
Imagine waiting in line at a store - first person in line gets served first! That's exactly how a Queue works. It's a FIFO (First In, First Out) data structure where the first element added is the first one removed. Queues power BFS, task scheduling, buffering, and more!
šÆ Why Queues Matter!
Queues are EVERYWHERE:
- ā BFS - Breadth-First Search (level-by-level)
- ā Task Scheduling - CPU, print queue, requests
- ā Buffering - IO, streaming, network packets
- ā Customer Service - First come, first served
- ā Async Processing - Message queues, job queues
All operations are O(1) - super efficient!
The FIFO Principle
First In, First Out
Think of a line at a store:
Enqueue (join line):
[Person 1] ā First in (at front)
[Person 2]
[Person 3] ā Last in (at rear)
Dequeue (serve):
Serve Person 1 first (first in, first out!)
Then Person 2
Then Person 3
Key insight: Add at rear, remove from front!
See It in Action
Queue Operations
Three Core Operations
1. Enqueue - Add to Rear
queue.enqueue(10) # Add 10 to rear
queue.enqueue(20) # Add 20 to rear
Time: O(1), Space: O(1)
2. Dequeue - Remove from Front
value = queue.dequeue() # Remove from front (10)
# Now front is 20
Time: O(1), Space: O(1)
3. Peek - View Front
front = queue.peek() # View front without removing
Time: O(1), Space: O(1)
Implementation: Circular Array
Why Circular?
Prevents wasted space! Front and rear wrap around.
class QueueArray:
def __init__(self, capacity):
self.items = [None] * capacity
self.front = 0
self.rear = -1
self.size = 0
def enqueue(self, item):
self.rear = (self.rear + 1) % self.capacity # Wrap!
self.items[self.rear] = item
self.size += 1
def dequeue(self):
item = self.items[self.front]
self.front = (self.front + 1) % self.capacity # Wrap!
self.size -= 1
return item
Key: Use modulo (%) for circular wrapping!
Queue vs Stack
| Aspect | Queue (FIFO) | Stack (LIFO) |
|---|---|---|
| Principle | First In, First Out | Last In, First Out |
| Add | Enqueue (rear) | Push (top) |
| Remove | Dequeue (front) | Pop (top) |
| Access Points | Two (front & rear) | One (top) |
| Use Case | BFS, scheduling | DFS, undo/redo |
The Complete Code
Click Run to execute your code
Real-World Applications
Where Queues Are Used
1. BFS (Breadth-First Search)
- Level-by-level traversal
- Shortest path in unweighted graphs
2. Task Scheduling
- CPU scheduling (Round Robin)
- Print queue
- Request handling
3. Buffering
- IO buffers
- Streaming data
- Network packets
Interview Tips
š” How to Ace Queue Questions
- ā Always mention FIFO!
- ā Circular array: Explain modulo wrapping
- ā All operations O(1)
- ā Queue vs Stack: Know differences
- ā Common follow-up: Implement using stacks
- ā Know BFS: Queue is essential!
Summary
Queue Basics Mastery
- FIFO Principle: First In, First Out
- Operations: Enqueue (rear), Dequeue (front), Peek (all O(1))
- Circular Array: Use modulo for wrapping
- Applications: BFS, scheduling, buffering
- vs Stack: FIFO vs LIFO, two access points vs one
What's Next?
You've mastered queue basics! Next: Queue Variations - Circular Queue, Deque, Priority Queue!
Enjoying these tutorials?