Web Analytics

šŸŽ« Queue Basics

Beginner to Intermediate ~20 min read ⭐ Essential

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

Output
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

  1. FIFO Principle: First In, First Out
  2. Operations: Enqueue (rear), Dequeue (front), Peek (all O(1))
  3. Circular Array: Use modulo for wrapping
  4. Applications: BFS, scheduling, buffering
  5. 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!