0
0
DSA Typescriptprogramming~20 mins

DFS Depth First Search on Graph in DSA Typescript - 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 following DFS traversal starting from node 0?
DSA Typescript
const graph = new Map<number, number[]>([
  [0, [1, 2]],
  [1, [3]],
  [2, []],
  [3, []]
]);

function dfs(node: number, visited: Set<number>, graph: Map<number, number[]>, result: number[]) {
  visited.add(node);
  result.push(node);
  for (const neighbor of graph.get(node) || []) {
    if (!visited.has(neighbor)) {
      dfs(neighbor, visited, graph, result);
    }
  }
}

const visited = new Set<number>();
const result: number[] = [];
dfs(0, visited, graph, result);
console.log(result.join(' -> ') + ' -> null');
A0 -> 2 -> 1 -> 3 -> null
B0 -> 1 -> 3 -> 2 -> null
C0 -> 1 -> 2 -> 3 -> null
D0 -> 3 -> 1 -> 2 -> null
Attempts:
2 left
💡 Hint
Remember DFS explores as far as possible along each branch before backtracking.
🧠 Conceptual
intermediate
1:30remaining
Number of nodes visited in DFS
Given the graph below, how many nodes will DFS visit starting from node 2?
DSA Typescript
const graph = new Map<number, number[]>([
  [0, [1]],
  [1, [2]],
  [2, [3]],
  [3, []],
  [4, [0]]
]);
A2
B4
C1
D3
Attempts:
2 left
💡 Hint
DFS visits all reachable nodes from the start node.
Predict Output
advanced
2:30remaining
DFS traversal order with cycle in graph
What is the output of the DFS traversal starting from node 0 in the graph with a cycle?
DSA Typescript
const graph = new Map<number, number[]>([
  [0, [1]],
  [1, [2]],
  [2, [0, 3]],
  [3, []]
]);

function dfs(node: number, visited: Set<number>, graph: Map<number, number[]>, result: number[]) {
  visited.add(node);
  result.push(node);
  for (const neighbor of graph.get(node) || []) {
    if (!visited.has(neighbor)) {
      dfs(neighbor, visited, graph, result);
    }
  }
}

const visited = new Set<number>();
const result: number[] = [];
dfs(0, visited, graph, result);
console.log(result.join(' -> ') + ' -> null');
A0 -> 2 -> 1 -> 3 -> null
B0 -> 1 -> 3 -> 2 -> null
C0 -> 1 -> 2 -> 3 -> null
D0 -> 3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
DFS avoids revisiting nodes already visited to prevent infinite loops.
🔧 Debug
advanced
2:30remaining
Identify the error in DFS implementation
What error will the following DFS code produce when run on the given graph?
DSA Typescript
const graph = new Map<number, number[]>([
  [0, [1]],
  [1, [2]],
  [2, []]
]);

function dfs(node: number, visited: Set<number>, graph: Map<number, number[]>, result: number[]) {
  if (visited.has(node)) return;
  result.push(node);
  for (const neighbor of graph.get(node)!) {
    dfs(neighbor, visited, graph, result);
  }
}

const visited = new Set<number>();
const result: number[] = [];
dfs(0, visited, graph, result);
console.log(result.join(' -> ') + ' -> null');
ARuntime error: Maximum call stack size exceeded
BThe code runs correctly and outputs '0 -> 1 -> 2 -> null'
CTypeError: Cannot read property 'Symbol.iterator' of undefined
DThe output is '0 -> 1 -> null'
Attempts:
2 left
💡 Hint
Check if the visited set is updated properly to avoid infinite recursion.
🚀 Application
expert
3:00remaining
DFS to find all connected components count
Given the graph below, what is the number of connected components?
DSA Typescript
const graph = new Map<number, number[]>([
  [0, [1]],
  [1, [0]],
  [2, [3]],
  [3, [2]],
  [4, []]
]);

function dfs(node: number, visited: Set<number>, graph: Map<number, number[]>) {
  visited.add(node);
  for (const neighbor of graph.get(node) || []) {
    if (!visited.has(neighbor)) {
      dfs(neighbor, visited, graph);
    }
  }
}

const visited = new Set<number>();
let count = 0;
for (const node of graph.keys()) {
  if (!visited.has(node)) {
    dfs(node, visited, graph);
    count++;
  }
}
console.log(count);
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Each connected component is a group of nodes reachable from each other.