๐ฏ Queue Variations
Regular queues are great, but sometimes you need more! Queue variations solve specific problems: Circular Queue for fixed buffers, Deque for sliding windows, Priority Queue for scheduling. Master these for advanced interviews!
๐ฏ Interview Alert - 3 Essential Variations!
Master these:
- โ Circular Queue - Fixed-size buffers, round-robin
- โ Deque - Sliding window maximum (very common!)
- โ Priority Queue - Dijkstra's, A*, scheduling
Variation 1: Circular Queue
Fixed Size, Wraps Around
Key feature: Rear wraps to beginning when it reaches end
class CircularQueue:
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
Use cases: Buffers, Round-robin scheduling
See Variations in Action
Variation 2: Deque (Double-Ended Queue)
Add/Remove from BOTH Ends
Operations:
- add_front(), add_rear()
- remove_front(), remove_rear()
from collections import deque
dq = deque()
dq.append(1) # Add to rear
dq.appendleft(0) # Add to front
dq.pop() # Remove from rear
dq.popleft() # Remove from front
Use cases: Sliding window, palindrome check
Variation 3: Priority Queue
Highest Priority First
import heapq
pq = []
heapq.heappush(pq, (priority, item))
item = heapq.heappop(pq) # Lowest value = highest priority
Use cases: Dijkstra's, A*, task scheduling
Application: Sliding Window Maximum
THE Deque Interview Question!
Problem: Find maximum in each window of size k
def sliding_window_max(arr, k):
dq = deque() # Stores indices
result = []
for i in range(len(arr)):
# Remove outside window
while dq and dq[0] < i - k + 1:
dq.popleft()
# Remove smaller elements
while dq and arr[dq[-1]] < arr[i]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(arr[dq[0]])
return result
Time: O(n), Space: O(k)
Key: Monotonic decreasing deque!
The Complete Code
Click Run to execute your code
Comparison Table
| Type | Add | Remove | Use Case |
|---|---|---|---|
| Regular Queue | Rear only | Front only | BFS, buffering |
| Circular Queue | Rear (wrap) | Front (wrap) | Fixed buffer |
| Deque | Both ends | Both ends | Sliding window |
| Priority Queue | By priority | Highest first | Dijkstra, scheduling |
Interview Tips
๐ก How to Ace Queue Variation Questions
- โ Circular Queue: Explain modulo wrapping
- โ Deque: Use collections.deque for O(1) both ends
- โ Priority Queue: Use heapq for O(log n)
- โ Sliding Window: Monotonic deque pattern!
- โ Know when to use each type
Summary
Queue Variations Mastery
- Circular Queue: Fixed size, wraps around
- Deque: Operations at both ends
- Priority Queue: Highest priority first
- Sliding Window: Monotonic deque (O(n))
- Choose wisely: Right tool for the job!
What's Next?
You've mastered queue variations! Next: Combined Problems - Advanced stack & queue challenges!
Enjoying these tutorials?