Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance slice with maximum integer values.
DSA Go
dist := make([]int, n) for i := 0; i < n; i++ { dist[i] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing distances to 0 which means all nodes are at zero distance initially.
Using negative values which are invalid for distances.
✗ Incorrect
We initialize distances to the maximum integer value to represent infinity before starting Dijkstra's algorithm.
2fill in blank
mediumComplete the code to update the distance if a shorter path is found.
DSA Go
if dist[u]+weight < dist[v] { dist[v] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning dist[u] instead of dist[u] + weight.
Assigning weight alone which ignores the distance to u.
✗ Incorrect
We update dist[v] to the new shorter distance which is dist[u] plus the edge weight.
3fill in blank
hardFix the error in the priority queue pop operation to get the node with the smallest distance.
DSA Go
current := pq.[1]()Attempts:
3 left
💡 Hint
Common Mistakes
Using Push which adds an element instead of removing.
Using Peek which only views the element without removing.
✗ Incorrect
Pop removes and returns the element with the smallest priority (distance) from the priority queue.
4fill in blank
hardFill both blanks to correctly iterate over neighbors and update distances.
DSA Go
for _, [1] := range graph[u] { v, weight := [2].node, [2].weight if dist[u]+weight < dist[v] { dist[v] = dist[u] + weight pq.Push(Item{node: v, priority: dist[v]}) } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using variable names that do not match the graph's edge struct.
✗ Incorrect
We use 'edge' as the variable name for each neighbor in graph[u] and access its node and weight fields.
5fill in blank
hardFill all three blanks to correctly initialize the priority queue and set the source distance.
DSA Go
dist := make([]int, n) for i := 0; i < n; i++ { dist[i] = [1] } dist[[2]] = 0 pq := &PriorityQueue{} pq.[3](Item{node: [2], priority: 0})
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting source distance to zero.
Using Pop instead of Push to add to the priority queue.
✗ Incorrect
Distances are initialized to max int, source distance set to 0, and source node pushed into the priority queue.
