0
0
DSA Cprogramming~10 mins

Topological Sort Using DFS 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 declare the adjacency list for the graph.

DSA C
int adj[[1]][100]; // adjacency list for graph
int visited[100];
Drag options to blanks, or click blank then click option'
A10
B50
C100
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using a size larger than the number of nodes wastes memory.
Using a smaller size causes out-of-bounds errors.
2fill in blank
medium

Complete the DFS function header to accept the current node.

DSA C
void dfs(int [1]) {
    visited[node] = 1;
    // DFS logic
}
Drag options to blanks, or click blank then click option'
Astart
Bnode
Cindex
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that doesn't match the variable used inside the function.
3fill in blank
hard

Fix the error in the DFS recursive call to visit adjacent nodes.

DSA C
for (int i = 0; i < adj[node][0]; i++) {
    int adjNode = adj[node][i + 1];
    if (!visited[adjNode]) {
        dfs([1]);
    }
}
Drag options to blanks, or click blank then click option'
AadjNode
Bi
Cnode
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Calling dfs on the loop index 'i' instead of the adjacent node.
Calling dfs on the current node again causing infinite recursion.
4fill in blank
hard

Fill both blanks to push the node onto the stack after visiting all neighbors.

DSA C
void dfs(int node) {
    visited[node] = 1;
    for (int i = 0; i < adj[node][0]; i++) {
        int adjNode = adj[node][i + 1];
        if (!visited[adjNode]) {
            dfs(adjNode);
        }
    }
    stack[[1]] = node;
    [2]++;
}
Drag options to blanks, or click blank then click option'
Atop
Bcount
Cindex
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for index and increment causing errors.
Forgetting to increment the stack pointer.
5fill in blank
hard

Fill all three blanks to print the topological order from the stack.

DSA C
for (int i = [1] - 1; i >= 0; i--) {
    printf("%d ", stack[[2]]);
}
printf("\n");

// Reset stack pointer
[3] = 0;
Drag options to blanks, or click blank then click option'
Atop
Bi
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing compilation errors.
Not resetting the stack pointer after printing.