Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the adjacency list for the tree.
DSA Javascript
const n = 5; const edges = [[1, 2], [1, 3], [3, 4], [3, 5]]; const graph = Array.from({ length: n + 1 }, () => [1]); console.log(graph);
Attempts:
3 left
💡 Hint
Common Mistakes
Using an object {} instead of an array for adjacency list.
Initializing with null or 0 which are not iterable.
✗ Incorrect
We use an array of empty arrays to represent adjacency lists for each node.
2fill in blank
mediumComplete the code to add edges to the adjacency list.
DSA Javascript
for (const [u, v] of edges) { graph[u].push([1]); graph[v].push(u); } console.log(graph);
Attempts:
3 left
💡 Hint
Common Mistakes
Adding u to u's own adjacency list.
Using the whole graph or edges array instead of node v.
✗ Incorrect
We add node v to u's adjacency list and vice versa to represent an undirected edge.
3fill in blank
hardFix the error in the DFS function to correctly compute the diameter.
DSA Javascript
function dfs(node, parent) {
let max1 = 0, max2 = 0;
for (const child of graph[node]) {
if (child === [1]) continue;
const dist = dfs(child, node) + 1;
if (dist > max1) {
max2 = max1;
max1 = dist;
} else if (dist > max2) {
max2 = dist;
}
}
diameter = Math.max(diameter, max1 + max2);
return max1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Skipping the current node instead of the parent.
Not skipping any node causing infinite loops.
✗ Incorrect
We skip the parent node to avoid revisiting it and causing infinite recursion.
4fill in blank
hardFill both blanks to start DFS from node 1 and print the diameter.
DSA Javascript
let diameter = 0; function dfs(node, parent) { let max1 = 0, max2 = 0; for (const child of graph[node]) { if (child === parent) continue; const dist = dfs(child, node) + 1; if (dist > max1) { max2 = max1; max1 = dist; } else if (dist > max2) { max2 = dist; } } diameter = Math.max(diameter, max1 + max2); return max1; } dfs([1], [2]); console.log(diameter);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as parent which is a valid node and causes wrong skipping.
Using null which is not a number and may cause errors.
✗ Incorrect
We start DFS from node 1 and use -1 as parent since it has no parent.
5fill in blank
hardFill all three blanks to implement the diameter calculation using DFS and print the result.
DSA Javascript
const n = 4; const edges = [[1, 2], [2, 3], [2, 4]]; const graph = Array.from({ length: n + 1 }, () => [1]); for (const [u, v] of edges) { graph[u].push(v); graph[v].push(u); } let diameter = 0; function dfs(node, parent) { let max1 = 0, max2 = 0; for (const child of graph[node]) { if (child === parent) continue; const dist = dfs(child, node) + 1; if (dist > max1) { max2 = max1; max1 = dist; } else if (dist > max2) { max2 = dist; } } diameter = Math.max(diameter, max1 + max2); return max1; } dfs([2], [3]); console.log(diameter);
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing graph with null or 0 instead of empty arrays.
Starting DFS from node 0 which does not exist.
Using 0 as parent which is a valid node.
✗ Incorrect
Initialize graph with empty arrays, start DFS from node 1 with parent -1 to compute diameter.
