Queues & Deques

First-in, first-out queues, double-ended deques and circular buffers — the backbone of BFS, scheduling and streaming.

Beginner 2 min readLesson 5 of 16 Interview Q&A

A queue is first-in, first-out (FIFO): add at the back (enqueue), remove from the front (dequeue) — like a line at a ticket counter. A deque ("deck", double-ended queue) allows adding and removing at both ends.

enqueue[ 7 | 3 | 9 | 4 ]back ……… frontdequeue
Elements leave in the order they arrived.

Operations

OperationQueueDequeTime
Add backoffer / enqueueaddLastO(1)
Remove frontpoll / dequeuepollFirstO(1)
Add front—addFirstO(1)
Remove back—pollLastO(1)
PeekpeekpeekFirst / peekLastO(1)

Circular buffer (ring buffer)

A fixed-size array with head and tail indices that wrap around with modulo. Both ends are O(1) and nothing ever shifts. This is how ArrayDeque, network packet buffers, audio buffers and logging queues work.

9041·2·37435tailhead
Capacity 6: the queue holds 7,3,9,4 — it wraps from index 5 back to 0.
class CircularQueue {
    private final int[] buf;
    private int head = 0, size = 0;

    CircularQueue(int capacity) { buf = new int[capacity]; }

    boolean enqueue(int x) {
        if (size == buf.length) return false;          // full
        buf[(head + size) % buf.length] = x;
        size++;
        return true;
    }

    Integer dequeue() {
        if (size == 0) return null;                     // empty
        int x = buf[head];
        head = (head + 1) % buf.length;
        size--;
        return x;
    }
}

Built-in queues and deques

import java.util.*;

Queue<Integer> q = new ArrayDeque<>();
q.offer(1); q.offer(2);
q.poll();                       // 1

Deque<Integer> dq = new ArrayDeque<>();
dq.offerFirst(1); dq.offerLast(2);
dq.pollLast();                  // 2

// Thread-safe producer/consumer queue
BlockingQueue<String> jobs = new LinkedBlockingQueue<>();

Where queues appear

  • Breadth-first search — explore level by level. See Tree BFS.
  • Scheduling — OS ready queues, print spoolers, thread pools. See CPU Scheduling.
  • Buffers between producers and consumers — keyboard input, network packets, message queues like Kafka and RabbitMQ (Message queues).
  • Sliding window maximum — a monotonic deque gives O(n). See Sliding window.
  • Priority queues are a different structure (a heap) — they dequeue by priority, not arrival order.
Key takeaways
  • Queue = FIFO; deque = both ends; all core operations O(1).
  • Implement with a circular buffer or linked list — never by shifting an array.
  • Queues power BFS, schedulers and producer/consumer pipelines.
Interview questions
QImplement a stack using queues.easy

With one queue: on push(x), enqueue x, then rotate the previous size−1 elements behind it (dequeue + enqueue). Now the front is the most recent element: push O(n), pop O(1).

QHow do you distinguish 'full' from 'empty' in a circular buffer?medium

When head == tail, it could be either. Fixes: keep an explicit size counter (as above), or always leave one slot empty so full is (tail + 1) % cap == head.

QSliding window maximum in O(n).hard

Keep a deque of indices with decreasing values. For each new index: pop from the back while its value ≤ the new value; push the new index; pop the front if it fell out of the window. The front is always the window's maximum. Each index is pushed and popped once.