Circular Queues
A circular queue (ring buffer) uses a fixed-size array and two pointers — head and tail — that wrap around using modulo arithmetic. When the tail reaches the end of the array it wraps back to index 0, reusing the space freed by earlier dequeues. Result: O(1) enqueue and dequeue with zero memory allocation.
What is a circular queue?
A circular queue (ring buffer) is a fixed-size queue stored in an array where the head and tail indices wrap around to the start using modulo ((i + 1) % capacity). It reuses freed slots, so enqueue and dequeue are O(1) with no shifting and no growing. It's used in audio and network buffers, keyboard input and logging the last N events.
class RingBuffer {
constructor(capacity) { this.buf = new Array(capacity); this.head = 0; this.size = 0; }
enqueue(x) {
if (this.size === this.buf.length) throw new Error("full");
this.buf[(this.head + this.size++) % this.buf.length] = x;
}
dequeue() {
const x = this.buf[this.head];
this.head = (this.head + 1) % this.buf.length; this.size--;
return x;
}
}Ring visualization
Capacity = 8. head = 2, tail = 5. Elements A,B,C,D (4 items). Free slots: 0,1,6,7. When we enqueue E: tail becomes (5+1) mod 8 = 6.
Live demo — FIFO queue
Enqueue and dequeue items to see the FIFO order in action. In a circular queue the same slots are reused using modulo index arithmetic.
Queue — First In, First Out (FIFO)
size: 4Implementation
1CircularQueue(capacity):2 data <- array of size capacity3 head <- 0 // points to front element4 tail <- 0 // points to next empty slot5 size <- 06 capacity <- capacity78enqueue(value):9 if size == capacity: error 'Queue is full'10 data[tail] <- value11 tail <- (tail + 1) mod capacity // wrap around with modulo12 size <- size + 11314dequeue() -> value:15 if size == 0: error 'Queue is empty'16 value <- data[head]17 head <- (head + 1) mod capacity // wrap around with modulo18 size <- size - 119 return value2021peek() -> value:22 if size == 0: error 'Queue is empty'23 return data[head]2425isFull() -> bool: return size == capacity26isEmpty() -> bool: return size == 0
Why modulo works
Capacity = 8. Tail starts at 6. After 3 enqueues:
Linear queue vs Circular queue
Linear Queue (array)
- − After n dequeues, front n slots are wasted
- − Need to shift elements left: O(n) per dequeue
- − Or accept growing memory waste
Circular Queue
- + Reuses dequeued slots via wrap-around
- + O(1) enqueue and dequeue, always
- + Fixed memory footprint
- + Cache-friendly contiguous array
Real-world uses
OS I/O buffers
Keyboard/disk input buffered in a ring so the OS can process at its own pace
Audio streaming
Producer fills buffer, consumer reads — ring prevents gaps without blocking
Network packet queues
Fixed-size queues drop oldest packets when full (drop-tail policy)
CPU scheduling
Round-robin schedulers cycle through tasks using a circular buffer of process IDs
Time complexity
Constant time
Constant time
Constant time
Linear time
🎯 Quick Check
A circular queue has capacity 5. head = 3, tail = 3, size = 0. After enqueue(X) and enqueue(Y), what is the tail index?
Circular Queues: summary
Key points
- Track
size(or keep one slot empty) to tell "full" from "empty". - Some ring buffers overwrite the oldest item when full, which suits "last N events".
Frequently asked questions
›Why use a circular queue instead of a normal queue?
It has a fixed memory footprint and O(1) operations without ever shifting or reallocating, which is ideal for streaming data.