Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the linked list contains the target value.
DSA Rust
fn contains(head: &Option<Box<Node>>, target: i32) -> bool {
let mut current = head;
while let Some(node) = current {
if node.value == [1] {
return true;
}
current = &node.next;
}
false
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong variable like 'node' or 'head' instead of 'target'.
Using 'value' without specifying the node.
✗ Incorrect
We compare the current node's value with the target value to find if it exists in the list.
2fill in blank
mediumComplete the code to move to the next node in the linked list during search.
DSA Rust
fn contains(head: &Option<Box<Node>>, target: i32) -> bool {
let mut current = head;
while let Some(node) = current {
if node.value == target {
return true;
}
current = &node.[1];
}
false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists, not singly linked lists.
Using incorrect field names like 'child' or 'link'.
✗ Incorrect
To traverse the linked list, we move from the current node to its 'next' node.
3fill in blank
hardFix the error in the code to correctly return false when the value is not found.
DSA Rust
fn contains(head: &Option<Box<Node>>, target: i32) -> bool {
let mut current = head;
while let Some(node) = current {
if node.value == target {
return true;
}
current = &node.next;
}
[1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true by mistake at the end.
Returning None which is not a boolean.
✗ Incorrect
If the loop finishes without finding the target, the function should return false.
4fill in blank
hardFill both blanks to create a function that searches for a value and returns its position (index) or -1 if not found.
DSA Rust
fn find_index(head: &Option<Box<Node>>, target: i32) -> i32 {
let mut current = head;
let mut index = 0;
while let Some(node) = current {
if node.value == [1] {
return index;
}
current = &node.[2];
index += 1;
}
-1
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names like 'prev' for singly linked list.
Comparing with 'value' instead of 'target'.
✗ Incorrect
We compare with 'target' and move to the 'next' node while counting the index.
5fill in blank
hardFill all three blanks to create a function that returns true if the linked list contains any value greater than the target.
DSA Rust
fn contains_greater(head: &Option<Box<Node>>, target: i32) -> bool {
let mut current = head;
while let Some(node) = current {
if node.[1] [2] [3] {
return true;
}
current = &node.next;
}
false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for comparison.
Comparing with wrong variables or fields.
✗ Incorrect
We check if node.value is greater than the target value.
