Bird
Raised Fist0
DSA C#programming~20 mins

Count Words with Given Prefix in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of CountWordsWithPrefix Method
What is the output of the following C# code that counts words starting with a given prefix?
DSA C#
using System;
using System.Collections.Generic;

class Program {
    static int CountWordsWithPrefix(List<string> words, string prefix) {
        int count = 0;
        foreach (var word in words) {
            if (word.StartsWith(prefix)) {
                count++;
            }
        }
        return count;
    }

    static void Main() {
        var words = new List<string> {"apple", "app", "application", "banana", "apply", "ape"};
        Console.WriteLine(CountWordsWithPrefix(words, "app"));
    }
}
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Count how many words start exactly with "app".
🧠 Conceptual
intermediate
1:30remaining
Understanding Prefix Matching
Which of the following best describes what the method CountWordsWithPrefix does?
ACounts how many words start exactly with the given prefix.
BCounts how many words contain the prefix anywhere inside the word.
CCounts how many words end with the given prefix.
DCounts how many words have the prefix as a suffix.
Attempts:
2 left
💡 Hint
Look at the method StartsWith used in the code.
🔧 Debug
advanced
2:00remaining
Identify the Error in Prefix Counting Code
What error will occur when running this code snippet?
DSA C#
using System;
using System.Collections.Generic;

class Program {
    static int CountWordsWithPrefix(List<string> words, string prefix) {
        int count = 0;
        foreach (var word in words) {
            if (word.StartsWith(prefix))
                count++;
            else
                count = count + 1;
        }
        return count;
    }

    static void Main() {
        var words = new List<string> {"cat", "car", "dog"};
        Console.WriteLine(CountWordsWithPrefix(words, "ca"));
    }
}
AThe method throws a NullReferenceException.
BThe method returns the total number of words instead of counting only those with the prefix.
CThe method returns zero always.
DThe method causes a compile-time error due to missing braces.
Attempts:
2 left
💡 Hint
Check what happens in the else block inside the loop.
❓ Predict Output
advanced
1:30remaining
Output with Empty Prefix
What is the output of the following code when counting words with an empty prefix?
DSA C#
using System;
using System.Collections.Generic;

class Program {
    static int CountWordsWithPrefix(List<string> words, string prefix) {
        int count = 0;
        foreach (var word in words) {
            if (word.StartsWith(prefix)) {
                count++;
            }
        }
        return count;
    }

    static void Main() {
        var words = new List<string> {"dog", "deer", "deal"};
        Console.WriteLine(CountWordsWithPrefix(words, ""));
    }
}
A3
B0
CThrows an ArgumentException
D1
Attempts:
2 left
💡 Hint
What does StartsWith return when prefix is empty?
🚀 Application
expert
3:00remaining
Optimizing Prefix Counting for Large Data
You have a large list of words and need to count how many start with a given prefix many times efficiently. Which data structure is best to optimize repeated prefix count queries?
AUse a Stack to store words and pop words starting with prefix.
BUse a HashSet to store words and check prefix counts by iteration.
CUse a Queue to enqueue words and dequeue those with prefix.
DUse a Trie (prefix tree) to store words and count prefixes quickly.
Attempts:
2 left
💡 Hint
Think about a data structure designed for prefix searches.

Practice

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

Solution

  1. Step 1: Understand the problem statement

    The problem asks to count words that begin with a given prefix, not to sort or replace words.
  2. Step 2: Identify the key action

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

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

    Count words with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not other operations [OK]
Common Mistakes:
  • Confusing counting with sorting
  • Thinking the task is to replace prefixes
  • Ignoring the prefix condition
2. Which of the following C# code snippets correctly checks if a string word starts with a prefix prefix?
easy
A. if (word == prefix) { /* do something */ }
B. if (word.Contains(prefix)) { /* do something */ }
C. if (word.EndsWith(prefix)) { /* do something */ }
D. if (word.StartsWith(prefix)) { /* do something */ }

Solution

  1. Step 1: Recall string methods in C#

    The method StartsWith checks if a string begins with a given substring.
  2. Step 2: Compare options

    Contains checks anywhere in the string, EndsWith checks the end, and == checks exact equality.
  3. Final Answer:

    if (word.StartsWith(prefix)) { /* do something */ } -> Option D
  4. Quick Check:

    Use StartsWith to check prefix [OK]
Hint: Use StartsWith method to check prefix in C# strings [OK]
Common Mistakes:
  • Using Contains instead of StartsWith
  • Checking equality instead of prefix
  • Using EndsWith which checks suffix
3. What is the output of the following C# code?
string[] words = {"apple", "app", "application", "banana"};
string prefix = "app";
int count = 0;
foreach (var word in words) {
    if (word.StartsWith(prefix)) {
        count++;
    }
}
Console.WriteLine(count);
medium
A. 2
B. 1
C. 3
D. 4

Solution

  1. Step 1: Check each word against prefix "app"

    Words: "apple" (yes), "app" (yes), "application" (yes), "banana" (no).
  2. Step 2: Count matches

    Three words start with "app", so count = 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Words starting with "app" = 3 [OK]
Hint: Count words starting with prefix by checking each word [OK]
Common Mistakes:
  • Counting only exact matches
  • Including words that contain prefix but don't start with it
  • Off-by-one counting errors
4. The following code is intended to count words starting with a prefix but gives wrong output. What is the error?
string[] words = {"cat", "car", "dog", "cart"};
string prefix = "car";
int count = 0;
for (int i = 0; i <= words.Length; i++) {
    if (words[i].StartsWith(prefix)) {
        count++;
    }
}
Console.WriteLine(count);
medium
A. The loop condition should be i < words.Length instead of <=.
B. The prefix variable is incorrect.
C. The StartsWith method is used wrongly.
D. The count variable should be initialized inside the loop.

Solution

  1. Step 1: Analyze the for loop condition

    The loop runs from i = 0 to i <= words.Length, which causes an index out of range error because valid indices are 0 to words.Length - 1.
  2. Step 2: Correct the loop condition

    Change condition to i < words.Length to avoid accessing invalid index.
  3. Final Answer:

    The loop condition should be i < words.Length instead of <=. -> Option A
  4. Quick Check:

    Loop index must be less than array length [OK]
Hint: Use i < array.Length to avoid index errors in loops [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Reinitializing count inside loop
  • Misusing StartsWith method
5. Given a list of words ["tree", "trie", "trigger", "trip", "try", "matrix"] and prefix "tri", which C# code snippet correctly counts the words starting with the prefix and prints the count?
hard
A. int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count);
B. int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count);
C. int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count);
D. int count = words.Length; Console.WriteLine(count);

Solution

  1. Step 1: Understand the requirement

    We need to count words starting exactly with prefix "tri".
  2. Step 2: Evaluate each option

    int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); uses LINQ Count with StartsWith correctly.
    int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count); uses Contains which counts words containing prefix anywhere.
    int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count); has loop with <= causing index error.
    int count = words.Length; Console.WriteLine(count); counts all words ignoring prefix.
  3. Final Answer:

    int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); -> Option B
  4. Quick Check:

    Use LINQ Count with StartsWith for prefix count [OK]
Hint: Use LINQ Count with StartsWith for concise prefix counting [OK]
Common Mistakes:
  • Using Contains instead of StartsWith
  • Loop index out of range errors
  • Counting all words without prefix check
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