Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code that prints a singly linked list?
Consider the following Rust code that creates a singly linked list and prints its elements. What will be printed?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn print_list(head: Option<Rc<RefCell<Node>>>) { let mut current = head; while let Some(node) = current { print!("{} -> ", node.borrow().value); current = node.borrow().next.clone(); } println!("null"); } fn main() { let node3 = Rc::new(RefCell::new(Node { value: 3, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: Some(node3) })); let node1 = Rc::new(RefCell::new(Node { value: 1, next: Some(node2) })); print_list(Some(node1)); }
Attempts:
2 left
💡 Hint
Think about how the list nodes are linked and how the print_list function traverses from head to next nodes.
✗ Incorrect
The list is created with node1 pointing to node2, which points to node3, which points to None. The print_list function starts at node1 and prints values in order: 1, 2, 3, then null.
❓ Predict Output
intermediateWhat is the output after traversing this linked list with a loop?
Given this Rust code snippet that traverses a linked list, what will be printed?
DSA Rust
use std::boxed::Box;
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn print_list(mut head: Option<Box<Node>>) {
while let Some(node) = head {
print!("{} -> ", node.value);
head = node.next;
}
println!("null");
}
fn main() {
let node3 = Box::new(Node { value: 30, next: None });
let node2 = Box::new(Node { value: 20, next: Some(node3) });
let node1 = Box::new(Node { value: 10, next: Some(node2) });
print_list(Some(node1));
}Attempts:
2 left
💡 Hint
The list nodes are linked from node1 to node3. The loop prints from head to next until None.
✗ Incorrect
The list starts at node1 with value 10, then node2 with 20, then node3 with 30, then None. The print_list function prints all values in order.
🔧 Debug
advancedWhat error does this Rust code produce when traversing a linked list?
This Rust code attempts to traverse and print a linked list. What error will it produce when compiled or run?
DSA Rust
use std::boxed::Box;
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head;
while current.is_some() {
println!("{}", current.as_ref().unwrap().value);
current = ¤t.as_ref().unwrap().next;
}
}
fn main() {
let node3 = Box::new(Node { value: 3, next: None });
let node2 = Box::new(Node { value: 2, next: Some(node3) });
let node1 = Box::new(Node { value: 1, next: Some(node2) });
print_list(&Some(node1));
}Attempts:
2 left
💡 Hint
Check if the code borrows correctly and if the print statement uses the right syntax.
✗ Incorrect
The code borrows references correctly and prints values by accessing the Option> references. It compiles and prints 1 2 3 each on a new line.
🧠 Conceptual
advancedHow many nodes are printed by this Rust linked list traversal?
Given this Rust code that traverses a linked list, how many nodes will be printed?
DSA Rust
use std::boxed::Box;
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn print_list(mut head: Option<Box<Node>>) {
while let Some(node) = head {
print!("{} -> ", node.value);
head = node.next;
}
println!("null");
}
fn main() {
let node3 = Box::new(Node { value: 3, next: None });
let node2 = Box::new(Node { value: 2, next: Some(node3) });
let node1 = Box::new(Node { value: 1, next: Some(node2) });
print_list(Some(node1));
}Attempts:
2 left
💡 Hint
Count how many nodes are linked starting from node1.
✗ Incorrect
The list has node1 linked to node2, linked to node3, then None. So 3 nodes are printed.
🚀 Application
expertWhat is the output after reversing and printing this linked list in Rust?
This Rust code reverses a singly linked list and then prints it. What will be the printed output?
DSA Rust
use std::boxed::Box;
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn reverse_list(mut head: Option<Box<Node>>) -> Option<Box<Node>> {
let mut prev = None;
while let Some(mut current) = head {
head = current.next.take();
current.next = prev;
prev = Some(current);
}
prev
}
fn print_list(mut head: Option<Box<Node>>) {
while let Some(node) = head {
print!("{} -> ", node.value);
head = node.next;
}
println!("null");
}
fn main() {
let node3 = Box::new(Node { value: 3, next: None });
let node2 = Box::new(Node { value: 2, next: Some(node3) });
let node1 = Box::new(Node { value: 1, next: Some(node2) });
let reversed = reverse_list(Some(node1));
print_list(reversed);
}Attempts:
2 left
💡 Hint
The reverse_list function reverses the links between nodes before printing.
✗ Incorrect
The reverse_list function changes the next pointers so the list order is reversed. The print_list then prints nodes in reversed order: 3, 2, 1.
