Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at beginning in circular linked list
What is the printed state of the circular linked list after inserting 10, then 20, then 30 at the beginning?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularLinkedList {
head: Option<Box<Node>>,
}
impl CircularLinkedList {
fn new() -> Self {
CircularLinkedList { head: None }
}
fn insert_at_beginning(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node.clone());
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}
fn print_list(&self) {
if self.head.is_none() {
println!("List is empty");
return;
}
let mut temp = self.head.as_ref();
loop {
print!("{} -> ", temp.unwrap().data);
temp = temp.unwrap().next.as_ref();
if temp.unwrap().data == self.head.as_ref().unwrap().data {
break;
}
}
println!("null");
}
}
fn main() {
let mut cll = CircularLinkedList::new();
cll.insert_at_beginning(10);
cll.insert_at_beginning(20);
cll.insert_at_beginning(30);
cll.print_list();
}Attempts:
2 left
💡 Hint
Remember, inserting at beginning means the newest node becomes the head and the last node points to it.
✗ Incorrect
Each new node is inserted before the current head, and the last node's next pointer is updated to point to the new head, maintaining circularity. So after inserting 30, 20, 10 in that order at beginning, the list is 30 -> 20 -> 10 -> null.
❓ Predict Output
intermediateOutput after inserting a single node in empty circular linked list
What is the printed state of the circular linked list after inserting only one node with value 5 at the beginning?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularLinkedList {
head: Option<Box<Node>>,
}
impl CircularLinkedList {
fn new() -> Self {
CircularLinkedList { head: None }
}
fn insert_at_beginning(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node.clone());
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}
fn print_list(&self) {
if self.head.is_none() {
println!("List is empty");
return;
}
let mut temp = self.head.as_ref();
loop {
print!("{} -> ", temp.unwrap().data);
temp = temp.unwrap().next.as_ref();
if temp.unwrap().data == self.head.as_ref().unwrap().data {
break;
}
}
println!("null");
}
}
fn main() {
let mut cll = CircularLinkedList::new();
cll.insert_at_beginning(5);
cll.print_list();
}Attempts:
2 left
💡 Hint
In a circular linked list with one node, the node points to itself.
✗ Incorrect
When only one node exists, its next pointer points to itself, so printing it once and stopping when we reach the head again prints the node twice in the loop before breaking.
🔧 Debug
advancedIdentify the bug in insert_at_beginning method
Which option correctly describes the bug in this insert_at_beginning method for circular linked list?
DSA Rust
fn insert_at_beginning(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node.clone());
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}Attempts:
2 left
💡 Hint
Look at the line where new_node.next is assigned when list is empty.
✗ Incorrect
Cloning new_node inside its own initialization causes infinite recursion because Box does not implement Clone by default and cloning a Box containing itself is invalid, leading to stack overflow.
❓ Predict Output
advancedOutput after multiple insertions and one traversal
After inserting 1, 2, 3 at beginning in that order, what is the output of one traversal starting from head?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct CircularLinkedList {
head: Option<Box<Node>>,
}
impl CircularLinkedList {
fn new() -> Self {
CircularLinkedList { head: None }
}
fn insert_at_beginning(&mut self, data: i32) {
let mut new_node = Box::new(Node { data, next: None });
if self.head.is_none() {
new_node.next = Some(new_node.clone());
self.head = Some(new_node);
} else {
let mut temp = self.head.as_mut().unwrap();
while temp.next.as_ref().unwrap().data != self.head.as_ref().unwrap().data {
temp = temp.next.as_mut().unwrap();
}
new_node.next = self.head.take();
temp.next = Some(new_node);
self.head = temp.next.clone();
}
}
fn traverse_once(&self) -> Vec<i32> {
let mut result = Vec::new();
if self.head.is_none() {
return result;
}
let mut temp = self.head.as_ref();
loop {
result.push(temp.unwrap().data);
temp = temp.unwrap().next.as_ref();
if temp.unwrap().data == self.head.as_ref().unwrap().data {
break;
}
}
result
}
}
fn main() {
let mut cll = CircularLinkedList::new();
cll.insert_at_beginning(1);
cll.insert_at_beginning(2);
cll.insert_at_beginning(3);
let output = cll.traverse_once();
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Inserting at beginning reverses the order of insertion.
✗ Incorrect
Each new node becomes the head, so after inserting 1, 2, 3 at beginning, the list order is 3 -> 2 -> 1.
🧠 Conceptual
expertWhy update last node's next pointer when inserting at beginning?
In a circular linked list, why must we update the last node's next pointer when inserting a new node at the beginning?
Attempts:
2 left
💡 Hint
Think about what keeps the list circular after changing the head.
✗ Incorrect
The last node's next pointer must point to the new head node to keep the list circular. Without this update, the last node would still point to the old head, breaking the circular structure.
