Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance matrix with the given graph.
DSA Go
dist := make([][]int, n) for i := 0; i < n; i++ { dist[i] = make([]int, n) for j := 0; j < n; j++ { if i == j { dist[i][j] = [1] } else if graph[i][j] != 0 { dist[i][j] = graph[i][j] } else { dist[i][j] = 1 << 30 } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dist[i][i] to -1 or a large number instead of 0.
Not initializing the diagonal elements properly.
✗ Incorrect
The distance from any node to itself is zero, so dist[i][i] should be 0.
2fill in blank
mediumComplete the code to update the distance matrix using the Floyd Warshall algorithm.
DSA Go
for k := 0; k < n; k++ { for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][k] != 1<<30 && dist[k][j] != 1<<30 && dist[i][k] + dist[k][j] [1] dist[i][j] { dist[i][j] = dist[i][k] + dist[k][j] } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes no updates or wrong updates.
Using == or != does not correctly find shorter paths.
✗ Incorrect
We update dist[i][j] only if the new path through k is shorter, so we check if dist[i][k] + dist[k][j] < dist[i][j].
3fill in blank
hardFix the error in the code that prints the shortest distance matrix after Floyd Warshall completes.
DSA Go
for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][j] == [1] { fmt.Print("INF ") } else { fmt.Print(dist[i][j], " ") } } fmt.Println() }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 or -1 instead of the large infinity value.
Printing wrong values for unreachable nodes.
✗ Incorrect
We used 1 << 30 as infinity, so distances equal to that mean no path exists and should print INF.
4fill in blank
hardFill both blanks to complete the code that initializes the graph and runs Floyd Warshall.
DSA Go
graph := [][]int{
{0, 3, [1], 7},
{8, 0, 2, [2],
{5, [2], 0, 1},
{2, [1], [2], 0},
}
n := len(graph)
// Initialize dist and run Floyd Warshall algorithmAttempts:
3 left
💡 Hint
Common Mistakes
Using 0 for missing edges causes confusion with actual zero-weight edges.
Using 'INF' as a string instead of a number causes errors.
✗ Incorrect
We use a large number (100000) to represent no direct edge between nodes in the graph.
5fill in blank
hardFill all three blanks to complete the code that updates the distance matrix and prints the final shortest paths.
DSA Go
for k := 0; k < n; k++ { for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][k] != [1] && dist[k][j] != [1] && dist[i][k] + dist[k][j] [2] dist[i][j] { dist[i][j] = dist[i][k] + dist[k][j] } } } } for i := 0; i < n; i++ { for j := 0; j < n; j++ { if dist[i][j] == [3] { fmt.Print("INF ") } else { fmt.Print(dist[i][j], " ") } } fmt.Println() }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of infinity for checks causes wrong updates.
Using > instead of < causes no updates.
Printing wrong values for unreachable nodes.
✗ Incorrect
We check that dist[i][k] and dist[k][j] are not infinity (1 << 30), update if new path is shorter (<), and print INF for distances equal to infinity.
