Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an adjacency list as a vector of vectors.
DSA Rust
let mut graph: Vec<Vec<usize>> = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using Vec::new() creates an empty vector with no inner vectors.
Using vec![0; 5] creates a vector of zeros, not vectors.
✗ Incorrect
We use vec![vec![]; 5] to create a vector with 5 empty vectors, representing adjacency lists for 5 nodes.
2fill in blank
mediumComplete the code to add an edge from node 1 to node 3 in the adjacency list.
DSA Rust
graph[1].[1](3);
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert without an index causes errors.
Using append or extend expects another vector, not a single element.
✗ Incorrect
We use push to add a new neighbor to the adjacency list vector for node 1.
3fill in blank
hardFix the error in this code that tries to print neighbors of node 2.
DSA Rust
for &neighbor in &graph[[1]] { println!("{}", neighbor); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong node index causes printing neighbors of a different node.
Using an index out of bounds causes runtime panic.
✗ Incorrect
We want to print neighbors of node 2, so the index must be 2.
4fill in blank
hardFill both blanks to create an adjacency list for 4 nodes and add an edge from node 0 to node 2.
DSA Rust
let mut graph = vec![[1]; 4]; graph[0].[2](2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using vec![0] creates vectors with zero inside, not empty.
Using insert without index causes errors.
✗ Incorrect
We create 4 empty vectors for nodes, then push 2 to node 0's adjacency list.
5fill in blank
hardFill all three blanks to initialize a graph with 3 nodes, add edges 0->1 and 1->2, then print neighbors of node 1.
DSA Rust
let mut graph = vec![[1]; 3]; graph[0].[2](1); graph[1].[3](2); for &neighbor in &graph[1] { println!("{}", neighbor); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert without index causes errors.
Initializing with vec![0] creates vectors with zero, not empty.
✗ Incorrect
We create empty adjacency lists, then push neighbors 1 and 2 to nodes 0 and 1 respectively, then print neighbors of node 1.
