Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of traversing a circular linked list once
What is the printed output of the following Rust code that traverses a circular linked list exactly once?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { data: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let node1 = Rc::new(RefCell::new(Node { data: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { data: 2, next: None })); let node3 = Rc::new(RefCell::new(Node { data: 3, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node3.clone()); node3.borrow_mut().next = Some(node1.clone()); // circular link let mut current = node1.clone(); print!("Traversal: "); loop { print!("{} -> ", current.borrow().data); current = current.borrow().next.as_ref().unwrap().clone(); if Rc::ptr_eq(¤t, &node1) { break; } } print!("null"); }
Attempts:
2 left
💡 Hint
Think about stopping the traversal when you reach the starting node again.
✗ Incorrect
The code prints each node's data starting from node1 and stops when it reaches node1 again, so it prints 1, 2, 3 and then stops, printing 'null' at the end.
❓ Predict Output
intermediateOutput when traversing circular linked list with 4 nodes
What is the output of this Rust code that traverses a circular linked list with 4 nodes once?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { data: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let node1 = Rc::new(RefCell::new(Node { data: 10, next: None })); let node2 = Rc::new(RefCell::new(Node { data: 20, next: None })); let node3 = Rc::new(RefCell::new(Node { data: 30, next: None })); let node4 = Rc::new(RefCell::new(Node { data: 40, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node3.clone()); node3.borrow_mut().next = Some(node4.clone()); node4.borrow_mut().next = Some(node1.clone()); // circular link let mut current = node1.clone(); print!("Traversal: "); loop { print!("{} -> ", current.borrow().data); current = current.borrow().next.as_ref().unwrap().clone(); if Rc::ptr_eq(¤t, &node1) { break; } } print!("null"); }
Attempts:
2 left
💡 Hint
Remember to stop when you reach the start node again.
✗ Incorrect
The loop prints all 4 nodes once and stops when it reaches the first node again, so it prints 10, 20, 30, 40 and then 'null'.
🔧 Debug
advancedIdentify the error in circular linked list traversal code
What error will this Rust code produce when trying to traverse a circular linked list?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { data: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let node1 = Rc::new(RefCell::new(Node { data: 5, next: None })); let node2 = Rc::new(RefCell::new(Node { data: 6, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node1.clone()); // circular link let mut current = node1.clone(); print!("Traversal: "); while current.borrow().next.is_some() { print!("{} -> ", current.borrow().data); current = current.borrow().next.as_ref().unwrap().clone(); } print!("null"); }
Attempts:
2 left
💡 Hint
Check the loop condition and how it handles circular links.
✗ Incorrect
The loop condition checks if next is Some, which is always true in a circular list, so it never stops, causing an infinite loop.
🧠 Conceptual
advancedWhy use Rc and RefCell in Rust circular linked list?
Why do we use Rc> for nodes in a circular linked list in Rust?
Attempts:
2 left
💡 Hint
Think about how circular references and mutability work in Rust.
✗ Incorrect
Rc allows multiple owners of the same node (needed for circular links), and RefCell allows mutating the node even when it is inside an Rc.
❓ Predict Output
expertOutput after modifying data during circular linked list traversal
What is the output of this Rust code that modifies node data during traversal of a circular linked list?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { data: i32, next: Option<Rc<RefCell<Node>>>, } fn main() { let node1 = Rc::new(RefCell::new(Node { data: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { data: 2, next: None })); let node3 = Rc::new(RefCell::new(Node { data: 3, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node3.clone()); node3.borrow_mut().next = Some(node1.clone()); let mut current = node1.clone(); print!("Traversal after increment: "); loop { let mut node_ref = current.borrow_mut(); node_ref.data += 10; print!("{} -> ", node_ref.data); drop(node_ref); current = current.borrow().next.as_ref().unwrap().clone(); if Rc::ptr_eq(¤t, &node1) { break; } } print!("null"); }
Attempts:
2 left
💡 Hint
Each node's data is increased by 10 exactly once during traversal.
✗ Incorrect
The code adds 10 to each node's data during traversal, so original 1,2,3 become 11,12,13 respectively.
