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 Circular Linked List Detection Function
What is the output of the following Rust code that checks if a linked list is circular?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn is_circular(head: Option<Rc<RefCell<Node>>>) -> bool { let mut slow = head.clone(); let mut fast = head.clone(); while let (Some(s), Some(f)) = (slow.clone(), fast.clone()) { let f_next = f.borrow().next.clone(); if f_next.is_none() { return false; } fast = f_next.unwrap().borrow().next.clone(); slow = s.borrow().next.clone(); if slow.is_some() && fast.is_some() && Rc::ptr_eq(slow.as_ref().unwrap(), fast.as_ref().unwrap()) { return true; } } false } fn main() { let node1 = Rc::new(RefCell::new(Node { value: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: None })); let node3 = Rc::new(RefCell::new(Node { value: 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 println!("{}", is_circular(Some(node1))); }
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and meet in a circular linked list.
✗ Incorrect
The code uses Floyd's cycle detection algorithm. Since node3 points back to node1, the list is circular, so the function returns true.
❓ Predict Output
intermediateOutput when Linked List is Not Circular
What will the output be when the linked list has no cycle in the following Rust code?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn is_circular(head: Option<Rc<RefCell<Node>>>) -> bool { let mut slow = head.clone(); let mut fast = head.clone(); while let (Some(s), Some(f)) = (slow.clone(), fast.clone()) { let f_next = f.borrow().next.clone(); if f_next.is_none() { return false; } fast = f_next.unwrap().borrow().next.clone(); slow = s.borrow().next.clone(); if slow.is_some() && fast.is_some() && Rc::ptr_eq(slow.as_ref().unwrap(), fast.as_ref().unwrap()) { return true; } } false } fn main() { let node1 = Rc::new(RefCell::new(Node { value: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: None })); let node3 = Rc::new(RefCell::new(Node { value: 3, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node3.clone()); node3.borrow_mut().next = None; // No cycle println!("{}", is_circular(Some(node1))); }
Attempts:
2 left
💡 Hint
If the fast pointer reaches the end, the list is not circular.
✗ Incorrect
Since node3's next is None, the fast pointer reaches the end and the function returns false.
🔧 Debug
advancedIdentify the Bug in Circular Linked List Detection
What error will the following Rust code produce when trying to detect a cycle in a linked list?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn is_circular(head: Option<Rc<RefCell<Node>>>) -> bool { let mut slow = head.clone(); let mut fast = head.clone(); while fast.is_some() && fast.as_ref().unwrap().borrow().next.is_some() { slow = slow.unwrap().borrow().next.clone(); fast = fast.unwrap().borrow().next.clone().unwrap().borrow().next.clone(); if slow.is_some() && fast.is_some() && Rc::ptr_eq(slow.as_ref().unwrap(), fast.as_ref().unwrap()) { return true; } } false } fn main() { let node1 = Rc::new(RefCell::new(Node { value: 1, next: None })); let node2 = Rc::new(RefCell::new(Node { value: 2, next: None })); let node3 = Rc::new(RefCell::new(Node { value: 3, next: None })); node1.borrow_mut().next = Some(node2.clone()); node2.borrow_mut().next = Some(node3.clone()); node3.borrow_mut().next = None; println!("{}", is_circular(Some(node1))); }
Attempts:
2 left
💡 Hint
Check how unwrap is used and if fast pointer always has next nodes.
✗ Incorrect
The code safely checks fast and fast.next before unwrap, so no panic occurs. The list is not circular, so it returns false correctly.
🧠 Conceptual
advancedWhy Use Two Pointers to Detect Circular Linked List?
Why does Floyd's cycle detection algorithm use two pointers moving at different speeds to detect a cycle in a linked list?
Attempts:
2 left
💡 Hint
Think about how two runners on a circular track eventually meet.
✗ Incorrect
The fast pointer moves two steps and the slow pointer moves one step. If a cycle exists, they will meet inside the loop.
❓ Predict Output
expertOutput of Circular Linked List Detection with Self-Loop Node
What is the output of the following Rust code where a node points to itself, forming a self-loop?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct Node { value: i32, next: Option<Rc<RefCell<Node>>>, } fn is_circular(head: Option<Rc<RefCell<Node>>>) -> bool { let mut slow = head.clone(); let mut fast = head.clone(); while let (Some(s), Some(f)) = (slow.clone(), fast.clone()) { let f_next = f.borrow().next.clone(); if f_next.is_none() { return false; } fast = f_next.unwrap().borrow().next.clone(); slow = s.borrow().next.clone(); if slow.is_some() && fast.is_some() && Rc::ptr_eq(slow.as_ref().unwrap(), fast.as_ref().unwrap()) { return true; } } false } fn main() { let node1 = Rc::new(RefCell::new(Node { value: 1, next: None })); node1.borrow_mut().next = Some(node1.clone()); // Self-loop println!("{}", is_circular(Some(node1))); }
Attempts:
2 left
💡 Hint
A node pointing to itself forms a cycle of length one.
✗ Incorrect
The slow and fast pointers both start at the same node and meet immediately, so the function returns true.
