Bird
Raised Fist0
DSA Cprogramming~10 mins

DFS Depth First Search on Graph in DSA C - Interactive Practice

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

Complete the code to declare the DFS function parameter for the graph adjacency list.

DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) {
Drag options to blanks, or click blank then click option'
Alist
Bgraph
Cadj
Dedges
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or inconsistent with the rest of the code.
2fill in blank
medium

Complete the code to mark the current node as visited inside DFS.

DSA C
visited[node] = [1];
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as 0 which means unvisited.
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].size(); i++) {
    int adjNode = adj[node][i];
    if(!visited[[1]]) {
        dfs(adjNode, visited, adj, n);
    }
}
Drag options to blanks, or click blank then click option'
AadjNode
Bnode
Ci
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Checking visited[node] instead of visited[adjNode].
Using the loop index 'i' as the node index.
4fill in blank
hard

Fill both blanks to correctly initialize and call DFS for all nodes in the graph.

DSA C
int visited[n];
for(int i = 0; i < n; i++) {
    visited[i] = [1];
}
for(int i = 0; i < n; i++) {
    if(visited[i] == [2]) {
        dfs(i, visited, adj, n);
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing visited array with 1 instead of 0.
Checking for visited[i] == 1 instead of 0.
5fill in blank
hard

Fill all three blanks to complete the DFS function that prints nodes in DFS order.

DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) {
    visited[node] = [2];
    printf("%d ", [3]);
    for(int i = 0; i < adj[node].size(); i++) {
        int adjNode = adj[node][i];
        if(!visited[adjNode]) {
            dfs(adjNode, visited, adj, n);
        }
    }
}
Drag options to blanks, or click blank then click option'
Aadj
B1
Cnode
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Printing visited[node] instead of node.
Using wrong variable name for adjacency list.

Practice

(1/5)
1. What is the main idea behind Depth First Search (DFS) on a graph?
easy
A. Visit all nodes at the current depth before moving deeper
B. Visit nodes randomly without any order
C. Explore as far as possible along each branch before backtracking
D. Only visit nodes with no outgoing edges

Solution

  1. Step 1: Understand DFS traversal

    DFS explores a graph by going deep into one branch before backtracking to explore others.
  2. Step 2: Compare with other traversal methods

    BFS visits nodes level by level, but DFS goes deep first.
  3. Final Answer:

    Explore as far as possible along each branch before backtracking -> Option C
  4. Quick Check:

    DFS = deep exploration first [OK]
Hint: DFS means go deep first, then backtrack [OK]
Common Mistakes:
  • Confusing DFS with BFS
  • Thinking DFS visits nodes level-wise
  • Assuming DFS visits nodes randomly
2. Which of the following is the correct way to start a DFS function in C for a graph represented by adjacency list adj and visited array visited?
easy
A. void dfs(int node) { visited[node] = 1; for(int i=0; i
B. void dfs(int node) { visited[node] = true; for(int i=0; i
C. void dfs(int node) { visited[node] = 0; for(int i=0; i
D. void dfs(int node) { visited[node] = 1; for(int i=0; i

Solution

  1. Step 1: Mark current node as visited

    Set visited[node] = 1 (or true) to avoid revisiting.
  2. Step 2: Traverse neighbors only if not visited

    Check if neighbor is unvisited before recursive DFS call to prevent infinite loops.
  3. Final Answer:

    void dfs(int node) { visited[node] = 1; for(int i=0; i -> Option D
  4. Quick Check:

    Mark visited and check before recursion [OK]
Hint: Always check visited before recursive DFS call [OK]
Common Mistakes:
  • Not checking if neighbor is visited
  • Using wrong visited value (0 instead of 1)
  • Using .size() instead of .length in C arrays
3. Consider the following graph adjacency list:
0: [1, 2]
1: [2]
2: [0, 3]
3: [3]

Starting DFS from node 2, what is the order of nodes visited?
medium
A. [2, 3, 0, 1]
B. [2, 0, 1, 3]
C. [2, 0, 3, 1]
D. [2, 1, 0, 3]

Solution

  1. Step 1: Start DFS at node 2

    Visit 2, mark visited. Neighbors: 0 and 3.
  2. Step 2: Visit neighbor 0 first

    From 2, go to 0. Visit 0, neighbors: 1 and 2 (2 already visited).
  3. Step 3: Visit neighbor 1 from 0

    Visit 1, neighbor: 2 (visited). Backtrack.
  4. Step 4: Back to 2, visit neighbor 3

    Visit 3, neighbor: 3 (already visited). End.
  5. Final Answer:

    [2, 0, 1, 3] -> Option B
  6. Quick Check:

    DFS order from 2 = [2,0,1,3] [OK]
Hint: Follow neighbors depth-first, mark visited to avoid repeats [OK]
Common Mistakes:
  • Visiting nodes multiple times
  • Visiting neighbors in wrong order
  • Ignoring visited checks causing loops
4. The following DFS code has a bug. What is the error?
void dfs(int node) {
  visited[node] = 1;
  for(int i=0; i<adj[node].length; i++) {
    dfs(adj[node][i]);
  }
}
medium
A. Missing check if neighbor is already visited before recursive call
B. Visited array is not initialized
C. Incorrect loop condition syntax
D. Visited[node] should be set after recursion

Solution

  1. Step 1: Analyze DFS recursion calls

    The code calls dfs on all neighbors without checking if they are visited.
  2. Step 2: Identify risk of infinite recursion

    Without visited check, DFS may revisit nodes endlessly causing stack overflow.
  3. Final Answer:

    Missing check if neighbor is already visited before recursive call -> Option A
  4. Quick Check:

    Always check visited before recursion [OK]
Hint: Check visited before recursive DFS call to avoid loops [OK]
Common Mistakes:
  • Assuming visited check is optional
  • Setting visited after recursion
  • Ignoring infinite recursion risk
5. Given a directed graph with possible cycles, how can DFS be modified to detect if a cycle exists?
hard
A. Use a recursion stack array to track nodes in the current path and check if a node is revisited in this stack
B. Only mark nodes visited after all neighbors are processed
C. Use BFS instead of DFS to detect cycles
D. Ignore visited array and rely on node values

Solution

  1. Step 1: Understand cycle detection in DFS

    Detect cycles by tracking nodes currently in recursion (call) stack.
  2. Step 2: Use recursion stack array

    Maintain an array marking nodes in current DFS path; if a neighbor is in this stack, a cycle exists.
  3. Step 3: Differentiate from visited array

    Visited marks all visited nodes; recursion stack tracks current path only.
  4. Final Answer:

    Use a recursion stack array to track nodes in the current path and check if a node is revisited in this stack -> Option A
  5. Quick Check:

    Recursion stack detects cycles in DFS [OK]
Hint: Track recursion stack to find cycles during DFS [OK]
Common Mistakes:
  • Confusing visited with recursion stack
  • Using BFS for cycle detection in directed graphs
  • Not tracking current path nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard