Challenge - 5 Problems
Tree Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of diameter calculation on a simple tree
What is the output of the following TypeScript code that calculates the diameter of a tree using DP on trees?
DSA Typescript
type Tree = { [key: number]: number[] }; function treeDiameter(tree: Tree): number { let diameter = 0; function dfs(node: number, parent: number): number { let max1 = 0, max2 = 0; for (const child of tree[node] || []) { if (child === parent) continue; const depth = dfs(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } dfs(0, -1); return diameter; } const tree = { 0: [1, 2], 1: [0, 3, 4], 2: [0], 3: [1], 4: [1] }; console.log(treeDiameter(tree));
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
✗ Incorrect
The diameter is the longest path between 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
intermediate1:30remaining
Understanding the role of DP in tree diameter calculation
Which statement best describes how dynamic programming (DP) is used in calculating the diameter of a tree?
Attempts:
2 left
💡 Hint
Think about how repeated calculations are avoided in DFS.
✗ Incorrect
DP stores the longest depths from each node to its children so that these values are reused, avoiding repeated DFS calls.
🔧 Debug
advanced2:00remaining
Identify the error in this diameter calculation code
What error will this TypeScript code produce when calculating the diameter of a tree?
DSA Typescript
type Tree = { [key: number]: number[] }; function treeDiameter(tree: Tree): number { let diameter = 0; function dfs(node: number, parent: number): number { let max1 = 0, max2 = 0; for (const child of tree[node] || []) { if (child === parent) continue; const depth = dfs(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } dfs(0, -1); return diameter; } const tree = { 0: [1, 2], 1: [0, 3, 4] }; console.log(treeDiameter(tree));
Attempts:
2 left
💡 Hint
Check if all nodes have entries in the tree object.
✗ Incorrect
If a node has no children, tree[node] is undefined, so for-of loop throws TypeError.
🚀 Application
advanced1:30remaining
Calculate diameter for a star-shaped tree
Given a star-shaped tree with 1 center node connected to 5 leaf nodes, what is the diameter of this tree?
Attempts:
2 left
💡 Hint
The diameter is the longest path between two leaves through the center.
✗ Incorrect
The longest path goes from one leaf to another through the center node, which is 2 edges.
❓ Predict Output
expert2:30remaining
Output of diameter calculation on a complex tree
What is the output of this TypeScript code that calculates the diameter of a more complex tree?
DSA Typescript
type Tree = { [key: number]: number[] }; function treeDiameter(tree: Tree): number { let diameter = 0; function dfs(node: number, parent: number): number { let max1 = 0, max2 = 0; for (const child of tree[node] || []) { if (child === parent) continue; const depth = dfs(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } dfs(0, -1); return diameter; } const tree = { 0: [1, 2], 1: [0, 3, 4], 2: [0, 5], 3: [1, 6], 4: [1], 5: [2, 7, 8], 6: [3], 7: [5], 8: [5] }; console.log(treeDiameter(tree));
Attempts:
2 left
💡 Hint
Find the longest path between any two nodes in the tree.
✗ Incorrect
The longest path is from node 6 to node 7 or 8, passing through nodes 3,1,0,2,5, length 6 edges.