Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the visited array to zero.
DSA C
int visited[V] = {0};
for (int i = 0; i < V; i++) {
visited[i] = [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing visited array with 1 instead of 0.
Using uninitialized values causing unpredictable behavior.
✗ Incorrect
The visited array should be initialized to 0 to mark all nodes as unvisited at the start.
2fill in blank
mediumComplete the code to mark the current node as visited in DFS.
DSA C
void dfs(int node) {
visited[node] = [1];
// rest of DFS code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as 0 or 2 incorrectly.
Not marking the node at all.
✗ Incorrect
Marking the node as 1 means it is currently being visited in the DFS path.
3fill in blank
hardFix the error in the condition to detect a cycle in DFS.
DSA C
for (int i = 0; i < adj[node].size(); i++) { int next = adj[node][i]; if (visited[next] == [1]) { return 1; // cycle detected } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for visited[next] == 0 instead of 1.
Using wrong values like 2 or -1.
✗ Incorrect
If visited[next] == 1, it means the node is in the current recursion stack, indicating a cycle.
4fill in blank
hardFill both blanks to correctly mark nodes after DFS call.
DSA C
visited[node] = [1]; for (int i = 0; i < adj[node].size(); i++) { int next = adj[node][i]; if (visited[next] == 0 && dfs(next)) { return 1; } } visited[node] = [2]; return 0;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not marking node as 2 after DFS.
Marking node as 0 instead of 1 initially.
✗ Incorrect
Mark node as 1 when visiting, and 2 after all descendants are processed to indicate done.
5fill in blank
hardFill all three blanks to complete the cycle detection function.
DSA C
int isCyclic() {
for (int i = 0; i < V; i++) {
if (visited[i] == [1]) {
if (dfs(i) == [2]) {
return [3];
}
}
}
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting DFS from visited nodes.
Returning wrong values on cycle detection.
✗ Incorrect
Start DFS from unvisited nodes (visited[i] == 0). If dfs returns 1 (cycle found), return 1.