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 { value: [1], prev: None, next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the function like 'value' or 'data'.
✗ Incorrect
The variable holding the value to insert is named 'val' in this context.
2fill in blank
mediumComplete the code to check if the position is zero to insert at the head.
DSA Rust
if position == [1] { insert_at_head(&mut self, val); return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for position == 1 instead of 0.
Using None or negative numbers for position.
✗ Incorrect
Position zero means inserting at the head of the list.
3fill in blank
hardFix the error in advancing the current node to the correct position.
DSA Rust
for _ in 0..position-1 { current = current.unwrap().next.as_mut().[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'clone' which copies the node instead of referencing.
Using 'unwrap' which consumes the Option and causes errors.
✗ Incorrect
We need to get a mutable reference to the next node using 'as_mut()'.
4fill in blank
hardFill both blanks to correctly link the new node between nodes.
DSA Rust
new_node.prev = Some(Box::into_raw(current.as_mut().unwrap())); new_node.next = current.as_mut().unwrap().next.[1]; current.as_mut().unwrap().next = Some(new_node); if let Some(next_node) = current.as_mut().unwrap().next.as_mut().unwrap().next.[2]() { next_node.prev = Some(Box::into_raw(current.as_mut().unwrap().next.as_mut().unwrap())); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'clone' instead of 'take' causes duplication.
Using 'unwrap' instead of 'as_mut' consumes the Option.
✗ Incorrect
Use 'take()' to move the next node out and 'as_mut()' to get mutable reference.
5fill in blank
hardFill all three blanks to complete the insert_at_position function correctly.
DSA Rust
fn insert_at_position(&mut self, val: i32, position: usize) {
if position == 0 {
self.insert_at_head(val);
return;
}
let mut current = self.head.as_mut();
for _ in 0..position-1 {
current = current.unwrap().next.as_mut().[1]();
}
let mut new_node = Box::new(Node { value: val, prev: None, next: None });
new_node.prev = Some(Box::into_raw(current.unwrap()));
new_node.next = current.unwrap().next.[2]();
current.unwrap().next = Some(new_node);
if let Some(next_node) = current.unwrap().next.as_mut().unwrap().next.[3]() {
next_node.prev = Some(Box::into_raw(current.unwrap().next.as_mut().unwrap()));
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'clone()' which duplicates nodes incorrectly.
Using 'unwrap()' which consumes Options and causes errors.
✗ Incorrect
Use 'as_mut()' to get mutable references and 'take()' to move the next node safely.
