Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the length variable to zero before counting nodes.
DSA Rust
let mut length = [1]; let mut current = head.as_ref(); while let Some(node) = current { length += 1; current = node.next.as_ref(); } println!("Length: {}", length);
Attempts:
3 left
💡 Hint
Common Mistakes
Starting length at 1 instead of 0
Using None or head as initial length
✗ Incorrect
We start counting from zero because no nodes have been counted yet.
2fill in blank
mediumComplete the code to move to the next node in the linked list inside the loop.
DSA Rust
while let Some(node) = current { length += 1; current = node.[1].as_ref(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next'
Using unrelated field names like 'child' or 'parent'
✗ Incorrect
We move to the next node by accessing the 'next' field of the current node.
3fill in blank
hardFix the error in the loop condition to correctly iterate over the linked list nodes.
DSA Rust
while let Some([1]) = current { length += 1; current = node.next.as_ref(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current' which is the Option itself, not the inner value
Using unrelated variable names
✗ Incorrect
The pattern variable should be 'node' to destructure the Option and access node fields.
4fill in blank
hardFill both blanks to define a function that returns the length of a linked list.
DSA Rust
fn get_length(head: Option<Box<Node>>) -> usize {
let mut length = 0;
let mut current = [1];
while let Some(node) = current {
length += 1;
current = node.[2].as_ref();
}
length
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head directly instead of head.as_ref()
Using 'prev' instead of 'next'
✗ Incorrect
We start with a reference to the head node and move through 'next' pointers to count nodes.
5fill in blank
hardFill all three blanks to create a linked list node struct and a function to get its length.
DSA Rust
#[derive(Debug)] struct Node { value: i32, [1]: Option<Box<Node>>, } fn get_length(head: Option<Box<Node>>) -> usize { let mut length = 0; let mut current = head.as_ref(); while let Some(node) = current { length += 1; current = node.[2].as_ref(); } length } fn main() { let node3 = Node { value: 3, [3]: None }; println!("Length: {}", get_length(Some(Box::new(node3)))); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or other unrelated field names
Inconsistent field names between struct and traversal
✗ Incorrect
The linked list node uses 'next' to point to the next node. The function traverses using 'next'. The example node has 'next' set to None.
