Challenge - 5 Problems
Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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")); } }
Attempts:
2 left
💡 Hint
Count how many words start exactly with "app".
✗ Incorrect
The words starting with "app" are: "apple", "app", "application", and "apply". That makes 4 words.
🧠 Conceptual
intermediateUnderstanding Prefix Matching
Which of the following best describes what the method CountWordsWithPrefix does?
Attempts:
2 left
💡 Hint
Look at the method StartsWith used in the code.
✗ Incorrect
The method uses StartsWith which checks if the word begins with the prefix, so it counts words starting exactly with the prefix.
🔧 Debug
advancedIdentify 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")); } }
Attempts:
2 left
💡 Hint
Check what happens in the else block inside the loop.
✗ Incorrect
The else block increments count for words that do not start with the prefix, so count ends up being the total number of words.
❓ Predict Output
advancedOutput 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, "")); } }
Attempts:
2 left
💡 Hint
What does StartsWith return when prefix is empty?
✗ Incorrect
In C#, StartsWith with an empty string returns true for every word, so all words are counted.
🚀 Application
expertOptimizing 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?
Attempts:
2 left
💡 Hint
Think about a data structure designed for prefix searches.
✗ Incorrect
A Trie stores words by their prefixes, allowing fast prefix count queries without scanning all words each time.
