Back to All Algorithms
Queue
Category: Data Structures | Time: O(1) | Space: O(n)
Visualization
Visual representation of the data structure
Enter commands to start queue visualization.
About Queue

A linear data structure that follows the First-In, First-Out (FIFO) principle. Elements are added (enqueued) at the rear and removed (dequeued) from the front, like a line of people.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Variable State
{
  "status": "Initializing queue"
}
Queue: How It Works
Last updated October 28, 2026

Overview

A queue is a First-In, First-Out (FIFO) collection: items are added at the rear and removed from the front, like a line at a ticket counter. The first item to arrive is always the first to be served.

Queues model anything that should be handled in arrival order: print jobs, network packets, requests to a web server, or messages between services. They are also the engine of breadth-first search, where the queue guarantees nodes are explored level by level.

The default commands enqueue 10 and 20, dequeue (removing 10, the oldest), enqueue 30 and 40, then dequeue again (removing 20). The final queue is [30, 40], with 30 at the front.

Step by step

  1. enqueue(x): add x at the rear.
  2. dequeue(): remove and return the front element; handle the empty case.
  3. peek(): look at the front element without removing it.
  4. isEmpty() / size(): check how many elements are waiting.

Complexity

CaseCostWhy
EnqueueO(1)Append at the rear.
DequeueO(1)With a linked list, circular buffer or head index; O(n) with Array.shift().
PeekO(1)Read the front element.
SpaceO(n)One slot per element.

When to use it

  • Breadth-first search and shortest paths in unweighted graphs.
  • Task scheduling and job processing in arrival order.
  • Buffers between a producer and a consumer running at different speeds.

Common mistakes

  • Implementing dequeue with Array.shift() in JavaScript moves every element, making it O(n).
  • A naive array queue that only moves the front index forward wastes the space behind it — the circular queue fixes this.
  • Forgetting the empty-queue check on dequeue.
Worked Example: Queue Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Queue: follow each row, predict the next one, and compare. The run below takes 14 steps in total. The Line column refers to the line numbers in the code listing above.

Input
enqueue 10,enqueue 20,dequeue,enqueue 30,enqueue 40,dequeue
Result
status = Operations complete, final_queue = [30, 40]
StepLineArrayWhat happens
12[]status = Initializing queue
24[]action = enqueue(10)
35[10]status = Enqueued 10, front = 10, rear = 10
44[10]action = enqueue(20)
55[10, 20]status = Enqueued 20, front = 10, rear = 20
67[10, 20]action = dequeue()
711[20]status = Dequeued 10, front = 20, rear = 20
84[20]action = enqueue(30)
95[20, 30]status = Enqueued 30, front = 20, rear = 30
104[20, 30]action = enqueue(40)
115[20, 30, 40]status = Enqueued 40, front = 20, rear = 40
127[20, 30, 40]action = dequeue()
1311[30, 40]status = Dequeued 20, front = 30, rear = 40
1418[30, 40]status = Operations complete, final_queue = [30, 40]
Frequently Asked Questions
About Queue

What is a Queue?

A Queue is a linear data structure that follows the **First-In, First-Out (FIFO)** principle. Like a real-world queue or line, the first element added to the queue will be the first one to be removed.

What are the primary operations of a Queue?

The main operations are:
  • Enqueue: Adds an element to the rear (end) of the queue.
  • Dequeue: Removes an element from the front of the queue.
  • Front (or Peek): Returns the first element without removing it.
  • isEmpty: Checks if the queue is empty.

What is the time complexity of queue operations?

When implemented efficiently (e.g., with a linked list or a dynamic array that handles resizing well), `enqueue` and `dequeue` operations have an amortized time complexity of O(1).

Where are queues used in computing?

Queues are used extensively:
  • Operating Systems: For managing processes in CPU scheduling.
  • Networking: To handle data packets in order.
  • Printers: To manage print jobs in the order they were received.
  • Breadth-First Search (BFS): The algorithm uses a queue to explore graph nodes level by level.

How can you implement a Queue?

A queue can be implemented using an array, but this can be inefficient for `dequeue` (which takes O(n) if you shift all elements). A better approach is to use a Linked List, where adding to the tail and removing from the head are both O(1) operations.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.
Related Interview Questions
Practice with real problems