Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the visited vector for all nodes.
DSA Rust
let mut visited = vec![];
visited.[1](n, false);Attempts:
3 left
💡 Hint
Common Mistakes
Using vec::new() creates an empty vector with zero length.
vec::with_capacity only reserves space but does not set length or values.
✗ Incorrect
We use vec::resize to create a vector of size n filled with false values to mark all nodes as unvisited.
2fill in blank
mediumComplete the code to add a node to the BFS queue.
DSA Rust
queue.push_back([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Adding the entire graph or neighbors instead of a single node.
Trying to add the visited vector instead of a node.
✗ Incorrect
We add the current node to the queue to start BFS from it.
3fill in blank
hardFix the error in the BFS loop condition to continue while the queue is not empty.
DSA Rust
while !queue.[1]() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_front() in the condition causes a runtime error.
Using len() without comparison does not return a boolean.
✗ Incorrect
We use is_empty() to check if the queue has no elements left; negating it continues while queue has elements.
4fill in blank
hardFill both blanks to mark a neighbor as visited and add it to the queue.
DSA Rust
if !visited[neighbor] { visited[neighbor] = [1]; queue.[2](neighbor); }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting visited to false instead of true.
Using pop_front to add elements instead of push_back.
✗ Incorrect
We mark the neighbor as visited by setting true and add it to the queue using push_back.
5fill in blank
hardFill all three blanks to count connected components using BFS.
DSA Rust
let mut count = 0; for node in 0..n { if !visited[[1]] { count += [2]; bfs([3], &graph, &mut visited); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for node.
Incrementing count by 0 or not at all.
✗ Incorrect
We check if the current node is unvisited, increment count by 1, and run bfs starting from that node.
