Challenge - 5 Problems
Tree Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Diameter Calculation on a Simple Tree
What is the output of the following Rust code that calculates the diameter of a tree represented as adjacency lists?
DSA Rust
fn dfs(node: usize, parent: usize, graph: &Vec<Vec<usize>>, max_diameter: &mut usize) -> usize {
let mut max1 = 0;
let mut max2 = 0;
for &child in &graph[node] {
if child == parent {
continue;
}
let dist = dfs(child, node, graph, max_diameter) + 1;
if dist > max1 {
max2 = max1;
max1 = dist;
} else if dist > max2 {
max2 = dist;
}
}
*max_diameter = (*max_diameter).max(max1 + max2);
max1
}
fn main() {
let graph = vec![
vec![1, 2], // 0
vec![0, 3, 4], // 1
vec![0], // 2
vec![1], // 3
vec![1], // 4
];
let mut max_diameter = 0;
dfs(0, usize::MAX, &graph, &mut max_diameter);
println!("{}", max_diameter);
}Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
✗ Incorrect
The diameter is the longest path between any two nodes. Here, the longest path is from node 3 to node 2 or node 4 to node 2, which has length 3 edges.
🧠 Conceptual
intermediateUnderstanding Diameter Calculation Logic
In the DP approach to find the diameter of a tree, why do we keep track of the two longest distances from a node to its children?
Attempts:
2 left
💡 Hint
Think about the longest path that goes through a node connecting two subtrees.
✗ Incorrect
The diameter can be the longest path passing through a node, which is the sum of the two longest child paths from that node.
🔧 Debug
advancedIdentify the Error in Diameter Calculation Code
What error will the following Rust code produce when calculating the diameter of a tree?
DSA Rust
fn dfs(node: usize, parent: usize, graph: &Vec<Vec<usize>>, max_diameter: &mut usize) -> usize {
let mut max1 = 0;
let mut max2 = 0;
for &child in &graph[node] {
if child == parent {
continue;
}
let dist = dfs(child, node, graph, max_diameter);
if dist > max1 {
max2 = max1;
max1 = dist;
} else if dist > max2 {
max2 = dist;
}
}
*max_diameter = (*max_diameter).max(max1 + max2);
max1
}
fn main() {
let graph = vec![
vec![1],
vec![0, 2],
vec![1]
];
let mut max_diameter = 0;
dfs(0, usize::MAX, &graph, &mut max_diameter);
println!("{}", max_diameter);
}Attempts:
2 left
💡 Hint
Check how distances are calculated and returned in recursion.
✗ Incorrect
The code forgets to add 1 to the distance returned by recursive calls, so all distances remain 0 and it outputs 0 instead of 2.
❓ Predict Output
advancedOutput of Diameter Calculation on a Star-Shaped Tree
What is the output of the following Rust code that calculates the diameter of a star-shaped tree with 5 nodes?
DSA Rust
fn dfs(node: usize, parent: usize, graph: &Vec<Vec<usize>>, max_diameter: &mut usize) -> usize {
let mut max1 = 0;
let mut max2 = 0;
for &child in &graph[node] {
if child == parent {
continue;
}
let dist = dfs(child, node, graph, max_diameter) + 1;
if dist > max1 {
max2 = max1;
max1 = dist;
} else if dist > max2 {
max2 = dist;
}
}
*max_diameter = (*max_diameter).max(max1 + max2);
max1
}
fn main() {
let graph = vec![
vec![1, 2, 3, 4], // 0
vec![0], // 1
vec![0], // 2
vec![0], // 3
vec![0], // 4
];
let mut max_diameter = 0;
dfs(0, usize::MAX, &graph, &mut max_diameter);
println!("{}", max_diameter);
}Attempts:
2 left
💡 Hint
The diameter in a star tree is the longest path between two leaves through the center.
✗ Incorrect
The longest path is between any two leaves through the center node 0, which is 2 edges long.
🚀 Application
expertMaximum Diameter After Adding One Edge
Given a tree with N nodes, you can add exactly one edge between any two nodes to create a cycle. Which option correctly describes how to find the maximum possible diameter after adding this edge?
Attempts:
2 left
💡 Hint
Think about how adding an edge creates a cycle and how that affects longest paths.
✗ Incorrect
Adding an edge between any two nodes creates a cycle, but the longest path (diameter) remains the same as the original tree since the original paths are still available and new paths using the edge are shorter.
