Queue
A queue is a linear data structure that follows First-In-First-Out (FIFO) ordering: elements are added at the rear (enqueue) and removed from the front (dequeue), so the first element inserted is the first one removed. Queues model waiting…
Definition
A queue is a linear data structure that follows First-In-First-Out (FIFO) ordering: elements are added at the rear (enqueue) and removed from the front (dequeue), so the first element inserted is the first one removed. Queues model waiting lines and are fundamental to task scheduling, breadth-first traversal, and buffering between producers and consumers.
Overview
A queue enforces a strict order of processing: whatever enters first exits first, mirroring a physical line of people waiting. Core operations are enqueue (add to the rear), dequeue (remove from the front), peek/front (view the front element without removing it), and isEmpty. All are O(1) when implemented with a proper structure such as a doubly linked list or a circular buffer over an array; a naive array implementation that shifts elements after dequeue costs O(n) per removal. Variants extend the basic FIFO queue for specific needs. A circular queue reuses array space by wrapping indices around, avoiding the need to shift elements. A deque (double-ended queue) allows insertion and removal from both ends, generalizing both stacks and queues. A priority queue, typically implemented with a heap, dequeues elements by priority rather than arrival order, breaking strict FIFO in favor of importance-based ordering. Queues underpin breadth-first search (BFS) on trees and graphs, where nodes are processed level by level. In systems design, message queues (Kafka, RabbitMQ, SQS) apply the same FIFO principle at scale to decouple producers and consumers, buffer bursts of traffic, and enable asynchronous processing between services. Operating systems use queues for process scheduling (ready queue) and I/O request buffering, and print spoolers, call centers, and rate limiters all model real-world queuing behavior directly. Understanding queues is also foundational for reasoning about concurrency: bounded queues with blocking behavior are a common pattern for coordinating producer and consumer threads safely.
Key Concepts
- First-In-First-Out (FIFO) ordering
- Core operations: enqueue, dequeue, peek, isEmpty, all O(1) when well-implemented
- Circular queue implementation avoids costly element shifting
- Deque variant supports insertion/removal from both ends
- Priority queue variant orders by importance instead of arrival time
- Backbone of breadth-first search traversal
- Scales to distributed message queues (Kafka, RabbitMQ, SQS)
Use Cases
Frequently Asked Questions
From the Blog
How JavaScript Really Works: Types, Scope, and Execution
JavaScript rests on three foundations: a value model that splits primitives from references, a lexical scope chain resolved before code runs, and a single-threaded event loop with a task queue. Understanding these three explains most of the language's surprising behaviour and the errors you actually hit.
Read More ProgrammingHow to debug an asyncio program that hangs
A hung async program is almost always awaiting something that will never complete, and you find it by dumping live task stacks rather than by reading code. Work through a fixed order: enable debug mode, dump tasks, then classify the wait as a lock, a queue, a missing timeout or a blocking call.
Read More ProgrammingThe JavaScript event loop: microtasks vs macrotasks
There are two queues with different draining rules: the microtask queue empties completely before the next task runs, which is why a promise chain always finishes before a zero-delay timeout. That one rule explains async ordering, starvation, and where rendering fits between tasks.
Read More Cloud & CybersecurityHow to manage vulnerable third-party dependencies
Turn a flood of advisories into a work queue: triaging by reachability, pinning with lockfiles, handling transitive pulls and keeping updates routine.
Read More