Linear Data StructuresIntermediate LevelStudy Time: 20 mins

Circular Queue Data Structure

A Circular Queue connects the end of an array back to the beginning to form a ring buffer, reusing freed space efficiently.

Loading visualizer workspace...

Circular Queue Modulo Wrapping Worked Example

Sample Input:Capacity: 3. Operations: Enqueue(10), Enqueue(20), Dequeue(), Enqueue(30), Enqueue(40)
Step 1

Initial Enqueues & Dequeue

Enqueue 10, 20. Then Dequeue 10 to free index 0.

Index 0: [Freed], Index 1: 20 (Front/Rear). Front: 1, Rear: 1.
Index 0 is empty but standard linear queue cannot reuse it without shifting.
Step 2

Circular Modulo Wrap Enqueue (Enqueue 40)

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!
Modulo arithmetic rear = (rear + 1) % capacity reuses freed front slots instantly.

Real-world analogy: Roundabout Traffic

Ring Buffer
Front & Rear
Hover over items for details

Cars enter and exit a circular ring roundabout. When cars leave, the open space is reused immediately by incoming cars.

  • Ring Buffer= Index wraps around using modulo math.
  • Front & Rear= Pointers advance circularly (i + 1) % N.

Operations explained

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapCircular Queue: O(1) Constant Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Circular Queue D
Time Steps (N=500)

1 step (Constant)

O(1) Constant Time

Space Footprint

O(N) Auxiliary Space

Memory growth rate as N expands.

Performance Rank
Optimal Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
EnqueueO(1)O(1)
DequeueO(1)O(1)

Code implementations

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;
  }
}

Real-world applications

Case 01

Audio Streaming

Buffers sound playback chunks in audio drivers.

Case 02

Traffic Controllers

Cycles signal light states continuously.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Circular Queue Knowledge & Skill Test

Validate your conceptual understanding, operation mechanics, and core concepts of Circular Queue.