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
intermediate
Explain the role of modulo (%) operator in circular queue implementation.
The modulo operator helps wrap around the index when it reaches the end of the array. It keeps the index within array bounds, making the queue circular.
Click to reveal answer
intermediate
What happens to front and rear pointers after enqueue and dequeue in a circular queue?
After enqueue, rear moves forward by one position using modulo to wrap around. After dequeue, front moves forward similarly. This keeps track of the current start and end of the queue.
Click to reveal answer
beginner
Why is a circular queue better than a simple queue using arrays?
A simple 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
What condition indicates that a circular queue is empty?
Afront == rear
Bfront == -1
Crear == -1
D(rear + 1) % size == front
✗ Incorrect
When front is -1, it means no elements are in the queue, so it is empty.
In a circular queue of size 5, if rear is 4 and front is 0, what is the next rear position after enqueue?
A1
B5
C0
D4
✗ Incorrect
Using modulo: (4 + 1) % 5 = 0, so rear wraps to position 0.
Which operation moves the front pointer in a circular queue?
AEnqueue
BIsFull check
CPeek
DDequeue
✗ Incorrect
Dequeue removes an element from the front, so front pointer moves forward.
What is the main advantage of using a circular queue over a linear queue?
ABetter space utilization
BNo need for pointers
CFaster enqueue
DSimpler code
✗ Incorrect
Circular queue reuses empty spaces, improving space utilization.
If front = 2, rear = 1, and size = 5, is the circular queue full?
AYes
BNo
CCannot determine
DOnly if front == rear
✗ Incorrect
If (rear + 1) % size == front, queue is full. Here, (1 + 1) % 5 = 2 equals front, so full.
Describe how enqueue and dequeue operations work in a circular queue using an array.
Think about how the pointers move and how the array wraps around.
You got /4 concepts.
Explain why the modulo operator (%) is essential in circular queue implementation.
Consider what happens when the pointer reaches the last array position.
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 allows storing different data types in the same queue.
B. It efficiently uses all available space by wrapping around the array.
C. It automatically sorts the elements 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 empty spaces 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 stores different 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 indices?
easy
A. if (rear === front)
B. if (front === -1)
C. if ((rear + 1) % size === front)
D. if (rear === size - 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: Apply modulo for wrap-around
Using modulo ensures the rear wraps to the start of the array, so the condition is (rear + 1) % size === front.
Final Answer:
if ((rear + 1) % size === front) -> Option C
Quick Check:
Full condition = (rear + 1) % size === front [OK]
Hint: Full when next rear wraps to front [OK]
Common Mistakes:
Checking rear === front means empty, not full
Using rear === size - 1 ignores wrap-around
Checking front === -1 is for empty queue
3. Consider a circular queue of size 5 (indices 0 to 4). Initially, front = 0 and rear = 3. After performing enqueue(10) and dequeue(), what are the new values of front and rear?
medium
A. front = 0, rear = 4
B. front = 1, rear = 3
C. front = 0, rear = 3
D. front = 1, rear = 4
Solution
Step 1: Perform enqueue(10)
rear moves from 3 to (3 + 1) % 5 = 4. So rear = 4, front remains 0.
Step 2: Perform dequeue()
front moves from 0 to (0 + 1) % 5 = 1. So front = 1, rear remains 4.
Final Answer:
front = 1, rear = 4 -> Option D
Quick Check:
Enqueue rear+1=4, Dequeue front+1=1 [OK]
Hint: Update rear on enqueue, front on dequeue with modulo [OK]
Common Mistakes:
Not applying modulo for wrap-around
Updating front on enqueue instead of rear
Confusing initial and final pointer positions
4. Given the following TypeScript snippet for dequeue operation in a circular queue, identify the bug:
function dequeue() {
if (front === -1) return null;
const value = queue[front];
if (front === rear) {
front = rear = -1;
} else {
front = front + 1;
}
return value;
}
medium
A. Missing modulo operation when updating front, causing index overflow.
B. Incorrect check for empty queue; should check rear instead of front.
C. Should update rear instead of front after dequeue.
D. Returning value before updating front causes wrong output.
Solution
Step 1: Analyze front update in dequeue
The code updates front as front = front + 1 without modulo, which can exceed array bounds.
Step 2: Correct update with modulo
To wrap around in circular queue, front should be updated as front = (front + 1) % size.
Final Answer:
Missing modulo operation when updating front, causing index overflow. -> Option A
Quick Check:
Front update needs modulo [OK]
Hint: Always use modulo when moving front or rear [OK]
Common Mistakes:
Forgetting modulo causes index errors
Confusing front and rear updates
Wrong empty queue condition check
5. You have a circular queue of size 4. You perform these operations in order: enqueue(5), enqueue(10), enqueue(15), dequeue(), enqueue(20), enqueue(25). What will be the final state of the queue array (index 0 to 3) and the positions of front and rear?