Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after enqueue operations?
Consider a queue implemented using a vector in Rust. What will be the printed state of the queue after the following enqueue operations?
DSA Rust
struct Queue {
elements: Vec<i32>,
}
impl Queue {
fn new() -> Self {
Queue { elements: Vec::new() }
}
fn enqueue(&mut self, value: i32) {
self.elements.push(value);
}
fn print(&self) {
for elem in &self.elements {
print!("{} -> ", elem);
}
println!("null");
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.print();
}Attempts:
2 left
💡 Hint
Remember enqueue adds elements at the end of the queue.
✗ Incorrect
Enqueue operation adds elements to the back of the queue. So after adding 10, 20, and 30 in order, the queue prints 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat error occurs if enqueue method is missing &mut self?
In Rust, what error will this code produce if the enqueue method is defined without &mut self but tries to push to the vector?
DSA Rust
struct Queue {
elements: Vec<i32>,
}
impl Queue {
fn enqueue(self, value: i32) {
self.elements.push(value);
}
}
fn main() {
let mut q = Queue { elements: vec![] };
q.enqueue(5);
}Attempts:
2 left
💡 Hint
Pushing to a vector requires mutable access to self.
✗ Incorrect
The enqueue method takes self by value and tries to mutate self.elements. Since self is moved, the vector is owned but the push is allowed. However, because self is moved, calling enqueue consumes the queue, so subsequent use causes a use-after-move error.
🔧 Debug
advancedWhy does this enqueue implementation cause a runtime panic?
Given this queue implementation, why does calling enqueue cause a panic at runtime?
DSA Rust
struct Queue {
elements: Vec<i32>,
front: usize,
}
impl Queue {
fn new() -> Self {
Queue { elements: Vec::new(), front: 0 }
}
fn enqueue(&mut self, value: i32) {
self.elements[self.front] = value;
self.front += 1;
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(1);
}Attempts:
2 left
💡 Hint
Accessing vector by index requires that index to exist.
✗ Incorrect
The elements vector is empty initially. Accessing elements[self.front] tries to access index 0 which does not exist, causing panic.
❓ Predict Output
advancedWhat is the queue state after these enqueue and dequeue operations?
Given this queue implementation with enqueue and dequeue, what is printed after these operations?
DSA Rust
struct Queue {
elements: Vec<i32>,
}
impl Queue {
fn new() -> Self {
Queue { elements: Vec::new() }
}
fn enqueue(&mut self, value: i32) {
self.elements.push(value);
}
fn dequeue(&mut self) -> Option<i32> {
if self.elements.is_empty() {
None
} else {
Some(self.elements.remove(0))
}
}
fn print(&self) {
for elem in &self.elements {
print!("{} -> ", elem);
}
println!("null");
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(5);
q.enqueue(10);
q.enqueue(15);
q.dequeue();
q.enqueue(20);
q.print();
}Attempts:
2 left
💡 Hint
Dequeue removes the first element, enqueue adds at the end.
✗ Incorrect
After enqueue 5,10,15 queue is [5,10,15]. Dequeue removes 5, queue is [10,15]. Enqueue 20 adds at end, queue is [10,15,20].
🧠 Conceptual
expertWhat is the time complexity of enqueue operation in a queue implemented with a linked list?
Consider a queue implemented using a singly linked list with pointers to both head and tail nodes. What is the time complexity of the enqueue operation?
Attempts:
2 left
💡 Hint
Having a tail pointer allows direct access to the end.
✗ Incorrect
With a tail pointer, enqueue adds a node at the tail in constant time without traversal.
