Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The DFS function should call itself recursively on each neighbor of the current node.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' operator which doesn't work for Set.
Using contains or includes which are not Set methods.
✗ Incorrect
The Set method to check if an element exists is has().
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a new Set() causes infinite recursion.
Passing undefined causes runtime errors.
✗ Incorrect
The same visited set must be passed to keep track of visited nodes and prevent infinite loops.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing visited instead of result to recursive calls.
Not checking if node is visited before processing.
✗ Incorrect
Check if node is visited with visited.has(node) and pass the result array to recursive calls to collect nodes.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node instead of neighbor in recursive call.
Not passing the result array causing incomplete results.
✗ Incorrect
Check if node is visited with visited.has(node), recurse on neighbor, and pass the result array to collect visited nodes.