Challenge - 5 Problems
Middle Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the middle element output for odd length list
What is the output of the code when finding the middle element of a linked list with 5 nodes?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn find_middle(head: &Option<Box<Node>>) -> Option<i32> {
let mut slow = head;
let mut fast = head;
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
slow = slow.as_ref().unwrap().next.as_ref();
fast = fast.as_ref().unwrap().next.as_ref().unwrap().next.as_ref();
}
slow.map(|node| node.value)
}
fn main() {
let node5 = Node { value: 5, next: None };
let node4 = Node { value: 4, next: Some(Box::new(node5)) };
let node3 = Node { value: 3, next: Some(Box::new(node4)) };
let node2 = Node { value: 2, next: Some(Box::new(node3)) };
let node1 = Node { value: 1, next: Some(Box::new(node2)) };
let head = Some(Box::new(node1));
println!("{}", find_middle(&head).unwrap());
}Attempts:
2 left
💡 Hint
Use two pointers: one moves one step, the other moves two steps.
✗ Incorrect
The slow pointer moves one node at a time, the fast pointer moves two nodes. When fast reaches the end, slow is at the middle. For 5 nodes, middle is the 3rd node with value 3.
❓ Predict Output
intermediateFind the middle element output for even length list
What is the output of the code when finding the middle element of a linked list with 6 nodes?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn find_middle(head: &Option<Box<Node>>) -> Option<i32> {
let mut slow = head;
let mut fast = head;
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
slow = slow.as_ref().unwrap().next.as_ref();
fast = fast.as_ref().unwrap().next.as_ref().unwrap().next.as_ref();
}
slow.map(|node| node.value)
}
fn main() {
let node6 = Node { value: 6, next: None };
let node5 = Node { value: 5, next: Some(Box::new(node6)) };
let node4 = Node { value: 4, next: Some(Box::new(node5)) };
let node3 = Node { value: 3, next: Some(Box::new(node4)) };
let node2 = Node { value: 2, next: Some(Box::new(node3)) };
let node1 = Node { value: 1, next: Some(Box::new(node2)) };
let head = Some(Box::new(node1));
println!("{}", find_middle(&head).unwrap());
}Attempts:
2 left
💡 Hint
For even length, the function returns the second middle node.
✗ Incorrect
The slow pointer ends at the 4th node for 6 nodes, so the middle element returned is 4.
🧠 Conceptual
advancedWhy use two pointers to find the middle element?
Why is the two-pointer technique (slow and fast pointers) efficient for finding the middle element of a linked list?
Attempts:
2 left
💡 Hint
Think about how many times the list is traversed and memory used.
✗ Incorrect
The two-pointer method finds the middle in one pass with constant extra space, making it efficient.
🔧 Debug
advancedIdentify the bug in this middle element finder
What error will this Rust code produce when trying to find the middle element of a linked list?
DSA Rust
fn find_middle(head: &Option<Box<Node>>) -> Option<i32> {
let mut slow = head;
let mut fast = head;
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
slow = &slow.as_ref().unwrap().next;
fast = &fast.as_ref().unwrap().next;
}
slow.as_ref().map(|node| node.value)
}Attempts:
2 left
💡 Hint
Check how fast pointer moves inside the loop.
✗ Incorrect
Fast pointer moves only one step instead of two, causing infinite loop.
🚀 Application
expertFind middle element in a circular linked list
Given a circular singly linked list where the last node points back to the head, which approach correctly finds the middle element?
Attempts:
2 left
💡 Hint
In circular lists, there is no None; use head to detect cycle.
✗ Incorrect
Stopping when fast or fast.next equals head prevents infinite loop and finds middle correctly.
