Challenge - 5 Problems
Linked List Queue Master
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 linked list queue initially empty. We enqueue the values 10, 20, and 30 in that order. What is the printed state of the queue after these operations?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct Queue {
front: Option<Box<Node>>,
rear: *mut Node,
}
impl Queue {
fn new() -> Self {
Queue { front: None, rear: std::ptr::null_mut() }
}
fn enqueue(&mut self, val: i32) {
let mut new_node = Box::new(Node { data: val, next: None });
let raw_node: *mut _ = &mut *new_node;
if self.rear.is_null() {
self.front = Some(new_node);
self.rear = raw_node;
} else {
unsafe {
(*self.rear).next = Some(new_node);
}
self.rear = raw_node;
}
}
fn print_queue(&self) {
let mut current = &self.front;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.print_queue();
}Attempts:
2 left
💡 Hint
Remember enqueue adds to the rear of the queue, so order is preserved.
✗ Incorrect
Enqueue adds elements at the rear. So after adding 10, 20, and 30, the queue prints in the order 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat happens if we enqueue on an empty queue?
Given the same Queue struct, what is the printed state after enqueueing a single value 42 into an empty queue?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct Queue {
front: Option<Box<Node>>,
rear: *mut Node,
}
impl Queue {
fn new() -> Self {
Queue { front: None, rear: std::ptr::null_mut() }
}
fn enqueue(&mut self, val: i32) {
let mut new_node = Box::new(Node { data: val, next: None });
let raw_node: *mut _ = &mut *new_node;
if self.rear.is_null() {
self.front = Some(new_node);
self.rear = raw_node;
} else {
unsafe {
(*self.rear).next = Some(new_node);
}
self.rear = raw_node;
}
}
fn print_queue(&self) {
let mut current = &self.front;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(42);
q.print_queue();
}Attempts:
2 left
💡 Hint
Enqueue on empty queue sets front and rear to the new node.
✗ Incorrect
When the queue is empty, enqueue sets both front and rear to the new node containing 42, so the queue prints 42 -> null.
🔧 Debug
advancedWhy does this enqueue implementation cause a runtime error?
This Rust code for enqueueing in a linked list queue compiles but causes a runtime error. What is the cause?
DSA Rust
fn enqueue(&mut self, val: i32) {
let new_node = Box::new(Node { data: val, next: None });
if self.rear.is_null() {
self.front = Some(new_node);
self.rear = &mut *new_node;
} else {
unsafe {
(*self.rear).next = Some(new_node);
}
self.rear = &mut *new_node;
}
}Attempts:
2 left
💡 Hint
Think about ownership and lifetimes of Box in Rust.
✗ Incorrect
The new_node is a local variable and is dropped at the end of the function, so rear points to invalid memory causing runtime error.
🧠 Conceptual
advancedWhat is the time complexity of enqueue in a linked list queue?
If a queue is implemented using a linked list with pointers to front and rear, what is the time complexity of the enqueue operation?
Attempts:
2 left
💡 Hint
Enqueue adds at the rear directly using a pointer.
✗ Incorrect
With a rear pointer, enqueue adds a node at the end in constant time without traversal.
❓ Predict Output
expertWhat is the queue state after mixed enqueue operations?
Given this Rust code snippet for enqueueing values 5, 15, 25, then 35, what is the printed queue state?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct Queue {
front: Option<Box<Node>>,
rear: *mut Node,
}
impl Queue {
fn new() -> Self {
Queue { front: None, rear: std::ptr::null_mut() }
}
fn enqueue(&mut self, val: i32) {
let mut new_node = Box::new(Node { data: val, next: None });
let raw_node: *mut _ = &mut *new_node;
if self.rear.is_null() {
self.front = Some(new_node);
self.rear = raw_node;
} else {
unsafe {
(*self.rear).next = Some(new_node);
}
self.rear = raw_node;
}
}
fn print_queue(&self) {
let mut current = &self.front;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(5);
q.enqueue(15);
q.enqueue(25);
q.enqueue(35);
q.print_queue();
}Attempts:
2 left
💡 Hint
Enqueue adds at the rear, preserving order.
✗ Incorrect
Enqueue adds nodes at the rear, so the queue prints in the order 5 -> 15 -> 25 -> 35 -> null.
