Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a queue for level order traversal.
DSA Rust
let mut queue = VecDeque::new();
queue.[1](root);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_front or pop_back instead of push_back to add elements.
Using push_front which adds to the front, not suitable for BFS queue.
✗ Incorrect
We use push_back to add elements at the end of the queue for BFS traversal.
2fill in blank
mediumComplete the code to calculate the width of the current level.
DSA Rust
let width = last_index - [1] + 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using level_size instead of first_index in the calculation.
Forgetting to add one to the difference.
✗ Incorrect
Width is calculated as last index minus first index plus one.
3fill in blank
hardFix the error in the code to correctly assign indices to child nodes.
DSA Rust
if let Some(left) = node.left.as_ref() { queue.push_back((left, [1] * 2)); } if let Some(right) = node.right.as_ref() { queue.push_back((right, [1] * 2 + 1)); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using level or width instead of index for child node positions.
Using node_val which is unrelated to indexing.
✗ Incorrect
Child indices are calculated based on the parent's index.
4fill in blank
hardFill both blanks to update the maximum width correctly.
DSA Rust
max_width = max(max_width, [1] - [2] + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping first_index and last_index in subtraction.
Using level_size or current_index incorrectly.
✗ Incorrect
Maximum width is updated by comparing current width (last_index - first_index + 1).
5fill in blank
hardFill all three blanks to complete the BFS loop for maximum width calculation.
DSA Rust
while let Some((node, [1])) = queue.pop_front() { let level_size = queue.len() + 1; let first_index = [2]; let last_index = [3]; // process children and update max_width }
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.val instead of index for position tracking.
Incorrectly accessing queue elements for indices.
✗ Incorrect
We pop node and index, then get first and last indices from queue for width calculation.
