Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 4 at the end?
Given a circular linked list with nodes 1 -> 2 -> 3 -> back to 1, what is the printed state after inserting 4 at the end?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularLinkedList {
tail: Option<Box<Node>>,
}
impl CircularLinkedList {
fn new() -> Self {
CircularLinkedList { tail: None }
}
fn insert_end(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.tail.is_none() {
new_node.next = Some(new_node.clone());
self.tail = Some(new_node);
} else {
let tail = self.tail.as_mut().unwrap();
new_node.next = tail.next.take();
tail.next = Some(new_node);
self.tail = tail.next.as_mut().map(|node| node.clone());
}
}
fn print_list(&self) {
if self.tail.is_none() {
println!("List is empty");
return;
}
let mut temp = self.tail.as_ref().unwrap().next.as_ref();
loop {
print!("{} -> ", temp.unwrap().data);
if temp.unwrap().data == self.tail.as_ref().unwrap().data {
break;
}
temp = temp.unwrap().next.as_ref();
}
println!("back to {}", self.tail.as_ref().unwrap().data);
}
}
fn main() {
let mut cll = CircularLinkedList::new();
cll.insert_end(1);
cll.insert_end(2);
cll.insert_end(3);
cll.insert_end(4);
cll.print_list();
}Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points back to the first node.
✗ Incorrect
After inserting 4 at the end, the list becomes 1 -> 2 -> 3 -> 4, and since it's circular, 4 points back to 1.
🧠 Conceptual
intermediateWhy update the tail pointer when inserting at the end?
In a circular linked list, why do we update the tail pointer after inserting a new node at the end?
Attempts:
2 left
💡 Hint
Think about how to quickly add nodes at the end without traversing the whole list.
✗ Incorrect
Updating the tail pointer ensures we always know the last node, making insertion at the end efficient.
🔧 Debug
advancedIdentify the error in this insert_end implementation
What error will this Rust code cause when inserting at the end of a circular linked list?
DSA Rust
fn insert_end(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.tail.is_none() {
new_node.next = Some(new_node.clone());
self.tail = Some(new_node);
} else {
let tail = self.tail.as_mut().unwrap();
new_node.next = tail.next.take();
tail.next = Some(new_node);
// Missing update of tail pointer here
}
}Attempts:
2 left
💡 Hint
Check what happens to the tail pointer after insertion.
✗ Incorrect
Without updating the tail pointer, the last node is not updated, breaking the circular link.
📝 Syntax
advancedWhich option correctly sets the new node's next pointer?
In Rust, how do you correctly set the new node's next pointer to the current head in a circular linked list?
Attempts:
2 left
💡 Hint
Consider ownership and borrowing rules in Rust.
✗ Incorrect
Using take() moves the ownership of the next node to new_node.next safely.
🚀 Application
expertAfter multiple insertions, how many nodes are in the circular linked list?
Starting from an empty circular linked list, you insert nodes with values 5, 10, 15, 20 at the end. How many nodes does the list contain?
Attempts:
2 left
💡 Hint
Each insertion adds one node at the end.
✗ Incorrect
Each insert_end call adds one node, so after 4 insertions, there are 4 nodes.
