Challenge - 5 Problems
DFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateDFS traversal order on a simple graph
What is the output order of nodes visited by the DFS starting from node 'A'?
DSA Javascript
const graph = {
A: ['B', 'C'],
B: ['D'],
C: ['E'],
D: [],
E: []
};
function dfs(graph, start) {
const visited = new Set();
const result = [];
function traverse(node) {
if (visited.has(node)) return;
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
traverse(neighbor);
}
}
traverse(start);
return result;
}
console.log(dfs(graph, 'A'));Attempts:
2 left
💡 Hint
Remember DFS explores as far as possible along each branch before backtracking.
✗ Incorrect
Starting at 'A', DFS visits 'B' first, then 'D' (no neighbors), backtracks to 'A', then visits 'C' and 'E'.
❓ Predict Output
intermediateDFS on graph with cycle detection
What is the output of the DFS traversal starting from node 1 on this graph with a cycle?
DSA Javascript
const graph = {
1: [2, 3],
2: [4],
3: [2],
4: [1]
};
function dfs(graph, start) {
const visited = new Set();
const result = [];
function traverse(node) {
if (visited.has(node)) return;
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
traverse(neighbor);
}
}
traverse(start);
return result;
}
console.log(dfs(graph, 1));Attempts:
2 left
💡 Hint
DFS should not revisit nodes already visited, even if cycles exist.
✗ Incorrect
DFS visits 1, then 2, then 4. From 4, it tries to visit 1 but skips since visited. Then backtracks and visits 3.
🔧 Debug
advancedIdentify the error in DFS implementation
What error will this DFS code produce when run on the given graph?
DSA Javascript
const graph = {
A: ['B'],
B: ['C'],
C: ['A']
};
function dfs(graph, start) {
const visited = new Set();
const result = [];
function traverse(node) {
if (visited.has(node)) return;
result.push(node);
for (const neighbor of graph[node]) {
traverse(neighbor);
}
visited.add(node);
}
traverse(start);
return result;
}
console.log(dfs(graph, 'A'));Attempts:
2 left
💡 Hint
Check when nodes are marked visited in the recursion.
✗ Incorrect
The visited set is updated after recursion, so nodes are revisited causing infinite recursion and stack overflow.
❓ Predict Output
advancedDFS traversal on disconnected graph
What is the output of DFS starting from node 'X' on this disconnected graph?
DSA Javascript
const graph = {
X: ['Y'],
Y: [],
Z: ['W'],
W: []
};
function dfs(graph, start) {
const visited = new Set();
const result = [];
function traverse(node) {
if (visited.has(node)) return;
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
traverse(neighbor);
}
}
traverse(start);
return result;
}
console.log(dfs(graph, 'X'));Attempts:
2 left
💡 Hint
DFS only visits nodes reachable from the start node.
✗ Incorrect
Starting at 'X', DFS visits 'Y'. Nodes 'Z' and 'W' are disconnected and not visited.
❓ Predict Output
expertDFS with stack instead of recursion
What is the output of this iterative DFS starting from node 1?
DSA Javascript
const graph = {
1: [2, 3],
2: [4],
3: [5],
4: [],
5: []
};
function dfsIterative(graph, start) {
const stack = [start];
const visited = new Set();
const result = [];
while (stack.length > 0) {
const node = stack.pop();
if (!visited.has(node)) {
visited.add(node);
result.push(node);
for (let i = graph[node].length - 1; i >= 0; i--) {
stack.push(graph[node][i]);
}
}
}
return result;
}
console.log(dfsIterative(graph, 1));Attempts:
2 left
💡 Hint
Stack LIFO order affects traversal order; neighbors are pushed in reverse order.
✗ Incorrect
Stack causes DFS to visit 1, then 2, then 4, backtrack to 3, then 5.
