Queue
A queue is a First-In, First-Out (FIFO) data structure: items are added at the back (enqueue) and removed from the front (dequeue), like a line at a coffee shop.
Explanation
The two core operations are enqueue (add to the back) and dequeue (remove from the front), both O(1) in a proper implementation. A peek operation returns the front element without removing it. Queues enforce strict ordering: the first item added is always the first item processed. This fairness property is why queues appear everywhere fairness or temporal ordering matters. In JavaScript, you can simulate a queue with an array using push() for enqueue and shift() for dequeue — but shift() is O(n) because it has to re-index all elements. For production use with high throughput, implement a queue with a doubly linked list (O(1) front removal) or use a circular buffer with head/tail pointers. Real-world queues are everywhere in computing: the event queue in the browser's event loop holds pending callbacks and I/O events waiting to be processed; task queues in web workers batch jobs off the main thread; message queues (like RabbitMQ, AWS SQS) decouple producers from consumers in distributed systems; print spoolers queue jobs for printers; CPU schedulers queue processes waiting for processor time; BFS algorithms use a queue to explore nodes level by level. The priority queue variant breaks pure FIFO: items have a priority and higher-priority items dequeue first regardless of insertion order. Priority queues are implemented with a heap data structure and are the basis for Dijkstra's shortest-path algorithm and task schedulers that need to process high-priority jobs first.
Code Example
javascript// O(1) queue using a doubly-ended deque approach
class Queue {
#head = 0;
#data = {};
#tail = 0;
enqueue(val) { this.#data[this.#tail++] = val; }
dequeue() {
if (this.isEmpty()) return undefined;
const val = this.#data[this.#head];
delete this.#data[this.#head++];
return val;
}
peek() { return this.#data[this.#head]; }
isEmpty() { return this.#head === this.#tail; }
size() { return this.#tail - this.#head; }
}
// BFS using a queue — level-order traversal
function bfs(root) {
if (!root) return [];
const queue = new Queue();
const result = [];
queue.enqueue(root);
while (!queue.isEmpty()) {
const node = queue.dequeue();
result.push(node.val);
if (node.left) queue.enqueue(node.left);
if (node.right) queue.enqueue(node.right);
}
return result;
}
Why It Matters for Engineers
The event loop — the mechanism that makes JavaScript asynchronous — is fundamentally a queue. Every Promise.then callback, setTimeout callback, and I/O event sits in a queue waiting to be processed. When you understand queue semantics, you understand why certain callbacks execute in the order they do, and you can reason about timing without guessing. Message queues are one of the most important patterns in distributed systems design. When you need to decouple a service that produces work from a service that consumes it (or handle traffic spikes without dropping requests), a queue is the standard solution. This is system design interview and real-world architecture knowledge.
Related Terms
Learn This In Practice
Go deeper with the full module on Beyond Vibe Code.
Data Structures Fundamentals → →