0
0
DSA Cprogramming~10 mins

Cycle Detection in Directed Graph in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A1
B0
C-1
DV
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing visited array with 1 instead of 0.
Using uninitialized values causing unpredictable behavior.
2fill in blank
medium

Complete 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'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as 0 or 2 incorrectly.
Not marking the node at all.
3fill in blank
hard

Fix 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'
A1
B2
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for visited[next] == 0 instead of 1.
Using wrong values like 2 or -1.
4fill in blank
hard

Fill 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'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Not marking node as 2 after DFS.
Marking node as 0 instead of 1 initially.
5fill in blank
hard

Fill 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'
A0
B1
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting DFS from visited nodes.
Returning wrong values on cycle detection.