Queues

November 19, 2025
#queue #data-structures #fifo #algorithms #interviewing

Introduction

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. Think of it like a line at a coffee shop: the first person to join the line is the first person to be served. This fair ordering makes queues perfect for managing tasks, requests, and data that needs to be processed in the order it arrives.

Queues are everywhere in computing—from managing print jobs and handling network requests to implementing breadth-first search algorithms. By the end of this tutorial, you’ll understand how queues work, when to use them, and how to implement them efficiently.

If you’re new to time and space complexity, start with our guide to Big-O Notation. You may also want to review Arrays, Linked Lists, and Stacks to understand related data structures.

What is a queue?

A queue is a collection of elements with two principal operations:

  1. Enqueue: Add an element to the back (rear) of the queue
  2. Dequeue: Remove and return the element from the front of the queue

Additionally, most queue implementations include: 3. Peek (or Front): View the front element without removing it 4. IsEmpty: Check if the queue is empty 5. Size: Get the number of elements in the queue

The key constraint: elements are added at one end (rear) and removed from the other end (front), maintaining FIFO order.

Why use a queue?

Choose a queue when:

  1. You need FIFO (First-In-First-Out) processing
  2. You’re managing tasks or requests that should be handled in order
  3. You’re implementing breadth-first search (BFS) algorithms
  4. You need a buffer between producers and consumers (producer-consumer pattern)
  5. You’re scheduling processes or managing resources fairly

Avoid a queue when:

  • You need LIFO (Last-In-First-Out) access (use a stack instead)
  • You need random access to elements (use an array)
  • You need priority-based ordering (use a priority queue with a heap)

Real-world applications

Queues are used in many practical scenarios:

  • Task scheduling: Operating systems use queues to manage processes and threads
  • Print queue: Managing print jobs in the order they’re submitted
  • Web servers: Handling incoming HTTP requests in order
  • Call centers: Managing customer service calls on hold
  • Breadth-first search: Graph and tree traversal algorithms
  • Message queues: RabbitMQ, Kafka for distributed systems
  • Keyboard buffer: Storing keystrokes in the order they’re typed
  • Asynchronous processing: Background job processing

Queue vs Stack

Understanding the difference between queues and stacks is crucial:

Feature Queue Stack
Order FIFO (First-In-First-Out) LIFO (Last-In-First-Out)
Add operation Enqueue (at rear) Push (at top)
Remove operation Dequeue (from front) Pop (from top)
Use cases Task scheduling, BFS Function calls, DFS, undo/redo
Analogy Line at a store Stack of plates

Core operations and complexity

All primary queue operations are extremely efficient:

  • Enqueue: O(1) — add element to rear
  • Dequeue: O(1) — remove and return front element
  • Peek/Front: O(1) — view front element without removing
  • IsEmpty: O(1) — check if queue is empty
  • Size: O(1) — get number of elements
  • Space: O(n) — where n is the number of elements in the queue

Queue implementation

Queues can be implemented using arrays or linked lists. Array-based implementations are simpler but may require resizing. Linked list implementations are more flexible and never need resizing.

Array-based queue (dynamic)

Linked list-based queue

For optimal performance, implement queues using linked lists with both head and tail pointers:

Common queue problems

1. Breadth-First Search (BFS)

Use a queue to traverse a binary tree level by level:

2. Generate binary numbers from 1 to N

Use a queue to generate binary representations efficiently:

3. Implement a stack using two queues

Challenge: Implement stack operations using only queue operations:

Queue variations

  • Circular Queue: Uses a fixed-size array with wraparound to avoid wasting space
  • Deque (Double-ended queue): Allows insertion and deletion at both ends
  • Priority Queue: Elements are dequeued based on priority rather than arrival order (uses heaps)
  • Blocking Queue: Thread-safe queue that blocks when empty or full (used in concurrent programming)

Common pitfalls

  • Array dequeue is O(n): Using array shift() or pop(0) requires shifting elements. Use linked lists for true O(1) dequeue.
  • Forgetting to update rear: In linked list implementations, ensure the rear pointer is updated correctly
  • Queue underflow: Always check if the queue is empty before dequeuing
  • Memory leaks: In manual-memory languages, ensure dequeued nodes are properly freed
  • Not handling empty queue in peek: Verify the queue has elements before accessing the front

When to prefer other data structures

  • Need LIFO access → Use a stack
  • Need random access → Use an array
  • Need priority ordering → Use a priority queue (heap)
  • Need search operations → Use a hash table
  • Need sorted data → Use a binary search tree or sorted array

Practice problems

  1. Implement a circular queue with fixed capacity using an array
  2. Reverse the first K elements of a queue
  3. Implement a queue using a single stack
  4. Generate all perfect squares from 1 to N using a queue
  5. Use a queue to check if a binary tree is a complete binary tree
  6. Implement a recent counter that counts requests in the last 3000ms using a queue
  7. Design a system to handle requests with rate limiting using a queue
  8. Solve the “Hot Potato” problem (Josephus problem) using a queue
  9. Level order traversal of a tree with null markers between levels
  10. Find the first non-repeating character in a stream of characters using a queue

Complexity summary

All core queue operations are constant time:

  • Time complexity:
    • Enqueue: O(1)
    • Dequeue: O(1) (linked list) or O(n) (naive array)
    • Peek: O(1)
    • IsEmpty: O(1)
    • Size: O(1)
  • Space complexity: O(n) where n is the number of elements

This constant-time performance for enqueue and dequeue (with proper implementation) makes queues ideal for managing ordered data efficiently.

Thanks for visiting
We are actively updating content to this site. Thanks for visiting! Please bookmark this page and visit again soon.
Sponsor