Challenge - 5 Problems
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code with linked list nodes?
Consider the following Rust code that creates a simple singly linked list with nodes holding integers. 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 node3 = Rc::new(RefCell::new(Node { value: 3, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: Some(Rc::clone(&node3)) })); let node1 = Rc::new(RefCell::new(Node { value: 1, next: Some(Rc::clone(&node2)) })); let mut current = Some(Rc::clone(&node1)); while let Some(node) = current { print!("{} -> ", node.borrow().value); current = node.borrow().next.clone(); } print!("null"); }
Attempts:
2 left
💡 Hint
Trace the nodes starting from node1 following the next pointers.
✗ Incorrect
The code creates three nodes linked from node1 to node3. The loop prints values from node1 to node3 in order, ending with null.
❓ Predict Output
intermediateWhat error does this Rust code produce when creating a cyclic linked list?
This Rust code tries to create a cycle in a linked list using Rc and RefCell. What error or behavior will it produce?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let node1 = Rc::new(RefCell::new(Node { value: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: Some(Rc::clone(&node1)) })); // Creating cycle: node1.next points to node2 node1.borrow_mut().next = Some(Rc::clone(&node2)); println!("Created cyclic list"); }
Attempts:
2 left
💡 Hint
Rc and RefCell allow runtime mutable borrows; cycle creation is possible but dangerous.
✗ Incorrect
The code compiles and runs printing the message. However, traversing this cyclic list would cause infinite loops or stack overflow.
🔧 Debug
advancedWhy does this Rust code cause a compile error with node pointers?
Identify the cause of the compile error in this Rust code that tries to create a linked list node with a next pointer.
DSA Rust
#[derive(Debug)] struct Node { value: i32, next: Option<Box<Node>>, } fn main() { let node1 = Node { value: 1, next: None }; let node2 = Node { value: 2, next: Some(Box::new(node1)) }; println!("{:?}", node2); }
Attempts:
2 left
💡 Hint
Check ownership rules when moving node1 into node2.
✗ Incorrect
node1 is moved into node2's next field, so node1 cannot be used again. The code tries to use node1 after move causing compile error.
❓ Predict Output
advancedWhat is the output of this Rust code using Rc and Weak pointers?
This Rust code creates nodes with Rc and Weak pointers to avoid cycles. What will be printed?
DSA Rust
use std::rc::{Rc, Weak};
use std::cell::RefCell;
#[derive(Debug)]
struct Node {
value: i32,
next: Option<Rc<RefCell<Node>>>,
prev: Option<Weak<RefCell<Node>>>,
}
fn main() {
let node1 = Rc::new(RefCell::new(Node { value: 1, next: None, prev: None }));
let node2 = Rc::new(RefCell::new(Node { value: 2, next: None, prev: Some(Rc::downgrade(&node1)) }));
node1.borrow_mut().next = Some(Rc::clone(&node2));
let prev_of_node2 = node2.borrow().prev.as_ref().unwrap().upgrade();
match prev_of_node2 {
Some(prev_node) => println!("Prev value: {}", prev_node.borrow().value),
None => println!("No previous node"),
}
}Attempts:
2 left
💡 Hint
Weak pointers can be upgraded to Rc if the original Rc exists.
✗ Incorrect
node2.prev is a Weak pointer to node1. Upgrading it succeeds because node1 is alive, so it prints Prev value: 1.
🧠 Conceptual
expertWhich pointer design prevents memory leaks in cyclic Rust data structures?
In Rust, when designing node structures that can form cycles, which pointer type combination is best to prevent memory leaks?
Attempts:
2 left
💡 Hint
Think about how Rust's reference counting works and how cycles affect it.
✗ Incorrect
Using Rc for next pointers and Weak for back pointers breaks cycles, allowing memory to be freed properly.
