AlgoMaster Logo

Introduction to Queues

High Priority10 min readUpdated November 9, 2026
Listen to this chapter
Unlock Audio

Introduction to Queues

A queue is one of the most fundamental data structures in computer science.

It follows a simple rule called FIFO: First In, First Out.

The first element you add to a queue is always the first one to come out.

Loading simulation...

Despite being simple, queues are used everywhere in real-world systems:

  • Operating systems use them to schedule tasks.
  • Web servers use queues to manage incoming requests.
  • Graph algorithms like Breadth-First Search rely on queues to explore nodes level by level.

And queues are equally important in coding interviews. Many problems become much easier once you recognize that a queue is the right data structure to use.

In this chapter, we'll understand what a queue is and how it works.

We'll look at its core operations and their time complexities, and explore different ways to implement a queue in code.

And finally, we'll learn how to recognize problems where queues can help us.

What is a Queue?

Think about people waiting in line at a coffee shop.

Whenever a new customer arrives, they join the back of the line. And the barista serves customers from the front.

The person who arrived first gets served first.

A queue works in exactly the same way. It is a linear data structure where elements are added at one end, called the rear, and removed from the other end, called the front.

This makes queues useful whenever the order of arrival matters.

Queue Operations

Now that we understand the basic idea, let's look at how we actually work with a queue.

There are four main operations you should know.

1. Enqueue

Enqueue, sometimes called offer or add, simply adds a new element to the back of the queue.

For example, suppose our queue contains 1, 2, and 3, with 1 at the front.

1
front
2
3
rear

If we enqueue 4, it gets added at the rear.

Our queue now contains 1, 2, 3, and 4.

1
front
2
3
4
rear

2. Dequeue

Dequeue, sometimes called poll or remove, removes and returns the element at the front.

So if we dequeue from our current queue, we get back 1.

And the remaining queue contains 2, 3, and 4.

2
front
3
4
rear

3. Peek

Peek, sometimes called front, lets us see the front element without removing it.

So peek would return 2, but our queue stays unchanged.

4. isEmpty

isEmpty tells us whether the queue contains any elements.

For example, if we keep dequeuing elements, 2, then 3, then 4, eventually the queue becomes empty.

At that point, isEmpty returns true.

This is especially useful when we need to process elements until there is nothing left. It also protects us from dequeuing an empty queue, which is an error.

Time and Space Complexity

The nice thing about queues is that all four operations can run in O(1) time.

That's because we only need to access or update the front or the rear. There's no need to search through the queue or shift elements around.

Operation

Time Complexity

enqueue

O(1)

dequeue

O(1)

peek

O(1)

isEmpty

O(1)

In terms of space, a queue takes O(n), where n is the number of elements it stores.

Queue Implementation

Now, let's see how we can implement a queue ourselves.

There are two common approaches. We can use an array or a linked list.

Let's start with the array-based implementation.

1. Using an Array

The basic idea is simple. We add elements at the rear and remove them from the front.

But there's a problem with a straightforward array implementation.

If we remove the first element and shift everything else one position to the left, each dequeue takes O(n) time. That's inefficient, especially for large queues.

A better approach is to use a circular array, also called a circular buffer.

This allows us to enqueue and dequeue without shifting elements.

How a Circular Buffer Works

Imagine an array with six slots. We keep two indices:

  • front is the index of the next element to remove.
  • rear is the index where the next element will be inserted.

We enqueue 10, 20, 30, and 40. The last element sits at index 3, so rear now points to index 4, the next free slot.

Next, we dequeue 10. Instead of shifting the remaining elements, we simply move front to index 1.

Now index 0 is available again.

We enqueue 50 and 60, filling the remaining slots at the end.

But what happens when we enqueue 70?

Instead of running out of space, we wrap around to index 0 and reuse that empty slot.

In the visualization below, yellow marks front and orange marks rear. Empty slots are shown as null.

1Empty queue of capacity 6. front = 0, rear = 0
-
0
front
rear
-
1
-
2
-
3
-
4
-
5
2Enqueue 10, 20, 30, 40. front = 0, rear = 4
10
0
front
20
1
30
2
40
3
-
4
rear
-
5
3Dequeue 10. front moves to 1, index 0 is free
-
0
20
1
front
30
2
40
3
-
4
rear
-
5
4Enqueue 50, 60. rear wraps from 5 to 0
-
0
rear
20
1
front
30
2
40
3
50
4
60
5
5Enqueue 70 into index 0. The queue is full
70
0
20
1
front
rear
30
2
40
3
50
4
60
5

In the last frame, the array reads [70, 20, 30, 40, 50, 60] from left to right, but the queue still holds 20, 30, 40, 50, 60, 70 in FIFO order, starting at front.

We do this wraparound using modular arithmetic:

That's the key idea behind a circular buffer: once we reach the end, we loop back to the beginning.

Code

Let's see how to implement this in code.

We use an array to store the elements, along with front and rear pointers.

  • front tells us where the next element will be removed.
  • rear helps us keep track of where to insert the next element.
  • When we enqueue, we insert at rear and advance it using modulo with the array's capacity.
  • When we dequeue, we read the front element and advance the front pointer the same way.
  • We also track the number of elements in size. When the queue is empty and when it is full, front and rear point to the same index, so size is what tells the two cases apart.

With this approach, both operations take O(1) time for a fixed-capacity circular buffer.

The trade-off is that the capacity is fixed. Once the buffer is full, enqueue fails until something is dequeued. Built-in queues like Java's ArrayDeque solve this by copying everything into a bigger array when they run out of room.

2. Using a Linked List

Now let's look at the linked-list implementation.

Here, we maintain two pointers: head and tail.

head points to the front of the queue, while tail points to the rear.

Here's how it works step by step:

  1. Enqueue 1. Since it's the only element, both head and tail point to it.
  2. Enqueue 2. We connect the new node to the current tail and move tail forward.
  3. Enqueue 3. This works the same way.
  4. Dequeue. We remove the head node and move head to the next one.
tail
1
head
null
1Enqueue 1: head and tail both point to 1
1 / 4

Since we're only updating pointers, both enqueue and dequeue take O(1) time.

Here's what the implementation looks like.

We maintain head and tail pointers to track both ends of the queue.

  • For enqueue, we create a new node. If the queue already contains elements, we connect the current tail to the new node and update tail. If the queue is empty, both head and tail point to the new node.
  • For dequeue, we first check whether head is null. If it is, the queue is empty, so we throw an error. Otherwise, we save the front value and move head to the next node. If that was the last element, we also set tail to null. Finally, we return the removed value.

Setting tail to null matters. Without it, tail would still point to the removed node, and the next enqueue would link the new node to a node that is no longer in the queue.

Array vs Linked List

Both implementations give O(1) enqueue and dequeue. The difference is in how they use memory.

Scroll

 

Circular Array

Linked List

enqueue / dequeue

O(1)

O(1)

Size

Fixed, unless you resize

Grows and shrinks as needed

Memory per element

Just the value

Value plus a next pointer

Memory layout

Contiguous, cache-friendly

Nodes spread across memory

Built-in Queues

In practice, you usually don't need to implement queues from scratch.

Most programming languages already provide built-in implementations.

In Java, a great default choice is ArrayDeque, which implements the Queue interface.

We create a queue and use offer to add elements. peek returns the front element without removing it. poll removes and returns the front element. And isEmpty tells us whether the queue is empty.

Under the hood, ArrayDeque uses a resizable circular array, giving us amortized O(1) insertion and removal at either end.

You might wonder why we don't just use LinkedList. That works too, but it creates a separate node for every element, which adds memory overhead.

For most cases, ArrayDeque is the better default.

In Rust, the standard library's VecDeque plays the same role: push_back adds at the rear and pop_front removes from the front, both in amortized O(1) time.

Unless an interviewer specifically asks you to implement a queue, it's usually better to use the standard library.

When to Use a Queue

Now comes an important question for coding interviews: when should you use a queue?

There are a few common patterns worth recognizing.

  1. Breadth-First Search (BFS). Whenever a problem asks you to explore a tree or graph level by level, or find the shortest path in an unweighted graph, a queue is often the right choice.
  2. Processing tasks in arrival order. Think of task schedulers, print queues, or systems that process incoming requests. New tasks enter at the back, while workers take tasks from the front.
  3. Producer-consumer problems. Here, one part of a system produces data while another processes it. A queue acts as a buffer between them.
  4. Sliding window problems. Queues are useful when we need to maintain the elements of a window in a particular order as it moves.

For many of these problems, we use a deque, or double-ended queue, which supports efficient insertion and removal at both ends. Java's ArrayDeque, Python's deque, C++'s std::deque, and Rust's VecDeque are all deques, so the same class covers both cases.

Summary

And that's how queues work.

  • A queue follows FIFO: the first element added is the first one removed.
  • Elements join at the rear and leave from the front.
  • enqueue, dequeue, peek, and isEmpty all run in O(1) time, and the queue takes O(n) space.
  • You can implement it with a circular array (fixed capacity, no shifting) or a linked list (grows freely, extra pointer per node).
  • In practice, use your language's built-in option, like ArrayDeque in Java, deque in Python, or std::queue in C++.

To really understand these concepts, I'd recommend implementing a basic queue yourself using both a circular array and a linked list.

Working through enqueue, dequeue, and pointer updates will help you understand what's happening behind the scenes.

Quiz

Introduction to Queue Quiz

10 quizzes