Bird
Raised Fist0
DSA Rustprogramming~5 mins

Count Words with Given Prefix in DSA Rust - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the problem 'Count Words with Given Prefix' ask you to do?
It asks to count how many words in a list start with a specific prefix string.
Click to reveal answer
intermediate
Why is a Trie (prefix tree) useful for counting words with a given prefix?
A Trie stores words so that common prefixes share nodes, making it fast to find and count words starting with a prefix.
Click to reveal answer
intermediate
In Rust, what data structure can efficiently represent children nodes in a Trie?
A HashMap can be used to map each character to its child node.
Click to reveal answer
intermediate
What is the time complexity to count words with a given prefix using a Trie?
It is O(m), where m is the length of the prefix, because you only traverse nodes matching the prefix.
Click to reveal answer
beginner
What is a simple way to count words with a prefix without a Trie?
You can iterate over all words and check if each starts with the prefix, but this is slower (O(n * m)).
Click to reveal answer
What data structure is best suited for prefix-based word counting?
AStack
BQueue
CTrie
DLinked List
What does the prefix length affect in a Trie search?
AThe number of nodes traversed
BThe total number of words stored
CThe size of the Trie
DThe number of children per node
In Rust, which collection is commonly used to map characters to child nodes in a Trie?
AHashMap<char, TrieNode>
BVec<char>
CBTreeSet<char>
DLinkedList<char>
What is the brute force approach to count words with a prefix?
ASort words and binary search
BUse a Trie to store words
CUse a stack to store prefixes
DCheck each word if it starts with the prefix
What is the main advantage of using a Trie over brute force for prefix counting?
ALess memory usage
BFaster prefix queries
CSimpler code
DNo need to store words
Explain how a Trie helps count words with a given prefix efficiently.
Think about how words with the same start share nodes.
You got /4 concepts.
    Describe the brute force method to count words with a prefix and its drawbacks.
    Consider checking each word one by one.
    You got /4 concepts.

      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