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:
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.
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.
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.
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.
If we enqueue 4, it gets added at the rear.
Our queue now contains 1, 2, 3, and 4.
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.
Peek, sometimes called front, lets us see the front element without removing it.
So peek would return 2, but our queue stays unchanged.
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.
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.
In terms of space, a queue takes O(n), where n is the number of elements it stores.
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.
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.
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.
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.
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.rear and advance it using modulo with the array's capacity.front pointer the same way.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.
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:
head and tail point to it.tail forward.head to the next one.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.
tail. If the queue is empty, both head and tail point to the new node.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.
Both implementations give O(1) enqueue and dequeue. The difference is in how they use memory.
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.
Now comes an important question for coding interviews: when should you use a queue?
There are a few common patterns worth recognizing.
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.
And that's how queues work.
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.
10 quizzes