CMD Guide
HomeDSAQueues

Types of Queue

A queue's variations exist because the plain array-based FIFO queue wastes memory or lacks flexibility, and each variant fixes one specific weakness by changing how the front/rear pointers move or how the next element to leave is chosen.

Recognize the pattern

These three — Simple/Circular, Deque, Priority — are the canonical DSA queue variants (the ones CLRS and standard interview taxonomies test). You may also hear practitioners talk about an "affinity queue" in scheduler/systems design (grouping tasks by key/thread/tenant for cache locality); that is a scheduling concept built on top of a queue, not a distinct core ADT, so it will not show up as a named data structure in a DSA problem set.

Circular Queue: brute force → optimal

Brute force (linear queue on an array): dequeue removes index 0, then every remaining element is shifted left by one so front stays at index 0. Cost: O(n) per dequeue, and slots freed at the front are never reused unless you shift — usable capacity effectively shrinks as enqueue/dequeue cycles continue.

Optimal (circular queue): track front and rear indices and advance them with modulo arithmetic, idx = (idx + 1) % capacity, so the array wraps and freed slots are reused without shifting any data. Cost: O(1) per enqueue/dequeue.

Complexity, derived

Let capacity = n. Each enqueue performs a fixed number of steps: one full-check, one array write, one modulo increment of rear — independent of n, so O(1) time per enqueue and dequeue. The linear (non-wrapping) queue traced above needed to shift every remaining element on each dequeue, an O(n) operation — that O(n) cost is paid on every single dequeue, not amortized away, which is exactly what the circular queue's modulo trick eliminates.

Space is O(n) for the fixed backing array in both designs. The difference is not total memory but usable capacity: the linear queue's freed front slots (index 0, 1, ...) sit unreachable until a shift or a fresh array is allocated, while the circular queue's % capacity arithmetic makes every one of the n slots reusable indefinitely, as the traced example below shows.

Deque operations (push/pop front or rear) are also O(1) with a doubly linked list or a resizable circular array. Priority queue insert/extract-min on a binary heap of size n costs O(log n) time (sift up/down touches one path root-to-leaf, height = log n) and O(n) space.

Traced example: circular queue, capacity 5

OpfrontrearsizeArray state (index 0..4)
init0-10[_, _, _, _, _]
enqueue(10)001[10, _, _, _, _]
enqueue(20)012[10, 20, _, _, _]
enqueue(30)023[10, 20, 30, _, _]
dequeue() → 10122[_, 20, 30, _, _]
enqueue(40)133[_, 20, 30, 40, _]
enqueue(50)144[_, 20, 30, 40, 50]
enqueue(60)105 (full)[60, 20, 30, 40, 50] — rear wrapped to slot 0, reusing it

Without wraparound, slot 0 would sit empty forever after the first dequeue even though the queue is logically not full. Note that the queue is full only once size == capacity (here, 5 elements at indices 0-4) — front and rear positions alone don't tell you full-vs-empty, which is why the code below tracks size explicitly.

Java: circular queue core

import java.util.NoSuchElementException;

class CircularQueue {
    private final int[] data;
    private int front = 0, rear = -1, size = 0;
    CircularQueue(int capacity) { data = new int[capacity]; }

    boolean enqueue(int val) {
        if (size == data.length) return false; // full
        rear = (rear + 1) % data.length;
        data[rear] = val;
        size++;
        return true;
    }

    int dequeue() {
        if (size == 0) throw new NoSuchElementException();
        int val = data[front];
        front = (front + 1) % data.length;
        size--;
        return val;
    }
}

Pitfalls

When to use / when not

TypeUse whenAvoid whenvs. alternative
Circular QueueFixed-size buffer, high-throughput enqueue/dequeue (ring buffers, producer-consumer)Size is unbounded/unpredictablevs. linked-list queue: better cache locality & no per-node allocation, but fixed capacity
DequeNeed both-end access (sliding window, undo/redo)Only need one-end access — extra API surface is unneeded complexityvs. plain array list: inserting/removing at the front of an array list is O(n) due to shifting; a deque (doubly linked list or circular buffer) gives O(1) at both ends
Priority QueueNext-to-process is defined by priority, not arrival (Dijkstra, task schedulers)Order of arrival must be preservedvs. sorted array: heap gives O(log n) insert vs O(n) insert into a sorted array

Three quick recognition checks: sliding-window maximum specifically wants a monotonic deque of indices, not a plain FIFO queue; Dijkstra wants a priority queue (with decrease-key, or lazy deletion of stale entries); and an audio/network ring buffer is the circular queue's home turf because a fixed-capacity ring does zero allocation in steady state. If the problem is about reversal or nesting rather than arrival order, that's a stack — not a queue variant at all.

Takeaways

Recall: A circular queue has capacity 5, and is currently full (size == 5) with front = 1 and rear = 0 (rear has wrapped around). After one dequeue frees a slot, what index does the next enqueue write to, and why?


Synthesized from the source notes on queue variations, standard CLRS/GeeksforGeeks treatment of circular buffers, and standard heap-based priority queue analysis.

🤖 Don't fully get this? Learn it with Claude

Stuck on Types of Queue? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Types of Queue** (DSA) and want to truly understand it. Explain Types of Queue from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Types of Queue** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Types of Queue** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Types of Queue** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes