Challenge - 5 Problems
Singly Linked List 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 creating a singly linked list?
Consider the following Rust code that creates a singly linked list with three nodes containing values 10, 20, and 30 respectively. What will be printed after running this code?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let third = Rc::new(RefCell::new(Node { value: 30, next: None })); let second = Rc::new(RefCell::new(Node { value: 20, next: Some(third.clone()) })); let first = Rc::new(RefCell::new(Node { value: 10, next: Some(second.clone()) })); let mut current = Some(first.clone()); while let Some(node) = current { print!("{} -> ", node.borrow().value); current = node.borrow().next.clone(); } print!("null\n"); }
Attempts:
2 left
💡 Hint
Trace the list starting from the first node and follow the next pointers until None.
✗ Incorrect
The list is created starting with the third node (30), then the second node (20) points to third, and the first node (10) points to second. Printing starts from first, so the output is 10 -> 20 -> 30 -> null.
🧠 Conceptual
intermediateWhat is the main advantage of using Rc> in Rust for singly linked lists?
In Rust, why do we often use Rc> to implement singly linked lists instead of just Box?
Attempts:
2 left
💡 Hint
Think about ownership and mutability rules in Rust and how linked lists need to share nodes.
✗ Incorrect
Rc allows multiple parts of the program to own the same node, and RefCell allows mutating the node even when it is shared. Box alone does not allow multiple ownership or interior mutability.
🔧 Debug
advancedWhat error does this Rust code produce when creating a singly linked list?
This Rust code attempts to create a singly linked list but fails to compile. What is the error message?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn main() {
let first = Node { value: 1, next: None };
let second = Node { value: 2, next: Some(first) };
}Attempts:
2 left
💡 Hint
Look at the type expected for next and the type provided.
✗ Incorrect
The field next expects Option>, but Some(first) provides Option. The compiler complains about mismatched types because first is not boxed.
🚀 Application
advancedAfter inserting nodes 5, 10, 15 in order, what is the linked list output?
You create an empty singly linked list and insert nodes with values 5, 10, and 15 at the end in that order. What is the printed output of the list?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, val: i32) {
let new_node = Box::new(Node { value: val, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn print_list(&self) {
let mut current = self.head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.value);
current = node.next.as_ref();
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.append(5);
list.append(10);
list.append(15);
list.print_list();
}Attempts:
2 left
💡 Hint
Appending adds nodes at the end, so the order is the same as insertion.
✗ Incorrect
Appending nodes 5, 10, 15 adds them in that order at the end of the list. Printing traverses from head to tail, so output is 5 -> 10 -> 15 -> null.
🧠 Conceptual
expertWhy is implementing a safe mutable singly linked list in Rust challenging?
Rust's ownership and borrowing rules make implementing a mutable singly linked list tricky. Which reason best explains this challenge?
Attempts:
2 left
💡 Hint
Think about Rust's borrowing rules and how linked lists need to update pointers.
✗ Incorrect
Rust's ownership model prevents having multiple mutable references at the same time, but linked lists often need to mutate nodes while traversing or inserting, which conflicts with these rules.
