Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize a graph adjacency list in Go.
DSA Go
graph := make(map[int][]int) graph[1] = []int{2, 3} graph[2] = []int{4} graph[3] = []int{4} graph[4] = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using nil instead of an empty slice can cause runtime errors when iterating.
Adding edges back to node 1 creates cycles, which is not needed here.
✗ Incorrect
Node 4 has no outgoing edges, so its adjacency list is an empty slice.
2fill in blank
mediumComplete the code to add an edge from node 2 to node 5 in the graph.
DSA Go
graph[2] = append(graph[2], [1])
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the wrong node number creates incorrect edges.
Appending to the wrong node's adjacency list.
✗ Incorrect
We want to add an edge from node 2 to node 5, so append 5 to graph[2].
3fill in blank
hardFix the error in the BFS queue initialization for shortest path search.
DSA Go
queue := []int[1]Attempts:
3 left
💡 Hint
Common Mistakes
Starting BFS from node 0 when nodes start at 1.
Initializing queue as nil or empty slice causes no traversal.
✗ Incorrect
The BFS starts from node 1, so the queue should start with 1.
4fill in blank
hardFill both blanks to check if a neighbor node has been visited before adding to the queue.
DSA Go
if !visited[[1]] { visited[neighbor] = true queue = append(queue, [2]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' instead of 'neighbor' causes logic errors.
Using 'visited' or 'queue' as variables in condition or append.
✗ Incorrect
We check if 'neighbor' is visited and then add 'neighbor' to the queue.
5fill in blank
hardFill all three blanks to update the distance map during BFS shortest path search.
DSA Go
distance[[1]] = distance[[2]] [3] 1
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 instead of adding causes wrong distances.
Swapping neighbor and node in distance update.
✗ Incorrect
Distance to neighbor is distance to current node plus 1 edge.
