Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the adjacency list for the tree.
DSA C
int adj[[1]][100]; // adjacency list for tree int size[[1]]; // size of adjacency for each node
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing too small an array size causing out-of-bounds errors.
Choosing unnecessarily large arrays wasting memory.
✗ Incorrect
The adjacency list size is set to 10000 to handle up to 10000 nodes, which is common for tree problems.
2fill in blank
mediumComplete the code to add an edge from node u to node v in the adjacency list.
DSA C
adj[u][size[u]++] = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding u instead of v causing incorrect adjacency.
Using size[v] which is unrelated here.
✗ Incorrect
We add node v to the adjacency list of node u, so the correct value is v.
3fill in blank
hardFix the error in the DFS function header to correctly pass the parent node.
DSA C
void dfs(int u, int [1]) { Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unclear or inconsistent parameter names.
Omitting the parent parameter causing infinite recursion.
✗ Incorrect
Using 'parent' as the parameter name is clear and consistent with common tree DFS implementations.
4fill in blank
hardFill both blanks to update the diameter and height during DFS.
DSA C
if (height[v] + height[u] + 1 > diameter) { diameter = height[v] + height[u] + [1]; } height[u] = 1 + [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 1 to diameter update.
Assigning height[u] without adding 1.
✗ Incorrect
Add 1 for the edge between u and v to update diameter; height[u] is 1 plus the max height of child v.
5fill in blank
hardFill all three blanks to complete the DFS loop over children and update heights.
DSA C
for (int i = 0; i < size[u]; i++) { int v = adj[u][[1]]; if (v != [2]) { dfs(v, u); if (height[v] > height[u]) { height[u] = height[v]; } } } height[u] = height[u] + [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index variable for adjacency.
Not skipping the parent node causing infinite recursion.
Forgetting to add 1 to height[u].
✗ Incorrect
Loop index i is used to access adjacency; skip parent node; height[u] is incremented by 1 for edge.