Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance array with infinity values.
DSA Go
dist := make([]int, V) for i := 0; i < V; i++ { dist[i] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing distances with 0 causes incorrect shortest path calculations.
Using negative numbers for initial distances.
✗ Incorrect
We initialize distances with a very large number (infinity) to represent unreachable nodes initially.
2fill in blank
mediumComplete the code to update the distance of adjacent nodes during relaxation.
DSA Go
for _, edge := range graph[u] { v := edge.to weight := edge.weight if dist[u] != [1] && dist[u]+weight < dist[v] { dist[v] = dist[u] + weight } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if dist[u] is infinity causes incorrect updates.
Using 0 instead of infinity in the condition.
✗ Incorrect
We check if dist[u] is not infinity before relaxing edges to avoid invalid updates.
3fill in blank
hardFix the error in the topological sort function to correctly mark nodes as visited.
DSA Go
func topoSort(u int, visited []bool, stack *[]int, graph [][]Edge) {
visited[u] = [1]
for _, edge := range graph[u] {
v := edge.to
if !visited[v] {
topoSort(v, visited, stack, graph)
}
}
*stack = append(*stack, u)
}Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[u] as false causes infinite loops.
Not marking visited[u] at all.
✗ Incorrect
Marking the node as true means it is visited, preventing revisiting it.
4fill in blank
hardFill both blanks to correctly initialize the distance of the source node and iterate over the topological order.
DSA Go
dist[[1]] = 0 for i := len(stack) - 1; i >= 0; i-- { u := stack[[2]] if dist[u] != math.MaxInt32 { for _, edge := range graph[u] { v := edge.to weight := edge.weight if dist[u]+weight < dist[v] { dist[v] = dist[u] + weight } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of source for distance initialization.
Using len(stack) instead of i to index stack.
✗ Incorrect
Set distance of source to 0 and use index i to access nodes in topological order.
5fill in blank
hardFill all three blanks to create the Edge struct, define the graph, and declare the stack for topological sort.
DSA Go
type [1] struct { to int weight int } graph := make([][][2], V) stack := make([][3], 0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Node instead of Edge for struct or graph.
Declaring graph as slice of int instead of Edge.
✗ Incorrect
Edge struct defines graph edges; graph is a slice of Edge slices; stack holds ints for node order.
