Data Structure Circular Queue Practice Questions with Solutions

Introductions

A circular queue is a queue where the last position connects back to the first position, allowing unused spaces to be reused. These practice questions focus on actual circular queue operations such as enqueue, dequeue, checking full and empty conditions, using front and rear, wrap-around movement, and implementing a circular queue with JavaScript. The examples start simple and gradually build practical problem-solving skills. Data Structure Circular Queue practice questions with solutions help to understand the concepts.

Question 1: Enqueue Elements into a Circular Queue

Question

Create a circular queue of size 5 and insert the values:

10, 20, 30

Display the queue elements.

Solution

We can implement a circular queue using an array.

let size = 5;
let queue = new Array(size);

let front = -1;
let rear = -1;

To insert the first element, both front and rear become 0.

For later elements, rear moves forward:

rear = (rear + 1) % size;

Complete code:

let size = 5;
let queue = new Array(size);

let front = -1;
let rear = -1;

function enqueue(value) {
    if ((rear + 1) % size === front) {
        console.log("Queue is Full");
        return;
    }

    if (front === -1) {
        front = 0;
        rear = 0;
    } else {
        rear = (rear + 1) % size;
    }

    queue[rear] = value;
}

enqueue(10);
enqueue(20);
enqueue(30);

console.log(queue);

After inserting:

index:   0    1    2    3    4
        -------------------------
queue:  10   20   30   -    -
         ↑         ↑
       front      rear

Output

[10, 20, 30, <2 empty items>]

Answer

The circular queue currently contains:

Front → 10 → 20 → 30 ← Rear

Question 2: Dequeue an Element from a Circular Queue

Question

Given a circular queue containing:

10 → 20 → 30 → 40

Remove the front element.

Solution

The front element is 10.

After removing it, front must move to the next position.

front = (front + 1) % size;

Complete example:

let size = 5;
let queue = [10, 20, 30, 40, undefined];

let front = 0;
let rear = 3;

let removed = queue[front];

queue[front] = undefined;

front = (front + 1) % size;

console.log("Removed:", removed);
console.log("New Front:", queue[front]);

Before deletion:

Front → 10 → 20 → 30 → 40

After deletion:

Front → 20 → 30 → 40

Output

Removed: 10
New Front: 20

Answer

10 is removed and 20 becomes the new front element.


Question 3: Demonstrate the Circular Movement of Rear

Question

A circular queue has size 5.

Insert:

10, 20, 30, 40

Then remove two elements and insert:

50, 60

Show how rear wraps around to the beginning.

Solution

Initially:

index:  0   1   2   3   4
        -------------------
queue: 10  20  30  40   -

Here:

front = 0
rear = 3

Remove 10 and 20.

Now:

front = 2
rear = 3

The available positions at the beginning can now be reused.

Insert 50:

rear = (rear + 1) % 5;

So:

rear = (3 + 1) % 5
rear = 4

Insert 60:

rear = (4 + 1) % 5
rear = 0

The rear has wrapped around to index 0.

Complete code:

let size = 5;
let queue = new Array(size);

queue[0] = 10;
queue[1] = 20;
queue[2] = 30;
queue[3] = 40;

let front = 0;
let rear = 3;

queue[0] = undefined;
front = 1;

queue[1] = undefined;
front = 2;

rear = (rear + 1) % size;
queue[rear] = 50;

rear = (rear + 1) % size;
queue[rear] = 60;

console.log(queue);
console.log("Front:", front);
console.log("Rear:", rear);

The important calculation is:

(4 + 1) % 5 = 0

So the rear moves from index 4 back to index 0.

Output

[60, <1 empty item>, 30, 40, 50]
Front: 2
Rear: 0

Answer

The rear wraps from the last array position back to the first position. This is the main idea behind a circular queue.


Question 4: Check Whether a Circular Queue is Full

Question

A circular queue has size 5.

The current positions are:

front = 2
rear = 1

Determine whether the queue is full.

Solution

For the common circular queue implementation, the queue is full when:

(rear + 1) % size === front

Substitute the values:

(1 + 1) % 5 === 2

Therefore:

2 === 2

This is true.

Complete code:

let size = 5;
let front = 2;
let rear = 1;

if ((rear + 1) % size === front) {
    console.log("Queue is Full");
} else {
    console.log("Queue is Not Full");
}

Output

Queue is Full

Answer

The circular queue is full because the next position of rear is already front.


Question 5: Insert an Element After Wrap-Around

Question

A circular queue of size 5 currently has:

index:  0   1   2   3   4
        -------------------
queue: 50   -  30  40  50
              ↑       ↑
            front    rear

Assume the actual values are:

30, 40, 50

Insert 60 and show the new rear position.

Solution

The current rear is at index 4.

The next position is calculated using:

rear = (rear + 1) % size;

Therefore:

rear = (4 + 1) % 5
rear = 0

So rear moves back to index 0.

Insert 60:

let size = 5;
let queue = new Array(size);

queue[2] = 30;
queue[3] = 40;
queue[4] = 50;

let front = 2;
let rear = 4;

rear = (rear + 1) % size;
queue[rear] = 60;

console.log(queue);
console.log("Rear:", rear);

The structure becomes:

index:  0   1   2   3   4
        -------------------
queue: 60   -  30  40  50
         ↑       ↑
       rear    front

Logically, the queue order is:

Front → 30 → 40 → 50 → 60 ← Rear

Output

[60, &lt;1 empty item>, 30, 40, 50]
Rear: 0

Answer

The rear wraps around from index 4 to index 0.


Question 6: Display All Elements of a Circular Queue

Question

Display all elements exactly once from this circular queue:

index:  0   1   2   3   4
        -------------------
queue: 50   -  20  30  40
              ↑       ↑
            front    rear

Solution

The front is at index 2 and the rear is at index 0.

Because the queue is circular, the logical order is:

20 → 30 → 40 → 50

We start at front and continue until we reach rear.

let size = 5;

let queue = new Array(size);

queue[0] = 50;
queue[2] = 20;
queue[3] = 30;
queue[4] = 40;

let front = 2;
let rear = 0;

let i = front;

while (true) {
    console.log(queue[i]);

    if (i === rear) {
        break;
    }

    i = (i + 1) % size;
}

The movement is:

2 → 3 → 4 → 0

Then we stop because index 0 is the rear.

Output

20
30
40
50

Answer

The circular queue must be displayed according to the logical order from front to rear, not simply from array index 0 to the last index.


Question 7: Remove Two Elements from a Circular Queue

Question

A circular queue contains:

Front → 10 → 20 → 30 → 40 → 50 ← Rear

Remove two elements and print the remaining elements.

Solution

The first dequeue removes 10.

Then:

front → 20

The second dequeue removes 20.

Then:

front → 30

We can use:

front = (front + 1) % size;

Complete code:

let size = 5;

let queue = [10, 20, 30, 40, 50];

let front = 0;
let rear = 4;

queue[front] = undefined;
front = (front + 1) % size;

queue[front] = undefined;
front = (front + 1) % size;

console.log("Front:", queue[front]);

After the first removal:

20 → 30 → 40 → 50

After the second removal:

30 → 40 → 50

Output

Front: 30

Answer

After removing two elements, 30 becomes the new front.


Question 8: Implement Enqueue in a Circular Queue

Question

Create a circular queue of size 5 and implement an enqueue() function that:

  • Adds a value.
  • Moves rear circularly.
  • Detects when the queue is full.

Test it with:

10, 20, 30, 40, 50

Solution

Create the queue:

let size = 5;
let queue = new Array(size);

let front = -1;
let rear = -1;

The enqueue() function:

function enqueue(value) {

    if ((rear + 1) % size === front) {
        console.log("Queue is Full");
        return;
    }

    if (front === -1) {
        front = 0;
        rear = 0;
    } else {
        rear = (rear + 1) % size;
    }

    queue[rear] = value;
}

Now insert the values:

enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);

Complete code:

let size = 5;
let queue = new Array(size);

let front = -1;
let rear = -1;

function enqueue(value) {

    if ((rear + 1) % size === front) {
        console.log("Queue is Full");
        return;
    }

    if (front === -1) {
        front = 0;
        rear = 0;
    } else {
        rear = (rear + 1) % size;
    }

    queue[rear] = value;
}

enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);

console.log(queue);

Output

[10, 20, 30, 40, 50]

Answer

The circular queue successfully stores all five values.


Question 9: Implement Dequeue in a Circular Queue

Question

Create a dequeue() function for a circular queue that removes the front element and correctly updates front.

Solution

We need to handle two important cases.

If the queue contains only one element:

front === rear

After removing it, the queue becomes empty.

Otherwise, move front using:

front = (front + 1) % size;

Complete code:

let size = 5;
let queue = new Array(size);

let front = -1;
let rear = -1;

function enqueue(value) {

    if ((rear + 1) % size === front) {
        console.log("Queue is Full");
        return;
    }

    if (front === -1) {
        front = 0;
        rear = 0;
    } else {
        rear = (rear + 1) % size;
    }

    queue[rear] = value;
}

function dequeue() {

    if (front === -1) {
        console.log("Queue is Empty");
        return;
    }

    let removed = queue[front];

    queue[front] = undefined;

    if (front === rear) {
        front = -1;
        rear = -1;
    } else {
        front = (front + 1) % size;
    }

    return removed;
}

enqueue(10);
enqueue(20);
enqueue(30);

console.log("Removed:", dequeue());
console.log("Removed:", dequeue());

console.log("Front:", queue[front]);

The queue starts as:

10 → 20 → 30

First dequeue:

10 removed
20 → 30

Second dequeue:

20 removed
30

Output

Removed: 10
Removed: 20
Front: 30

Answer

The dequeue() function correctly removes elements from the front and moves the front position circularly.


Question 10: Build a Complete Circular Queue

Question

Create a CircularQueue class with these operations:

  • enqueue()
  • dequeue()
  • display()

Create a queue of size 5, insert:

10, 20, 30, 40

Remove two elements, then insert:

50, 60

Finally, display the queue.

Solution

Create the class:

class CircularQueue {
    constructor(size) {
        this.size = size;
        this.queue = new Array(size);
        this.front = -1;
        this.rear = -1;
    }
}

Enqueue Operation

enqueue(value) {

    if ((this.rear + 1) % this.size === this.front) {
        console.log("Queue is Full");
        return;
    }

    if (this.front === -1) {
        this.front = 0;
        this.rear = 0;
    } else {
        this.rear = (this.rear + 1) % this.size;
    }

    this.queue[this.rear] = value;
}

Dequeue Operation

dequeue() {

    if (this.front === -1) {
        console.log("Queue is Empty");
        return;
    }

    let removed = this.queue[this.front];

    this.queue[this.front] = undefined;

    if (this.front === this.rear) {
        this.front = -1;
        this.rear = -1;
    } else {
        this.front = (this.front + 1) % this.size;
    }

    return removed;
}

Display Operation

display() {

    if (this.front === -1) {
        console.log("Queue is Empty");
        return;
    }

    let result = [];
    let i = this.front;

    while (true) {
        result.push(this.queue[i]);

        if (i === this.rear) {
            break;
        }

        i = (i + 1) % this.size;
    }

    console.log(result);
}

Complete Code

class CircularQueue {
    constructor(size) {
        this.size = size;
        this.queue = new Array(size);
        this.front = -1;
        this.rear = -1;
    }

    enqueue(value) {

        if ((this.rear + 1) % this.size === this.front) {
            console.log("Queue is Full");
            return;
        }

        if (this.front === -1) {
            this.front = 0;
            this.rear = 0;
        } else {
            this.rear = (this.rear + 1) % this.size;
        }

        this.queue[this.rear] = value;
    }

    dequeue() {

        if (this.front === -1) {
            console.log("Queue is Empty");
            return;
        }

        let removed = this.queue[this.front];

        this.queue[this.front] = undefined;

        if (this.front === this.rear) {
            this.front = -1;
            this.rear = -1;
        } else {
            this.front = (this.front + 1) % this.size;
        }

        return removed;
    }

    display() {

        if (this.front === -1) {
            console.log("Queue is Empty");
            return;
        }

        let result = [];
        let i = this.front;

        while (true) {
            result.push(this.queue[i]);

            if (i === this.rear) {
                break;
            }

            i = (i + 1) % this.size;
        }

        console.log(result);
    }
}

let cq = new CircularQueue(5);

cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
cq.enqueue(40);

console.log("Removed:", cq.dequeue());
console.log("Removed:", cq.dequeue());

cq.enqueue(50);
cq.enqueue(60);

cq.display();

Let’s follow the important steps.

Initially:

10 → 20 → 30 → 40

Remove 10:

20 → 30 → 40

Remove 20:

30 → 40

Now insert 50:

30 → 40 → 50

Insert 60:

30 → 40 → 50 → 60

The circular queue reuses positions that became available after the dequeue operations.

Output

Removed: 10
Removed: 20
[30, 40, 50, 60]

Answer

The complete circular queue successfully supports insertion, deletion, and circular reuse of array positions.

Key Takeaways

  • A circular queue is a queue in which the positions are treated as circular.
  • The last array position can be followed by the first position.
  • Circular queues help reuse spaces that become available after dequeue operations.
  • front represents the position from which elements are removed.
  • rear represents the position where new elements are inserted.
  • The circular movement is commonly calculated using (index + 1) % size.
  • The queue is full when (rear + 1) % size === front in this implementation.
  • When front === -1, the queue is empty.
  • When front === rear, removing the only element makes the queue empty.
  • Circular queues are useful for fixed-size buffers, scheduling systems, streaming data, and resource management.
  • A circular queue avoids the unused-space problem that can occur in a simple array-based queue.
  • Enqueue and dequeue can be implemented in O(1) time when front and rear are maintained correctly.
  • Displaying all elements takes O(n) time because every element needs to be visited.

FAQs

1. What is a circular queue?

A circular queue is a queue where the last position connects back to the first position, allowing previously used positions to be reused.

2. How is a circular queue different from a normal queue?

A normal array-based queue may leave unused spaces at the beginning after elements are removed. A circular queue reuses those spaces by allowing rear to wrap around to the beginning.

3. What is the purpose of the modulo operator in a circular queue?

The modulo operator % helps move an index back to the beginning after it reaches the last position. For example, (4 + 1) % 5 gives 0.

4. When is a circular queue considered full?

In the implementation used here, the queue is full when (rear + 1) % size === front.

5. When is a circular queue considered empty?

In this implementation, the queue is empty when front === -1.

6. What are the main operations of a circular queue?

The main operations are enqueue, which adds an element, and dequeue, which removes the front element. A display operation is also commonly implemented to view the queue.

7. What is the time complexity of enqueue and dequeue in a circular queue?

When front and rear are maintained correctly, both enqueue and dequeue can be performed in O(1) time.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top