0
0
DSA Typescriptprogramming~10 mins

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

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

Complete the code to start DFS from the given node.

DSA Typescript
function dfs(graph: Record<string, string[]>, node: string, visited: Set<string>) {
  if (visited.has(node)) return;
  visited.add(node);
  console.log(node);
  for (const neighbor of graph[node]) {
    dfs(graph, [1], visited);
  }
}
Drag options to blanks, or click blank then click option'
Anode
Bneighbor
Cvisited
Dgraph
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the current node again instead of the neighbor.
Passing the visited set incorrectly.
Passing the whole graph instead of a node.
2fill in blank
medium

Complete the code to check if a node has been visited before processing it.

DSA Typescript
function dfs(graph: Record<string, string[]>, node: string, visited: Set<string>) {
  if ([1]) return;
  visited.add(node);
  console.log(node);
  for (const neighbor of graph[node]) {
    dfs(graph, neighbor, visited);
  }
}
Drag options to blanks, or click blank then click option'
Avisited.contains(node)
Bnode in visited
Cvisited.has(node)
Dvisited.includes(node)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' operator which doesn't work for Set.
Using contains or includes which are not Set methods.
3fill in blank
hard

Fix the error in the DFS function to avoid infinite recursion.

DSA Typescript
function dfs(graph: Record<string, string[]>, node: string, visited: Set<string>) {
  if (visited.has(node)) return;
  visited.add(node);
  console.log(node);
  for (const neighbor of graph[node]) {
    dfs(graph, neighbor, [1]);
  }
}
Drag options to blanks, or click blank then click option'
Avisited
Bnew Set()
Cnew Set([node])
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a new Set() causes infinite recursion.
Passing undefined causes runtime errors.
4fill in blank
hard

Fill both blanks to create a DFS function that returns all visited nodes as an array.

DSA Typescript
function dfs(graph: Record<string, string[]>, node: string, visited: Set<string>, result: string[]) {
  if ([1]) return;
  visited.add(node);
  result.push(node);
  for (const neighbor of graph[node]) {
    dfs(graph, neighbor, visited, [2]);
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Avisited.has(node)
Bresult
Cvisited
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Passing visited instead of result to recursive calls.
Not checking if node is visited before processing.
5fill in blank
hard

Fill all three blanks to implement DFS that returns nodes in order visited, avoiding cycles.

DSA Typescript
function dfs(graph: Record<string, string[]>, node: string, visited: Set<string> = new Set(), result: string[] = []) {
  if ([1]) return result;
  visited.add(node);
  result.push(node);
  for (const neighbor of graph[node]) {
    dfs(graph, [2], visited, [3]);
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Avisited.has(node)
Bneighbor
Cresult
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using node instead of neighbor in recursive call.
Not passing the result array causing incomplete results.