A Circular Queue connects the end of an array back to the beginning to form a ring buffer, reusing freed space efficiently.
Enqueue 10, 20. Then Dequeue 10 to free index 0.
Index 0: [Freed], Index 1: 20 (Front/Rear). Front: 1, Rear: 1.Calculate rear = (1 + 1) % 3 = 2 -> Enqueue 30. Calculate rear = (2 + 1) % 3 = 0 -> Enqueue 40 at index 0!
Index 0: 40 (Rear), Index 1: 20 (Front), Index 2: 30. Buffer wraps seamlessly!Cars enter and exit a circular ring roundabout. When cars leave, the open space is reused immediately by incoming cars.
1 step (Constant)
O(1) Constant Time
O(N) Auxiliary Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Enqueue | O(1) | O(1) |
| Dequeue | O(1) | O(1) |
class CircularQueue {
constructor(k) { this.size = k; this.queue = new Array(k); this.head = -1; this.tail = -1; }
enqueue(val) {
if (this.isFull()) return false;
if (this.isEmpty()) this.head = 0;
this.tail = (this.tail + 1) % this.size;
this.queue[this.tail] = val;
return true;
}
}Buffers sound playback chunks in audio drivers.
Cycles signal light states continuously.
Validate your conceptual understanding, operation mechanics, and core concepts of Circular Queue.
Circular queues prevent memory waste in fixed-capacity queues by avoiding array element shifts!
Circular Queue initialized. Ring capacity: 8 slots. Modular formula: (rear + 1) % 8.