Challenge - 5 Problems
Circular Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Queue Enqueue and Dequeue Operations
What is the printed state of the circular queue after the following operations?
DSA Rust
struct CircularQueue {
data: Vec<Option<i32>>,
front: usize,
rear: usize,
size: usize,
}
impl CircularQueue {
fn new(capacity: usize) -> Self {
CircularQueue {
data: vec![None; capacity],
front: 0,
rear: 0,
size: 0,
}
}
fn enqueue(&mut self, val: i32) -> bool {
if self.size == self.data.len() {
return false; // Queue full
}
self.data[self.rear] = Some(val);
self.rear = (self.rear + 1) % self.data.len();
self.size += 1;
true
}
fn dequeue(&mut self) -> Option<i32> {
if self.size == 0 {
return None; // Queue empty
}
let val = self.data[self.front].take();
self.front = (self.front + 1) % self.data.len();
self.size -= 1;
val
}
fn print_queue(&self) {
let mut idx = self.front;
let mut count = self.size;
let mut result = Vec::new();
while count > 0 {
if let Some(v) = self.data[idx] {
result.push(v);
}
idx = (idx + 1) % self.data.len();
count -= 1;
}
println!("{:?}", result);
}
}
fn main() {
let mut q = CircularQueue::new(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.enqueue(40);
q.enqueue(50);
q.enqueue(60);
q.dequeue();
q.enqueue(70);
q.print_queue();
}Attempts:
2 left
💡 Hint
Track the front and rear pointers carefully after each enqueue and dequeue.
✗ Incorrect
After the operations, the queue contains elements 30, 40, 50, 60, 70 in order. The front pointer moves forward on dequeue, and rear moves forward on enqueue, wrapping around the array.
🧠 Conceptual
intermediateUnderstanding Circular Queue Full Condition
In a circular queue implemented using an array of size N, which condition correctly indicates that the queue is full?
Attempts:
2 left
💡 Hint
Think about how the rear pointer moves and wraps around.
✗ Incorrect
The queue is full when the next position of rear (rear + 1 modulo N) equals front, meaning no space to insert new elements.
🔧 Debug
advancedIdentify the Bug in Circular Queue Dequeue Method
What error will occur when running this dequeue method if the queue is empty?
DSA Rust
fn dequeue(&mut self) -> Option<i32> {
let val = self.data[self.front].take();
self.front = (self.front + 1) % self.data.len();
self.size -= 1;
val
}Attempts:
2 left
💡 Hint
Consider what happens if dequeue is called on an empty queue.
✗ Incorrect
If size is zero, decrementing size causes underflow panic in Rust. The method does not check if queue is empty before decrementing size.
❓ Predict Output
advancedResult of Circular Queue After Wrap-Around Enqueue
What is the printed queue after these operations on a circular queue of size 4?
DSA Rust
let mut q = CircularQueue::new(4); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.dequeue(); q.enqueue(4); q.enqueue(5); q.print_queue();
Attempts:
2 left
💡 Hint
Remember the queue size is 4 and enqueue wraps around after reaching the end.
✗ Incorrect
After dequeue removes 1, enqueue adds 4 and 5 wrapping around. The queue holds 2,3,4,5 in order.
🧠 Conceptual
expertWhy Use Circular Queue Instead of Linear Queue?
Which is the main advantage of using a circular queue over a linear queue implemented with an array?
Attempts:
2 left
💡 Hint
Think about what happens when elements are dequeued in a linear queue.
✗ Incorrect
A circular queue reuses empty slots freed by dequeues, avoiding wasted space unlike a linear queue which can become full even if slots at front are empty.
