
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle: the first element inserted is the first one to be removed.
Queues model real-world scenarios where order matters, such as people waiting in line, tasks scheduled for execution, or requests handled by a server. Because of this natural ordering, queues often appear in problems involving simulation, scheduling, and streaming data.
The core operations of a queue are:
All these operations are expected to run in time with the right implementation.
A queue is usually the right tool when:
As we will see, this simple idea extends naturally to more advanced patterns such as monotonic queues, which allow efficient solutions to otherwise expensive problems. The position of insertion and removal is fixed, which enforces the FIFO ordering.
There are multiple ways to implement a queue, each with different trade-offs. The most straightforward implementation uses an array:
enqueue, push at the end of the arraydequeue, remove from the front of the arrayWhile enqueue is , removing from the front requires shifting elements, making dequeue O(n) in many languages.
This approach is simple but often inefficient.
class Queue<T> {
private data: (T | undefined)[];
private head = 0;
private tail = 0;
private size = 0;
constructor(private capacity: number) {
this.data = new Array(capacity);
}
enqueue(value: T): boolean {
if (this.size === this.capacity) return false;
this.data[this.tail] = value;
this.tail = (this.tail + 1) % this.capacity;
this.size++;
return true;
}
dequeue(): T | undefined {
if (this.size === 0) return undefined;
const value = this.data[this.head];
this.data[this.head] = undefined;
this.head = (this.head + 1) % this.capacity;
this.size--;
return value;
}
peek(): T | undefined {
return this.size === 0 ? undefined : this.data[this.head];
}
isEmpty(): boolean {
return this.size === 0;
}
}
Other common queue implementations include:
shift() causes element shifting and degrades performance to O(n) per dequeue.head and tail indices, reuses freed space, and treats the array as circular to guarantee O(1) enqueue and dequeue.A monotonic queue is a specialized type of queue that maintains its elements in a strictly increasing or decreasing order. Unlike a standard queue, where elements are only processed in FIFO order, a monotonic queue actively enforces an invariant: every new element added preserves the monotonic property, and elements that would violate the order are removed immediately.
There are two main types:
This invariant makes monotonic queues extremely powerful for problems involving sliding windows or range-based queries, such as computing the maximum or minimum over a moving window. Because each element is added and removed at most once, operations on a monotonic queue run in amortized time, even for sequences where naive approaches would be .
By carefully combining the FIFO behavior with the monotonic invariant, you can solve a wide range of problems efficiently without additional sorting or scanning steps.
Queues provide predictable performance characteristics that make them ideal for simulation, scheduling, and streaming-style problems. Understanding where each cost comes from is essential, especially when choosing between a simple queue and a monotonic queue. Standard queue operations are designed to work in constant time when implemented correctly (using a circular buffer, deque, or linked list). Monotonic queues add a small layer of logic on top, but still preserve linear-time behavior thanks to amortized analysis.
| Operation / Pattern | Time Complexity | Space Complexity |
|---|---|---|
| Enqueue | ||
| Dequeue | ||
| Peek (front) | ||
| Queue traversal | ||
| Simulation with queue | ||
| Sliding window (simple queue) | ||
| Monotonic queue operations | (amortized) | |
| Sliding window min/max |
| Problem | Technique | Solution |
|---|---|---|
| Number of Recent Calls | Queue | Solution |
| Time Needed to Buy Tickets | Queue | Solution |
| Reveal Cards In Increasing Order | Queue | Solution |
| Longest Continuous Subarray With Absolute Diff ≤ Limit | Monotonic Queue | Solution |
| Jump Game VI | Monotonic Queue | Solution |
| Sliding Window Maximum | Monotonic Queue | Solution |
| Max Value of Equation | Monotonic Queue | Solution |