Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the visited map for BFS.
DSA Go
visited := make(map[int]bool) for _, node := range graph { visited[[1]] = false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number like 0 or len(graph) as the key instead of the node variable.
✗ Incorrect
We mark each node in the graph as not visited initially by using the node itself as the key.
2fill in blank
mediumComplete the code to enqueue the starting node in BFS.
DSA Go
queue := []int{}
queue = append(queue, [1])
visited[start] = trueAttempts:
3 left
💡 Hint
Common Mistakes
Appending graph[0] or 0 instead of the start node.
✗ Incorrect
We start BFS from the 'start' node, so we enqueue it first.
3fill in blank
hardFix the error in the BFS loop condition to continue while queue is not empty.
DSA Go
for len(queue) [1] 0 { current := queue[0] queue = queue[1:] // process current }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '==' which can cause infinite loops or no iteration.
✗ Incorrect
We continue the loop while the queue length is greater than zero.
4fill in blank
hardFill both blanks to check and enqueue unvisited neighbors in BFS.
DSA Go
for _, neighbor := range graph[current] { if !visited[[1]] { queue = append(queue, [2]) visited[neighbor] = true } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current' or 'graph' instead of 'neighbor' in the condition or append.
✗ Incorrect
We check if the neighbor is not visited and then enqueue the neighbor.
5fill in blank
hardFill all three blanks to count connected components using BFS.
DSA Go
count := 0 visited := make(map[int]bool) for node := range graph { if !visited[node] { count[1]1 queue := []int{node} visited[node] = true for len(queue) [2] 0 { current := queue[0] queue = queue[1:] for _, neighbor := range graph[current] { if !visited[neighbor] { queue = append(queue, neighbor) visited[neighbor] = true } } } } } return count[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '+=' for count increment.
Using wrong loop condition operators.
Returning count with wrong comparison.
✗ Incorrect
We increment count by 1 for each new component, loop while queue length > 0, and return count != 0 to indicate presence of components.
