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 C code that calculates the diameter of a tree represented as an adjacency list?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 5 int adj[MAX][MAX] = {0}; int visited[MAX] = {0}; int max_diameter = 0; int dfs(int node) { visited[node] = 1; int max1 = 0, max2 = 0; for (int i = 0; i < MAX; i++) { if (adj[node][i] && !visited[i]) { int dist = dfs(i) + 1; if (dist > max1) { max2 = max1; max1 = dist; } else if (dist > max2) { max2 = dist; } } } if (max1 + max2 > max_diameter) { max_diameter = max1 + max2; } return max1; } int main() { adj[0][1] = adj[1][0] = 1; adj[1][2] = adj[2][1] = 1; adj[1][3] = adj[3][1] = 1; adj[3][4] = adj[4][3] = 1; dfs(0); printf("%d\n", max_diameter); return 0; }
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
✗ Incorrect
The longest path (diameter) in this tree is from node 2 to node 4 passing through nodes 1 and 3, which has length 3 edges.
🧠 Conceptual
intermediate1:30remaining
Understanding the diameter calculation logic
In the DP approach to find the diameter of a tree, what does the sum of the two largest depths from a node represent?
Attempts:
2 left
💡 Hint
Think about how the diameter can pass through a node connecting two longest branches.
✗ Incorrect
The diameter is the longest path between any two nodes. At each node, the sum of the two largest depths of its children represents the longest path passing through that node.
❓ Predict Output
advanced2:30remaining
Output of diameter calculation with weighted edges
What is the output of the following C code that calculates the diameter of a weighted tree using DP on trees?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 4 int adj[MAX][MAX] = {0}; int visited[MAX] = {0}; int max_diameter = 0; int dfs(int node) { visited[node] = 1; int max1 = 0, max2 = 0; for (int i = 0; i < MAX; i++) { if (adj[node][i] > 0 && !visited[i]) { int dist = dfs(i) + adj[node][i]; if (dist > max1) { max2 = max1; max1 = dist; } else if (dist > max2) { max2 = dist; } } } if (max1 + max2 > max_diameter) { max_diameter = max1 + max2; } return max1; } int main() { adj[0][1] = adj[1][0] = 3; adj[1][2] = adj[2][1] = 4; adj[1][3] = adj[3][1] = 2; dfs(0); printf("%d\n", max_diameter); return 0; }
Attempts:
2 left
💡 Hint
Sum the two longest weighted paths from the root node.
✗ Incorrect
The longest path is from node 2 to node 0 passing through node 1 with weights 4 + 3 = 7.
🔧 Debug
advanced2:00remaining
Identify the bug in diameter calculation code
What error will the following code produce when calculating the diameter of a tree?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 3 int adj[MAX][MAX] = {0}; int visited[MAX] = {0}; int max_diameter = 0; int dfs(int node) { visited[node] = 1; int max1 = 0, max2 = 0; for (int i = 0; i < MAX; i++) { if (adj[node][i] && !visited[i]) { int dist = dfs(i) + 1; if (dist > max1) { max2 = max1; max1 = dist; } else if (dist > max2) { max2 = dist; } } } if (max1 + max2 > max_diameter) { max_diameter = max1 + max2; } return max1; } int main() { adj[0][1] = 1; adj[1][0] = 1; adj[1][2] = 1; // Missing adj[2][1] = 1; dfs(0); printf("%d\n", max_diameter); return 0; }
Attempts:
2 left
💡 Hint
Check if the tree edges are correctly bidirectional.
✗ Incorrect
The missing adj[2][1] = 1 means the edge from node 2 to 1 is missing, so DFS won't visit node 2 properly, underestimating the diameter.
🧠 Conceptual
expert1:30remaining
Why DP on trees is efficient for diameter calculation
Why is the DP approach on trees preferred over brute force for finding the diameter of a tree?
Attempts:
2 left
💡 Hint
Think about how DP avoids repeated work in recursive tree traversal.
✗ Incorrect
DP on trees stores the longest paths from children to avoid recalculating them, making the diameter calculation efficient in linear time.