Challenge - 5 Problems
DFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of DFS traversal on a simple graph
What is the output of the following DFS traversal starting from node 'A'?
DSA Python
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': [],
'F': []
}
visited = set()
result = []
def dfs(node):
if node not in visited:
visited.add(node)
result.append(node)
for neighbor in graph[node]:
dfs(neighbor)
dfs('A')
print(result)Attempts:
2 left
💡 Hint
Remember DFS explores as far as possible along each branch before backtracking.
✗ Incorrect
The DFS starts at 'A', visits 'B' first, then explores 'B's neighbors 'D' and 'E' before backtracking to 'A' and visiting 'C' and its neighbor 'F'.
🧠 Conceptual
intermediateNumber of nodes visited in DFS
Given the graph below and starting DFS from node '1', how many nodes will be visited?
DSA Python
graph = {
1: [2, 3],
2: [4],
3: [],
4: [5],
5: []
}
visited = set()
def dfs(node):
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
dfs(neighbor)
dfs(1)
print(len(visited))Attempts:
2 left
💡 Hint
Count all nodes reachable from node 1.
✗ Incorrect
Starting from node 1, DFS visits nodes 1, 2, 4, 5, and 3, totaling 5 nodes.
🔧 Debug
advancedIdentify the error in DFS implementation
What error will the following DFS code produce when run?
DSA Python
graph = {'X': ['Y'], 'Y': ['Z'], 'Z': []}
visited = set()
result = []
def dfs(node):
if node not in visited:
visited.add(node)
result.append(node)
for neighbor in graph[node]:
dfs(neighbor)
dfs('A')
print(result)Attempts:
2 left
💡 Hint
Check if the starting node exists in the graph keys.
✗ Incorrect
The code tries to access graph['A'], but 'A' is not a key in the graph dictionary, causing a KeyError.
❓ Predict Output
advancedDFS traversal order with cycle in graph
What is the output of the DFS traversal starting from node 1 on the graph with a cycle?
DSA Python
graph = {
1: [2],
2: [3],
3: [1, 4],
4: []
}
visited = set()
result = []
def dfs(node):
if node not in visited:
visited.add(node)
result.append(node)
for neighbor in graph[node]:
dfs(neighbor)
dfs(1)
print(result)Attempts:
2 left
💡 Hint
DFS should not revisit nodes already visited, even with cycles.
✗ Incorrect
The DFS visits 1, then 2, then 3, and finally 4. It does not revisit 1 again due to the cycle.
🚀 Application
expertDFS to find if path exists between two nodes
Which option correctly returns True if there is a path from start to end in the graph using DFS, otherwise False?
DSA Python
graph = {
'start': ['a', 'b'],
'a': ['c'],
'b': ['d'],
'c': ['end'],
'd': [],
'end': []
}
def has_path(graph, start, end, visited=None):
if visited is None:
visited = set()
if start == end:
return True
if start in visited:
return False
visited.add(start)
for neighbor in graph.get(start, []):
if has_path(graph, neighbor, end, visited):
return True
return False
print(has_path(graph, 'start', 'end'))Attempts:
2 left
💡 Hint
DFS explores neighbors recursively and returns True if end is found.
✗ Incorrect
The function correctly uses DFS to find a path from 'start' to 'end'. Since such a path exists, it returns True.
