Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance vector with infinity values.
DSA Rust
let mut dist = vec![[1]; graph.len()];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of a large number causes incorrect shortest path calculations.
Using negative numbers is invalid for usize type.
✗ Incorrect
We use usize::MAX to represent infinity for distances initially.
2fill in blank
mediumComplete the code to push the starting node with distance zero into the priority queue.
DSA Rust
pq.push((0, [1]));
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing graph.len() causes out-of-bounds errors.
Using dist[0] instead of the source node index is incorrect.
✗ Incorrect
We push the source node with distance zero to start the algorithm.
3fill in blank
hardFix the error in the condition to skip outdated queue entries.
DSA Rust
if current_dist > dist[[1]] { continue; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using neighbor instead of node causes wrong comparisons.
Using weight or source here is incorrect.
✗ Incorrect
We compare current_dist with dist[node] to skip if we already have a better distance.
4fill in blank
hardFill both blanks to update the distance and push the neighbor into the priority queue.
DSA Rust
if dist[node] + weight < dist[[1]] { dist[[2]] = dist[node] + weight; pq.push((dist[[2]], [2])); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using node instead of neighbor causes wrong updates.
Using weight or source as indices causes errors.
✗ Incorrect
We update dist[neighbor] and push neighbor with new distance into the queue.
5fill in blank
hardFill all three blanks to correctly iterate over neighbors and their weights.
DSA Rust
for &(neighbor, [1]) in &graph[[2]] { let weight = [3]; // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using neighbor as index causes errors.
Confusing weight variable names leads to bugs.
✗ Incorrect
We destructure neighbor and weight from graph[node], then assign weight to variable.
