Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the prefix as the first string in the list.
DSA Rust
let mut prefix = [1][0].clone();
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name like 'words' or 'list'.
Forgetting to clone the string.
✗ Incorrect
We start the prefix as the first string in the input list named 'strings'.
2fill in blank
mediumComplete the code to loop through each string in the list.
DSA Rust
for s in [1].iter() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name.
Forgetting to use .iter() for looping.
✗ Incorrect
We loop through the input list named 'strings' using .iter().
3fill in blank
hardFix the error in the while loop condition to check prefix matching.
DSA Rust
while !s.starts_with(&[1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable in starts_with.
Not using a reference with &prefix.
✗ Incorrect
We check if the current string 's' starts with the current prefix.
4fill in blank
hardFill both blanks to shorten the prefix by one character from the end.
DSA Rust
prefix = prefix[1](0..prefix[2]().saturating_sub(1)).to_string();
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or size instead of len().
Not using chars() for slicing characters.
✗ Incorrect
We use prefix.chars() to get characters and prefix.len() to get length for slicing.
5fill in blank
hardFill all three blanks to return the prefix or empty string if none.
DSA Rust
if strings.is_empty() { return [1].to_string(); } let mut prefix = strings[0].clone(); for s in strings.iter() { while !s.starts_with(&prefix) { prefix = prefix[2](0..prefix[3]().saturating_sub(1)).to_string(); if prefix.is_empty() { return String::new(); } } } prefix
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of empty string.
Using wrong methods for slicing prefix.
✗ Incorrect
Return empty string with String::new(), use chars() and len() to shorten prefix.
