Challenge - 5 Problems
Circular Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Circular Queue Enqueue and Dequeue Operations
What is the printed state of the circular queue after performing the following operations?
Initialize queue with size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50.
Dequeue two elements.
Enqueue 60, 70.
Print the queue elements from front to rear.
Initialize queue with size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50.
Dequeue two elements.
Enqueue 60, 70.
Print the queue elements from front to rear.
DSA Python
class CircularQueue: def __init__(self, size): self.size = size self.queue = [None] * size self.front = -1 self.rear = -1 def enqueue(self, data): if (self.rear + 1) % self.size == self.front: return "Queue is Full" elif self.front == -1: self.front = 0 self.rear = 0 self.queue[self.rear] = data else: self.rear = (self.rear + 1) % self.size self.queue[self.rear] = data def dequeue(self): if self.front == -1: return "Queue is Empty" data = self.queue[self.front] if self.front == self.rear: self.front = -1 self.rear = -1 else: self.front = (self.front + 1) % self.size return data def display(self): if self.front == -1: return [] elements = [] i = self.front while True: elements.append(self.queue[i]) if i == self.rear: break i = (i + 1) % self.size return elements cq = CircularQueue(5) cq.enqueue(10) cq.enqueue(20) cq.enqueue(30) cq.dequeue() cq.enqueue(40) cq.enqueue(50) cq.dequeue() cq.dequeue() cq.enqueue(60) cq.enqueue(70) print(cq.display())
Attempts:
2 left
💡 Hint
Remember that the circular queue wraps around when rear reaches the end of the array.
✗ Incorrect
After the operations, the queue contains 40, 50, 60, 70 in order from front to rear. Earlier elements were dequeued.
🧠 Conceptual
intermediate1:30remaining
Understanding Circular Queue Full Condition
In a circular queue implemented using an array of size 6, which condition correctly checks if the queue is full?
Attempts:
2 left
💡 Hint
Think about how the rear wraps around to the front in a circular queue.
✗ Incorrect
The queue is full when the next position of rear (circularly) is front, meaning no space left.
🔧 Debug
advanced1:30remaining
Identify the Bug in Circular Queue Dequeue Method
What error will occur when running this dequeue method if the queue is empty?
DSA Python
def dequeue(self): if self.front == -1: return "Queue is Empty" data = self.queue[self.front] if self.front == self.rear: self.front = -1 self.rear = -1 else: self.front = (self.front + 1) % self.size return data cq = CircularQueue(3) cq.dequeue()
Attempts:
2 left
💡 Hint
Check what happens when front is -1 and dequeue is called.
✗ Incorrect
The method returns the string 'Queue is Empty' when the queue is empty, no error occurs.
❓ Predict Output
advanced2:00remaining
Result of Circular Queue After Wrap-Around Enqueue
Given a circular queue of size 4, what is the queue content after these operations?
Enqueue 1, 2, 3.
Dequeue one element.
Enqueue 4.
Enqueue 5.
Print the queue elements from front to rear.
Enqueue 1, 2, 3.
Dequeue one element.
Enqueue 4.
Enqueue 5.
Print the queue elements from front to rear.
DSA Python
cq = CircularQueue(4) cq.enqueue(1) cq.enqueue(2) cq.enqueue(3) cq.dequeue() cq.enqueue(4) cq.enqueue(5) print(cq.display())
Attempts:
2 left
💡 Hint
Remember the queue size is 4 and it wraps around when full.
✗ Incorrect
After dequeuing 1, the queue has 2,3. Enqueue 4 and 5 fills the queue to [2,3,4,5].
🚀 Application
expert1:30remaining
Maximum Number of Elements in Circular Queue
If a circular queue is implemented using an array of size N, what is the maximum number of elements it can hold without resizing?
Attempts:
2 left
💡 Hint
One slot is kept empty to distinguish full and empty states.
✗ Incorrect
To differentiate full and empty states, one slot remains unused, so max elements = N - 1.