Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the discovery time array with -1 for all nodes.
DSA Javascript
const disc = new Array(n).fill([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of -1 causes confusion with valid discovery times.
Using null or Infinity is not standard for discovery times.
✗ Incorrect
We use -1 to mark nodes as unvisited before DFS starts.
2fill in blank
mediumComplete the code to update the low time of the current node after visiting a neighbor.
DSA Javascript
low[u] = Math.min(low[u], [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using disc[v] instead of low[v] misses updates from deeper recursion.
Using disc[u] or low[u] does not update based on neighbor's info.
✗ Incorrect
We update low[u] with the lowest reachable discovery time from neighbor v.
3fill in blank
hardFix the error in the condition that detects a bridge edge between u and v.
DSA Javascript
if ([1] > disc[u]) { bridges.push([u, v]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using disc[v] instead of low[v] misses the correct bridge condition.
Using low[u] or disc[u] incorrectly compares the wrong values.
✗ Incorrect
A bridge is found if low[v] > disc[u], meaning v cannot reach u or its ancestors.
4fill in blank
hardFill both blanks to correctly perform DFS and update low time for back edges.
DSA Javascript
for (const v of graph[u]) { if (v === [1]) continue; if (disc[v] === -1) { dfs(v, u); low[u] = Math.min(low[u], [2]); } else { low[u] = Math.min(low[u], disc[v]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not skipping parent causes infinite recursion.
Updating low[u] with disc[u] instead of low[v] misses deeper reachability.
✗ Incorrect
Skip the parent node to avoid revisiting it, and update low[u] with low[v] after DFS.
5fill in blank
hardFill all three blanks to initialize and start DFS for all nodes in the graph.
DSA Javascript
const disc = new Array(n).fill(-1); const low = new Array(n).fill(-1); const bridges = []; let time = 0; function dfs(u, [1]) { disc[u] = time; low[u] = time; time++; for (const v of graph[u]) { if (disc[v] === -1) { dfs(v, u); low[u] = Math.min(low[u], low[v]); if (low[v] > disc[u]) { bridges.push([u, v]); } } else if (v !== [2]) { low[u] = Math.min(low[u], disc[v]); } } } for (let i = 0; i < n; i++) { if (disc[i] === -1) { dfs(i, [3]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined instead of -1 for initial parent.
Using inconsistent variable names for parent.
✗ Incorrect
The DFS function uses 'parent' to track the previous node; initial call uses -1 as no parent.
