Bird
Raised Fist0
DSA Rustprogramming~10 mins

Count Words with Given Prefix in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a word starts with the given prefix.

DSA Rust
if word.[1](prefix) { count += 1; }
Drag options to blanks, or click blank then click option'
Aends_with
Bcontains
Cstarts_with
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' instead of 'starts_with' will check anywhere in the string, not just the start.
Using 'ends_with' checks the end of the string, which is incorrect here.
2fill in blank
medium

Complete the code to iterate over all words in the list.

DSA Rust
for [1] in words.iter() {
Drag options to blanks, or click blank then click option'
Aindex
Bprefix
Ccount
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prefix' as the loop variable is confusing because prefix is the search term.
Using 'count' or 'index' does not represent the word itself.
3fill in blank
hard

Fix the error in the code to convert the prefix to a string slice.

DSA Rust
let prefix_str = &prefix[1]();
Drag options to blanks, or click blank then click option'
Ato_string
Bas_str
Cclone
Dto_owned
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to_string' creates a new String, which is unnecessary here.
Using 'clone' copies the whole String, which is inefficient.
4fill in blank
hard

Fill both blanks to create a function that counts words starting with the prefix.

DSA Rust
fn count_words_with_prefix(words: &[String], prefix: &str) -> usize {
    let mut count = 0;
    for [1] in words.iter() {
        if [2].starts_with(prefix) {
            count += 1;
        }
    }
    count
}
Drag options to blanks, or click blank then click option'
Aword
Bprefix
Ccount
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prefix' inside the loop instead of the current word.
Using 'count' or 'words' in the condition instead of the loop variable.
5fill in blank
hard

Fill all three blanks to create a main function that tests counting words with a prefix.

DSA Rust
fn main() {
    let words = vec!["apple".to_string(), "app".to_string(), "banana".to_string()];
    let prefix = "app";
    let result = count_words_with_prefix([1], [2]);
    println!("Count: {}", [3]);
}
Drag options to blanks, or click blank then click option'
A&words
Bprefix
Cresult
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'words' instead of '&words' causes a type error.
Printing 'prefix' or 'words' instead of the result.

Practice

(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Find the longest word in a list
B. Count how many words in a list start with a specific prefix
C. Count the total number of characters in all words
D. Sort the words alphabetically

Solution

  1. Step 1: Understand the problem statement

    The problem asks to find how many words start with a given prefix, not about length or sorting.
  2. Step 2: Identify the key task

    Counting words that begin with the prefix is the main task.
  3. Final Answer:

    Count how many words in a list start with a specific prefix -> Option B
  4. Quick Check:

    Count words starting with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not full word or sorting [OK]
Common Mistakes:
  • Confusing prefix with substring anywhere in word
  • Counting total characters instead of words
  • Sorting words instead of counting
2. Which of the following Rust code snippets correctly checks if a word starts with a given prefix prefix?
easy
A. word.starts_with(prefix)
B. word.contains(prefix)
C. word.ends_with(prefix)
D. word == prefix

Solution

  1. Step 1: Recall Rust string methods

    Rust's starts_with method checks if a string begins with a given substring.
  2. Step 2: Compare options

    contains checks anywhere, ends_with checks suffix, and == checks full equality, so only starts_with fits.
  3. Final Answer:

    word.starts_with(prefix) -> Option A
  4. Quick Check:

    Use starts_with() to check prefix [OK]
Hint: Use starts_with() method to check prefix in Rust strings [OK]
Common Mistakes:
  • Using contains() which matches anywhere, not just prefix
  • Using ends_with() which checks suffix
  • Comparing full equality instead of prefix
3. What is the output of this Rust code?
let words = vec!["apple", "app", "application", "banana"];
let prefix = "app";
let count = words.iter().filter(|w| w.starts_with(prefix)).count();
println!("{}", count);
medium
A. 3
B. 2
C. 1
D. 4

Solution

  1. Step 1: Identify words starting with "app"

    Words are "apple", "app", "application", and "banana". The first three start with "app".
  2. Step 2: Count matching words

    Count is 3 because "apple", "app", and "application" start with "app".
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 words start with "app" [OK]
Hint: Count words where starts_with(prefix) is true [OK]
Common Mistakes:
  • Counting only exact matches
  • Including words that don't start with prefix
  • Off-by-one counting errors
4. The following Rust code tries to count words starting with a prefix but gives wrong output. What is the bug?
let words = vec!["cat", "car", "dog", "scare"];
let prefix = "car";
let count = words.iter().filter(|w| w.contains(prefix)).count();
println!("{}", count);
medium
A. Missing semicolon after count declaration
B. Prefix variable is not declared correctly
C. The vector words is empty
D. Using contains() instead of starts_with() causes wrong matches

Solution

  1. Step 1: Understand difference between contains() and starts_with()

    contains() checks if prefix appears anywhere, not just at start.
  2. Step 2: Identify incorrect matches

    "scare" contains "car" but does not start with "car", so contains() wrongly counts it.
  3. Final Answer:

    Using contains() instead of starts_with() causes wrong matches -> Option D
  4. Quick Check:

    Use starts_with() to count prefix matches [OK]
Hint: Replace contains() with starts_with() to fix prefix counting [OK]
Common Mistakes:
  • Confusing contains() with starts_with()
  • Assuming contains() only matches prefix
  • Ignoring method differences
5. Given a list of words ["test", "tester", "testing", "toast", "team"] and prefix "te", which Rust code correctly counts words starting with the prefix and ignores case sensitivity?
hard
A. words.iter().filter(|w| w.contains(prefix)).count()
B. words.iter().filter(|w| w.starts_with(prefix)).count()
C. words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count()
D. words.iter().filter(|w| w.ends_with(prefix)).count()

Solution

  1. Step 1: Understand case sensitivity in Rust strings

    Rust's starts_with is case sensitive, so we convert both word and prefix to lowercase.
  2. Step 2: Check each option for case-insensitive prefix check

    words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() converts both to lowercase before checking prefix, ensuring case-insensitive match. Others do not handle case.
  3. Final Answer:

    words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() -> Option C
  4. Quick Check:

    Convert both strings to lowercase before starts_with() [OK]
Hint: Lowercase both word and prefix before starts_with() for case-insensitive [OK]
Common Mistakes:
  • Ignoring case sensitivity in starts_with()
  • Using contains() which is wrong for prefix
  • Using ends_with() instead of starts_with()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard