0
0
DSA Cprogramming~20 mins

DFS Depth First Search on Graph in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of DFS traversal on a simple graph
What is the output of the DFS traversal starting from node 0 on the given graph?
DSA C
/* Graph adjacency list representation:
0: 1, 2
1: 2
2: 0, 3
3: 3

DFS starting from node 0 */
#include <stdio.h>
#include <stdbool.h>

#define N 4

int graph[N][N] = {
    {0, 1, 1, 0},
    {0, 0, 1, 0},
    {1, 0, 0, 1},
    {0, 0, 0, 1}
};
bool visited[N] = {false};

void dfs(int node) {
    visited[node] = true;
    printf("%d ", node);
    for (int i = 0; i < N; i++) {
        if (graph[node][i] && !visited[i]) {
            dfs(i);
        }
    }
}

int main() {
    dfs(0);
    return 0;
}
A0 1 3 2
B0 1 2 3
C0 3 2 1
D0 2 3 1
Attempts:
2 left
💡 Hint
Trace the DFS calls starting from node 0 and follow adjacency order.
🧠 Conceptual
intermediate
1:30remaining
Number of nodes visited in DFS on disconnected graph
Given a graph with 5 nodes and edges only between nodes 0-1 and 2-3, how many nodes will DFS starting from node 0 visit?
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
DFS only visits nodes reachable from the start node.
🔧 Debug
advanced
2:00remaining
Identify the error in DFS implementation
What error will this DFS code produce when run on a graph with 3 nodes?
DSA C
#include <stdio.h>
#include <stdbool.h>

#define N 3

int graph[N][N] = {
    {0,1,0},
    {0,0,1},
    {1,0,0}
};
bool visited[N] = {false};

void dfs(int node) {
    visited[node] = true;
    printf("%d ", node);
    for (int i = 0; i < N; i++) {
        if (graph[node][i] && !visited[i]) {
            dfs(i);
        }
    }
}

int main() {
    dfs(0);
    return 0;
}
ANo error, prints: 0 1 2
BInfinite recursion
CSegmentation fault (out of bounds array access)
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check the loop boundary in the DFS function.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in DFS code snippet
Which option contains the syntax error in the DFS function?
DSA C
void dfs(int node) {
    visited[node] = true;
    printf("%d ", node);
    for (int i = 0; i < N; i++) {
        if (graph[node][i] && !visited[i]) {
            dfs(i);
        }
    }
}
AMissing parentheses in printf
BMissing braces around for loop body
CIncorrect function return type
DMissing semicolon after visited[node] = true
Attempts:
2 left
💡 Hint
Look carefully at each line for missing punctuation.
🚀 Application
expert
3:00remaining
DFS to detect cycle in directed graph
Which option correctly implements cycle detection using DFS in a directed graph?
DSA C
#include <stdio.h>
#include <stdbool.h>

#define N 4

int graph[N][N] = {
    {0,1,0,0},
    {0,0,1,0},
    {0,0,0,1},
    {1,0,0,0}
};
bool visited[N] = {false};
bool recStack[N] = {false};

bool dfs(int node) {
    visited[node] = true;
    recStack[node] = true;
    for (int i = 0; i < N; i++) {
        if (graph[node][i]) {
            if (!visited[i] && dfs(i))
                return true;
            else if (recStack[i])
                return true;
        }
    }
    recStack[node] = false;
    return false;
}

int main() {
    bool hasCycle = false;
    for (int i = 0; i < N; i++) {
        if (!visited[i]) {
            if (dfs(i)) {
                hasCycle = true;
                break;
            }
        }
    }
    printf("%s\n", hasCycle ? "Cycle detected" : "No cycle");
    return 0;
}
APrints: Cycle detected
BPrints: No cycle
CCompilation error due to missing return in dfs
DRuntime error due to stack overflow
Attempts:
2 left
💡 Hint
Check how recursion stack is used to detect back edges.