SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Circular Queues

Learn how a circular queue uses a fixed-size array with wrapped index calculations.

Suppose you fill a three-slot queue, then remove its first item. There is space for one more item at index 0. A circular queue can reuse that slot while the two older items stay in place.

What “circular” means

Number the slots 0, 1, 2. From slot 2, advancing one step takes you back to slot 0. The array stays in place. Only the index wraps.

Fixed-size and growing-array queues

In the previous queue, enqueue always appended to the underlying array. In this lesson, the queue has a fixed positive capacity k and rejects an enqueue when all k slots are occupied. Other implementations can grow a circular buffer by allocating a larger array and copying the items in queue order.

State and queue order

A queue’s state can be described with three values:

Piece of stateRole
slotsThe physical array positions that hold queue items.
frontThe physical position of the queue’s first logical item.
sizeThe number of items currently in the queue.

In four slots, inserting E at slot 0 changes the logical order from B, C, D to B, C, D, E.

To read the four-slot queue in the diagram, begin at front = 1 and follow the three stored items, B, C, D. Enqueuing E fills slot 0. Read from the same front for four steps and the order becomes B, C, D, E.

Circular indexing

Slots 4 and 0 contain A and B. Adding the size 2 to front 4 wraps the next insertion to slot 1.

The item at logical offset j occupies slot (front + j) % k. Substitute j = 0 for the front item. For a nonempty queue, substitute j = size - 1 for the rear, the most recently enqueued item still present. If the queue has room, j = size gives the slot for the next insertion.

Enqueue and dequeue wraparound

Take a capacity-five queue whose first item, A, occupies slot 4. Follow one enqueue and one dequeue.

StateSlots 0 through 4frontsizeFIFO order
Start[B, ·, ·, ·, A]42A, B
Enqueue C at (4 + 2) % 5 = 1[B, C, ·, ·, A]43A, B, C
Dequeue A and advance to (4 + 1) % 5 = 0[B, C, ·, ·, ·]02B, C

The dots denote unused slots. Check the last row by starting at index 0 and reading two values.

Empty, full, front, and rear

On an empty queue there is no front or rear item to read. On a nonempty queue, front identifies the oldest item and (front + size - 1) % k identifies the newest. The next insertion index is one step beyond that newest item.

Why size is necessary

The front and next insertion arrows coincide at slot 0 when none or all of the four slots are occupied.

In this diagram, the arrow labeled rear marks the next insertion position. Some implementations use that name for the write index. The peekRear method below returns the last stored item, one slot before that write index.

Compare front with the next insertion index. If size = 0, then (front + 0) % k equals front. If size = k, then (front + k) % k also equals front. Attempting a write in the second state would overwrite the oldest item.

Our implementation uses size to distinguish the two states while making all k slots available. Allow a dequeue when size > 0 and an enqueue when size < k. A different design could instead keep a separate full flag or reserve one unused slot.

Implementing a circular queue

The class below stores the slots, the capacity, the front index, and the size. It calculates both the next write position and the rear item’s position when needed.

Required queue states

Enqueue and dequeue return a boolean to indicate whether they succeeded. Reading an empty queue returns undefined in JavaScript or None in Python. Assume the capacity is a positive integer.

class CircularQueue {
  constructor(capacity) {
    this.slots = Array(capacity);
    this.capacity = capacity;
    this.front = 0;
    this.size = 0;
  }

  enqueue(value) {
    if (this.size === this.capacity) return false;

    const writeIndex = (this.front + this.size) % this.capacity;
    this.slots[writeIndex] = value;
    this.size++;
    return true;
  }

  dequeue() {
    if (this.size === 0) return false;

    this.slots[this.front] = undefined;
    this.front = (this.front + 1) % this.capacity;
    this.size--;
    return true;
  }

  peekFront() {
    return this.size === 0 ? undefined : this.slots[this.front];
  }

  peekRear() {
    if (this.size === 0) return undefined;
    const rearIndex = (this.front + this.size - 1) % this.capacity;
    return this.slots[rearIndex];
  }
}

Operation design

OperationBefore the updateAfter the update
EnqueueCheck for space. Compute (front + size) % k.Write the value at that slot and increase size by one.
DequeueCheck for an item. Clear the slot at front.Set front = (front + 1) % k and decrease size by one.
Read front or rearCheck for an item. Compute its index.Read the slot. Both front and size keep their values.

Each operation uses O(1)O(1)O(1) time. Reserving the buffer uses O(k)O(k)O(k) space.

Verification checklist

  1. Use a queue with capacity one: enqueue one item, inspect both ends, dequeue it, and verify that the queue is empty.
  2. Fill a queue to capacity and verify that another enqueue is handled as a full-queue operation.
  3. Dequeue from an empty queue and verify that the empty-queue operation is handled.
  4. Enqueue and dequeue enough items to force positions to wrap, then verify front and rear lookups.

A correct circular queue keeps all four operations constant-time while handling capacity-one, full, empty, and wrapped states.


Key Takeaways

  • This bounded queue reuses a fixed array by wrapping its indices from the last slot to the first.
  • front identifies the first logical item, while size identifies how many physical slots belong to the queue.
  • Use (front + size) % k to find the next insertion position within a capacity of k slots.
  • The next insertion index equals front in both the empty and full states. Use size to distinguish them.
  • Enqueue, dequeue, and front and rear lookups can all run in O(1) time when their indexes and queue state are updated directly.

How is this lesson?

Previous lessonImplement Queue Using StacksNext lessonCircular Queues Quiz
Edit on GitHubReport on GitHub
On this page
%

On this page

What “circular” meansFixed-size and growing-array queuesState and queue orderCircular indexingEnqueue and dequeue wraparoundEmpty, full, front, and rearWhy size is necessaryImplementing a circular queueRequired queue statesOperation designVerification checklistKey Takeaways