Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a 3x3 adjacency matrix initialized with zeros.
DSA Rust
let mut matrix = vec![vec![[1]; 3]; 3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 initializes all edges as present.
Using None causes a type mismatch error.
✗ Incorrect
The adjacency matrix is initialized with zeros to represent no edges between nodes.
2fill in blank
mediumComplete the code to set an edge from node 0 to node 2 in the adjacency matrix.
DSA Rust
matrix[0][[1]] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 causes an out-of-bounds error.
Using index 1 sets edge to wrong node.
✗ Incorrect
Setting matrix[0][2] = 1 marks an edge from node 0 to node 2.
3fill in blank
hardFix the error in the code to correctly check if there is an edge from node 1 to node 0.
DSA Rust
if matrix[[1]][0] == 1 { println!("Edge exists"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as source index checks wrong edge.
Using index 3 causes out-of-bounds error.
✗ Incorrect
To check edge from node 1 to node 0, use matrix[1][0].
4fill in blank
hardFill both blanks to iterate over all edges and print them if they exist.
DSA Rust
for i in 0..[1] { for j in 0..[2] { if matrix[i][j] == 1 { println!("Edge from {} to {}", i, j); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed numbers limits matrix size.
Mixing rows and columns causes index errors.
✗ Incorrect
Use matrix.len() for rows and matrix[0].len() for columns to cover all nodes.
5fill in blank
hardFill all three blanks to create a function that returns true if there is an edge from src to dst.
DSA Rust
fn has_edge(matrix: &Vec<Vec<i32>>, src: usize, dst: usize) -> bool {
if matrix[[1]][[2]] == [3] {
true
} else {
false
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping src and dst indices.
Checking for 0 instead of 1.
✗ Incorrect
Check matrix[src][dst] == 1 to confirm edge presence.
