Challenge - 5 Problems
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the intersection node value
Given two singly linked lists that intersect, what is the value of the node where they intersect?
DSA Rust
use std::rc::Rc;
struct ListNode {
val: i32,
next: Option<Rc<ListNode>>,
}
// List A: 1 -> 3 -> 5 -> 7 -> 9 -> null
// List B: 2 -> 4 -> 7 -> 9 -> null
// Intersection starts at node with value 7
fn get_intersection_node_value(
head_a: &Option<Rc<ListNode>>,
head_b: &Option<Rc<ListNode>>,
) -> Option<i32> {
use std::collections::HashSet;
let mut nodes = HashSet::new();
let mut current_a = head_a;
while let Some(node) = current_a {
nodes.insert(&**node as *const ListNode);
current_a = &node.next;
}
let mut current_b = head_b;
while let Some(node) = current_b {
if nodes.contains(&**node as *const ListNode) {
return Some(node.val);
}
current_b = &node.next;
}
None
}
fn main() {
// Assume intersection node is shared
let intersect = Rc::new(ListNode { val: 7, next: Some(Rc::new(ListNode { val: 9, next: None })) });
let list_a = Some(Rc::new(ListNode {
val: 1,
next: Some(Rc::new(ListNode {
val: 3,
next: Some(Rc::new(ListNode {
val: 5,
next: Some(intersect.clone()),
})),
})),
}));
let list_b = Some(Rc::new(ListNode {
val: 2,
next: Some(Rc::new(ListNode {
val: 4,
next: Some(intersect.clone()),
})),
}));
let result = get_intersection_node_value(&list_a, &list_b);
println!("{}", result.unwrap_or(-1));
}Attempts:
2 left
💡 Hint
Think about how to detect the first common node by comparing node addresses.
✗ Incorrect
The intersection node is the first node shared by both lists, which has value 7.
🧠 Conceptual
intermediateWhy do two linked lists intersect at a node?
What is the main reason two singly linked lists can share an intersection node?
Attempts:
2 left
💡 Hint
Think about what it means for two lists to intersect physically, not just by value.
✗ Incorrect
Two linked lists intersect if they share the exact same node object in memory from some point onward.
🔧 Debug
advancedIdentify the error in intersection detection code
What error will this Rust code produce when trying to find the intersection node of two linked lists?
DSA Rust
fn get_intersection_node(
head_a: &Option<Box<ListNode>>,
head_b: &Option<Box<ListNode>>,
) -> Option<i32> {
let mut current_a = head_a;
let mut current_b = head_b;
while current_a.is_some() && current_b.is_some() {
if current_a == current_b {
return current_a.as_ref().map(|node| node.val);
}
current_a = ¤t_a.as_ref().unwrap().next;
current_b = ¤t_b.as_ref().unwrap().next;
}
None
}Attempts:
2 left
💡 Hint
Check what happens when one list is longer than the other.
✗ Incorrect
The code unwraps next without checking if it is None, causing panic if lists differ in length.
❓ Predict Output
advancedOutput of intersection detection with pointer alignment
What is the output of this Rust code that finds the intersection node by aligning list lengths?
DSA Rust
use std::rc::Rc;
struct ListNode {
val: i32,
next: Option<Rc<ListNode>>,
}
fn get_length(head: &Option<Rc<ListNode>>) -> usize {
let mut count = 0;
let mut current = head;
while let Some(node) = current {
count += 1;
current = &node.next;
}
count
}
fn get_intersection_node(
mut head_a: &Option<Rc<ListNode>>,
mut head_b: &Option<Rc<ListNode>>,
) -> Option<i32> {
let len_a = get_length(head_a);
let len_b = get_length(head_b);
let mut diff = if len_a > len_b { len_a - len_b } else { len_b - len_a };
if len_a > len_b {
while diff > 0 {
head_a = &head_a.as_ref()?.next;
diff -= 1;
}
} else {
while diff > 0 {
head_b = &head_b.as_ref()?.next;
diff -= 1;
}
}
while head_a.is_some() && head_b.is_some() {
if std::ptr::eq(&**head_a.as_ref()?, &**head_b.as_ref()?) {
return head_a.as_ref().map(|node| node.val);
}
head_a = &head_a.as_ref()?.next;
head_b = &head_b.as_ref()?.next;
}
None
}
fn main() {
let intersect = Rc::new(ListNode { val: 8, next: Some(Rc::new(ListNode { val: 10, next: None })) });
let list_a = Some(Rc::new(ListNode {
val: 3,
next: Some(Rc::new(ListNode {
val: 7,
next: Some(intersect.clone()),
})),
}));
let list_b = Some(Rc::new(ListNode {
val: 99,
next: Some(Rc::new(ListNode {
val: 1,
next: Some(intersect.clone()),
})),
}));
let result = get_intersection_node(&list_a, &list_b);
println!("{}", result.unwrap_or(-1));
}Attempts:
2 left
💡 Hint
The intersection node is the first shared node after aligning list lengths.
✗ Incorrect
The code aligns the longer list pointer to the same distance from the end, then compares nodes by pointer equality. The intersection node has value 8.
🚀 Application
expertCalculate number of nodes after intersection
Given two intersecting singly linked lists, how many nodes are there from the intersection node to the end?
DSA Rust
use std::rc::Rc;
struct ListNode {
val: i32,
next: Option<Rc<ListNode>>,
}
fn count_nodes_from_intersection(
head_a: &Option<Rc<ListNode>>,
head_b: &Option<Rc<ListNode>>,
) -> usize {
use std::collections::HashSet;
let mut nodes = HashSet::new();
let mut current_a = head_a;
while let Some(node) = current_a {
nodes.insert(&**node as *const ListNode);
current_a = &node.next;
}
let mut current_b = head_b;
while let Some(node) = current_b {
if nodes.contains(&**node as *const ListNode) {
// Count nodes from intersection to end
let mut count = 0;
let mut temp: Option<&ListNode> = Some(&**node);
while let Some(n) = temp {
count += 1;
temp = n.next.as_ref().map(|b| &**b);
}
return count;
}
current_b = &node.next;
}
0
}
fn main() {
let intersect = Rc::new(ListNode { val: 15, next: Some(Rc::new(ListNode { val: 30, next: None })) });
let list_a = Some(Rc::new(ListNode {
val: 10,
next: Some(Rc::new(ListNode {
val: 12,
next: Some(intersect.clone()),
})),
}));
let list_b = Some(Rc::new(ListNode {
val: 3,
next: Some(Rc::new(ListNode {
val: 6,
next: Some(Rc::new(ListNode {
val: 9,
next: Some(intersect.clone()),
})),
})),
}));
let result = count_nodes_from_intersection(&list_a, &list_b);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Count nodes starting from the intersection node to the end of the list.
✗ Incorrect
The intersection node is 15, followed by 30, so total 2 nodes from intersection to end.
