A circular queue is a type of queue where the last position is connected back to the first position to make a circle. It helps use space efficiently by reusing empty spots after dequeue operations.
Click to reveal answer
beginner
How do you check if a circular queue is full?
The queue is full if the next position of rear (calculated as (rear + 1) % size) is equal to front. This means no space is left to insert new elements.
Click to reveal answer
beginner
What happens to front and rear pointers when you enqueue in a circular queue?
When you enqueue, rear moves forward by one position using (rear + 1) % size. If the queue was empty, front is set to 0 to mark the start.
Click to reveal answer
beginner
How do you dequeue from a circular queue?
You remove the element at front. If front equals rear, the queue becomes empty, so both are reset to -1. Otherwise, front moves forward by (front + 1) % size.
Click to reveal answer
beginner
Why use a circular queue instead of a normal queue with an array?
A normal queue wastes space when elements are dequeued because front moves forward and space behind is unused. Circular queue reuses that space by wrapping around, making it efficient.
Click to reveal answer
In a circular queue of size 5, if front = 3 and rear = 1, is the queue empty?
ANo, it is not empty
BYes, it is empty
CIt is full
DCannot determine
✗ Incorrect
If front = 3 and rear = 1, the queue is not empty because front != -1 and rear != -1. The positions show elements are present.
What is the condition to check if a circular queue is full?
Afront == -1
B(rear + 1) % size == front
Crear == front
Drear == size - 1
✗ Incorrect
The queue is full when the next position of rear wraps to front, meaning no space to insert.
What happens to front and rear when the last element is dequeued from a circular queue?
ANo change
Bfront moves forward, rear stays
Crear moves backward, front stays
Dfront and rear both reset to -1
✗ Incorrect
When the last element is removed, the queue becomes empty, so front and rear reset to -1.
How do you move rear pointer when enqueueing in a circular queue?
Arear = front + 1
Brear = rear + 1
Crear = (rear + 1) % size
Drear = size - 1
✗ Incorrect
Rear moves forward circularly using modulo to wrap around the array.
Why is a circular queue more space efficient than a normal queue using an array?
ABecause it reuses empty spaces after dequeue
BBecause it uses linked list internally
CBecause it has unlimited size
DBecause it does not need front pointer
✗ Incorrect
Circular queue wraps around to reuse spaces freed by dequeues, unlike normal queue which wastes space.
Explain how enqueue and dequeue operations work in a circular queue implemented with an array.
Think about how the pointers wrap around the array.
You got /4 concepts.
Describe the advantages of using a circular queue over a simple linear queue with an array.
Consider what happens when elements are removed from the front.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using a circular queue over a simple linear queue when implemented using an array?
easy
A. It automatically sorts the elements in the queue.
B. It efficiently uses all available space by wrapping around the array.
C. It allows storing different data types in the queue.
D. It uses less memory than a linear queue.
Solution
Step 1: Understand linear queue limitation
In a linear queue using an array, once the rear reaches the end, no more elements can be added even if there is free space at the front.
Step 2: Circular queue advantage
A circular queue connects the end of the array back to the front, allowing reuse of free space and efficient use of the array.
Final Answer:
It efficiently uses all available space by wrapping around the array. -> Option B
Quick Check:
Circular queue = efficient space use [OK]
Hint: Circular queue reuses space by wrapping indices [OK]
Common Mistakes:
Thinking circular queue sorts elements
Believing it uses less memory than linear queue
Assuming it allows mixed data types automatically
2. Which of the following is the correct way to check if a circular queue is full when using an array of size size with front and rear pointers?
easy
A. if (rear === size - 1)
B. if (rear === front)
C. if ((rear + 1) % size === front)
D. if (front === -1)
Solution
Step 1: Understand full condition in circular queue
The queue is full when the next position of rear (rear + 1 modulo size) equals front, meaning no space to insert.
Step 2: Check each option
if ((rear + 1) % size === front) correctly uses modulo to wrap around and compare with front. Others do not correctly represent full condition.
Final Answer:
if ((rear + 1) % size === front) -> Option C
Quick Check:
Full condition = (rear + 1) % size === front [OK]
Hint: Use modulo to wrap rear and compare with front [OK]
Common Mistakes:
Checking rear === front for full (actually empty condition)
Ignoring modulo wrap-around
Using front === -1 to check full
3. Consider the following JavaScript code for a circular queue of size 5. What will be the output after these operations?
const size = 5;
let queue = new Array(size);
let front = -1, rear = -1;
function enqueue(value) {
if ((rear + 1) % size === front) {
console.log('Queue is full');
return;
}
if (front === -1) front = 0;
rear = (rear + 1) % size;
queue[rear] = value;
}
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50); // This enqueues successfully (queue now full)
console.log(queue);
medium
A. [10, 20, 30, 40, 50]
B. [10, 20, 30, 40, undefined]
C. [10, 20, 30, 40, null]
D. [undefined, 10, 20, 30, 40]
Solution
Step 1: Trace enqueue operations
Initial: front=-1, rear=-1. enqueue(10): front=0, rear=0, queue[0]=10. enqueue(20): rear=1, queue[1]=20. enqueue(30): rear=2, queue[2]=30. enqueue(40): rear=3, queue[3]=40. enqueue(50): (rear+1)%5 = 4 === front 0? No, rear=4, queue[4]=50. No full printed.
Hint: Check modulo full condition carefully to trace enqueue [OK]
Common Mistakes:
Thinking queue full after 4 enqueues (5th fails)
Confusing undefined and null values
Ignoring front and rear initial values
4. The following code snippet for dequeue operation in a circular queue has a bug. What is the bug?
function dequeue() {
if (front === -1) {
console.log('Queue is empty');
return null;
}
let value = queue[front];
if (front === rear) {
front = rear = -1;
} else {
front = front + 1;
}
return value;
}
medium
A. front should be updated using modulo to wrap around
B. rear should be incremented instead of front
C. The empty check should be rear === -1
D. The function should not return null when empty
Solution
Step 1: Analyze front update in dequeue
When removing an element, front moves forward. Since queue is circular, front must wrap using modulo size.
Step 2: Identify bug in front update
Code uses front = front + 1 without modulo, so front can go beyond array size causing errors.
Final Answer:
front should be updated using modulo to wrap around -> Option A
Quick Check:
Update front with modulo to avoid overflow [OK]
Hint: Always use modulo when moving front or rear in circular queue [OK]
Common Mistakes:
Incrementing rear instead of front on dequeue
Checking empty condition incorrectly
Ignoring modulo wrap-around
5. You have a circular queue of size 4 implemented using an array. Initially, front = -1 and rear = -1. You perform these operations: