Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Rust
let new_node = Box::new(Node { data: [1], next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'tail' instead of the value.
Setting data to None which is invalid.
✗ Incorrect
We create a new node with the data field set to the given value.
2fill in blank
mediumComplete the code to check if the list is empty by checking the head pointer.
DSA Rust
if self.head.is_[1]() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_some instead of is_none.
Using non-existent methods like is_empty or is_null.
✗ Incorrect
We check if head is None to know if the list is empty.
3fill in blank
hardFix the error in the code to update the tail pointer after inserting the new node.
DSA Rust
self.tail = Some([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Dereferencing new_node which moves the value.
Taking reference &new_node which is a temporary reference.
Wrapping new_node again in Box::new which is unnecessary.
✗ Incorrect
The tail should point to the new node wrapped in Some, which is new_node.
4fill in blank
hardFill both blanks to link the current tail's next to the new node and update the tail pointer.
DSA Rust
if let Some(tail) = self.tail.as_mut() { tail.next = [1]; self.tail = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning new_node directly to tail.next without wrapping in Some.
Wrapping new_node twice in Box::new.
Not updating self.tail correctly.
✗ Incorrect
We set tail.next to Some(new_node) and update self.tail to Some(new_node).
5fill in blank
hardFill all three blanks to complete the tail insertion method for the linked list.
DSA Rust
fn insert_at_end(&mut self, value: i32) {
let new_node = Box::new(Node { data: [1], next: None });
if self.head.is_none() {
self.head = Some(new_node);
self.tail = [2];
} else {
if let Some(tail) = self.tail.as_mut() {
tail.next = Some(new_node);
self.tail = [3];
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variables for tail assignment.
Not wrapping new_node in Some when assigning to tail.
Using tail.next.as_mut().unwrap() which is incorrect here.
✗ Incorrect
We create new_node with data = value, set tail to Some(new_node) when list empty, and update tail to Some(new_node) after insertion.
