Skip to main content

🎟️ Queue

One-line summary: First-in first-out (FIFO) β€” the backbone of BFS, level-order traversal, and rate-limiting.


Concept​

A queue supports:

  • enqueue(x) β€” add to rear: O(1)
  • dequeue() β€” remove from front: O(1)
  • peek() β€” read front: O(1)

Variants: circular buffer (fixed-size), deque (double-ended), monotonic deque (sliding window max).


Diagram​

Queue Circular Buffer Queue GIF


Time & Space Complexity​

OperationTimeSpace
Enqueue / DequeueO(1)O(1)
BFS traversalO(V+E)O(V)

Common Patterns​

BFS Level Order​

function levelOrder(root) {
if (!root) return [];
const result = [],
queue = [root];
while (queue.length) {
const level = [],
size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
level.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(level);
}
return result;
}

Monotonic Deque (Sliding Window Max)​

function maxSlidingWindow(nums, k) {
const deque = [],
result = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length && deque[0] < i - k + 1) deque.shift();
while (deque.length && nums[deque[deque.length - 1]] < nums[i]) deque.pop();
deque.push(i);
if (i >= k - 1) result.push(nums[deque[0]]);
}
return result;
}

Pitfalls​

  • JS array.shift() is O(n) β€” for performance use a pointer-based deque or linked list
  • BFS: process all nodes at current level before moving to next (use size snapshot)

Practice Problems​

Easy Problems​

ProblemDifficultySolution
LC 225 β€” Implement Stack using QueuesEasy
LC 232 β€” Implement Queue using StacksEasy
LC 346 β€” Moving Average from Data StreamEasy
LC 637 β€” Average of Levels in Binary TreeEasy
LC 933 β€” Number of Recent CallsEasy
LC 1700 β€” Number of Students Unable to Eat LunchEasy
LC 1971 β€” Find if Path Exists in GraphEasy
LC 2073 β€” Time Needed to Buy TicketsEasy
CC β€” Queue Operations (QUEUEOP)Easy
CC β€” Basic Queue (BASICQUE)Easy
CC β€” Queue Implementation (QUEIMPL)Easy

Medium Problems​

ProblemDifficultySolution
LC 102 β€” Binary Tree Level Order TraversalMedium
LC 103 β€” Binary Tree Zigzag Level OrderMedium
LC 107 β€” Binary Tree Level Order Traversal IIMedium
LC 116 β€” Populating Next Right Pointers in Each NodeMedium
LC 117 β€” Populating Next Right Pointers in Each Node IIMedium
LC 199 β€” Binary Tree Right Side ViewMedium
LC 200 β€” Number of IslandsMedium
LC 286 β€” Walls and GatesMedium
LC 429 β€” N-ary Tree Level Order TraversalMedium
LC 515 β€” Find Largest Value in Each Tree RowMedium
LC 542 β€” 01 MatrixMedium
LC 622 β€” Design Circular QueueMedium
LC 641 β€” Design Circular DequeMedium
LC 649 β€” Dota2 SenateMedium
LC 752 β€” Open the LockMedium
LC 909 β€” Snakes and LaddersMedium
LC 950 β€” Reveal Cards in Increasing OrderMedium
LC 994 β€” Rotting OrangesMedium
LC 1091 β€” Shortest Path in Binary MatrixMedium
LC 1162 β€” As Far from Land as PossibleMedium
LC 1161 β€” Maximum Level Sum of a Binary TreeMedium
LC 1302 β€” Deepest Leaves SumMedium
LC 1609 β€” Even Odd TreeMedium
LC 1823 β€” Find the Winner of the Circular GameMedium
LC 1926 β€” Nearest Exit from Entrance in MazeMedium
CC β€” Processing a Queue (QUEUE2)Medium
CC β€” Circular Queue (CIRCQUE)Medium
CC β€” BFS Problems (BFSPROB)Medium
CC β€” Level Order Traversal (LEVELORD)Medium

Hard Problems​

ProblemDifficultySolution
LC 126 β€” Word Ladder IIHard
LC 127 β€” Word LadderHard
LC 239 β€” Sliding Window MaximumHard
LC 317 β€” Shortest Distance from All BuildingsHard
LC 407 β€” Trapping Rain Water IIHard
LC 815 β€” Bus RoutesHard
LC 847 β€” Shortest Path Visiting All NodesHard
LC 864 β€” Shortest Path to Get All KeysHard
LC 1036 β€” Escape a Large MazeHard
LC 1263 β€” Minimum Moves to Move a Box to Their Target LocationHard
LC 1293 β€” Shortest Path in a Grid with Obstacles EliminationHard
LC 1345 β€” Jump Game IVHard
LC 1368 β€” Minimum Cost to Make at Least One Valid Path in a GridHard
LC 1728 β€” Cat and Mouse IIHard
CC β€” Advanced BFS (ADVBFS)Hard
CC β€” Multi-source BFS (MULTIBFS)Hard
CC β€” Bidirectional BFS (BIBFS)Hard

← Back to Home Β· Β© sparshjaswal