0
0
DSA Cprogramming~20 mins

Topological Sort Using DFS in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Topological Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Topological Sort on a Simple DAG
What is the output of the following topological sort using DFS on the given graph?

The graph edges are:
0 -> 1
0 -> 2
1 -> 3
2 -> 3
DSA C
void dfs(int node, int visited[], int stack[], int *top, int graph[][4]) {
    visited[node] = 1;
    for (int i = 0; i < 4; i++) {
        if (graph[node][i] && !visited[i]) {
            dfs(i, visited, stack, top, graph);
        }
    }
    stack[(*top)++] = node;
}

int main() {
    int graph[4][4] = {
        {0,1,1,0},
        {0,0,0,1},
        {0,0,0,1},
        {0,0,0,0}
    };
    int visited[4] = {0};
    int stack[4];
    int top = 0;
    for (int i = 0; i < 4; i++) {
        if (!visited[i]) dfs(i, visited, stack, &top, graph);
    }
    for (int i = top - 1; i >= 0; i--) {
        printf("%d ", stack[i]);
    }
    return 0;
}
A3 1 2 0
B0 2 1 3
C0 1 2 3
D1 0 2 3
Attempts:
2 left
💡 Hint
Remember that DFS pushes nodes to stack after visiting all descendants.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of the Stack in DFS Topological Sort
In the DFS-based topological sort, why do we push the node onto the stack only after visiting all its neighbors?
ABecause it reduces the time complexity of DFS.
BBecause it helps to detect cycles in the graph.
CBecause it ensures nodes are output in the order they are first visited.
DBecause it guarantees that all dependencies of a node are processed before the node itself.
Attempts:
2 left
💡 Hint
Think about the meaning of dependencies in a directed graph.
🔧 Debug
advanced
2:00remaining
Identify the Error in This DFS Topological Sort Implementation
What error will this code produce when running DFS topological sort on a graph with 3 nodes and edges 0->1, 1->2?

Code snippet:
void dfs(int node, int visited[], int stack[], int *top, int graph[][3]) { visited[node] = 1; for (int i = 0; i < 3; i++) { if (graph[node][i] && visited[i] == 0) { dfs(i, visited, stack, top, graph); } } stack[*top] = node; (*top)++; } int main() { int graph[3][3] = { {0,1,0}, {0,0,1}, {0,0,0} }; int visited[3] = {0}; int stack[3]; int top = 0; for (int i = 0; i < 3; i++) { if (!visited[i]) dfs(i, visited, stack, &top, graph); } for (int i = 0; i < top; i++) { printf("%d ", stack[i]); } return 0; }
AThe output order is reversed; it prints nodes in finishing order, not topological order.
BThe code causes a segmentation fault due to stack overflow.
CThe code prints nodes in correct topological order.
DThe code causes a syntax error due to missing semicolons.
Attempts:
2 left
💡 Hint
Check how the stack is printed compared to how nodes are pushed.
🚀 Application
advanced
1:30remaining
Find a Valid Topological Order for a Complex DAG
Given the directed edges:
5 -> 2
5 -> 0
4 -> 0
4 -> 1
2 -> 3
3 -> 1

Which of the following is a valid topological order?
A4 5 0 2 3 1
B5 2 4 3 1 0
C5 4 2 3 1 0
D2 3 5 4 1 0
Attempts:
2 left
💡 Hint
Remember that a node must come before all nodes it points to.
🧠 Conceptual
expert
2:00remaining
Detecting Cycles in DFS-based Topological Sort
In a DFS-based topological sort, how can you detect a cycle in the graph during traversal?
AIf a node is found in the recursion stack (currently being visited), a cycle exists.
BIf the stack size is less than the number of nodes, a cycle exists.
CIf a node is visited twice during DFS, a cycle exists.
DIf the graph has more edges than nodes, a cycle exists.
Attempts:
2 left
💡 Hint
Think about nodes currently in the path of DFS recursion.