Challenge - 5 Problems
Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prefix count after adding words
What is the output of the following Java code that counts words with a given prefix?
DSA Java
import java.util.*; class PrefixCounter { private List<String> words = new ArrayList<>(); public void addWord(String word) { words.add(word); } public int countWordsStartingWith(String prefix) { int count = 0; for (String w : words) { if (w.startsWith(prefix)) { count++; } } return count; } } public class Main { public static void main(String[] args) { PrefixCounter pc = new PrefixCounter(); pc.addWord("apple"); pc.addWord("app"); pc.addWord("application"); pc.addWord("banana"); pc.addWord("apply"); System.out.println(pc.countWordsStartingWith("app")); } }
Attempts:
2 left
💡 Hint
Count all words that start exactly with "app".
✗ Incorrect
The words starting with "app" are "apple", "app", "application", and "apply". So the count is 4.
🧠 Conceptual
intermediateUnderstanding prefix counting complexity
If you have N words each of average length M, what is the time complexity of counting words starting with a prefix of length P using a simple list and startsWith check?
Attempts:
2 left
💡 Hint
Consider that startsWith compares characters up to prefix length P for each word.
✗ Incorrect
For each of the N words, startsWith compares up to P characters, so total time is O(N * P).
🔧 Debug
advancedIdentify the error in prefix counting code
What error will this Java code produce when counting words with a prefix?
DSA Java
import java.util.*; class PrefixCounter { private List<String> words = new ArrayList<>(); public void addWord(String word) { words.add(word); } public int countWordsStartingWith(String prefix) { int count = 0; for (String w : words) { if (w.length() >= prefix.length() && w.substring(0, prefix.length()) == prefix) { count++; } } return count; } } public class Main { public static void main(String[] args) { PrefixCounter pc = new PrefixCounter(); pc.addWord("apple"); pc.addWord("app"); System.out.println(pc.countWordsStartingWith("app")); } }
Attempts:
2 left
💡 Hint
Java strings: use equals() for content comparison, == for references.
✗ Incorrect
Prints 0 because string comparison uses '==' instead of equals().
🚀 Application
advancedChoosing data structure for efficient prefix counting
Which data structure is best to efficiently count words starting with a given prefix among many words?
Attempts:
2 left
💡 Hint
Think about prefix-based search speed.
✗ Incorrect
Trie stores words by prefixes allowing fast prefix counting in O(P) time, better than linear search.
❓ Predict Output
expertOutput of prefix count using Trie implementation
What is the output of this Java code that uses a Trie to count words with prefix "app"?
DSA Java
import java.util.*; class TrieNode { Map<Character, TrieNode> children = new HashMap<>(); int count = 0; } class Trie { private TrieNode root = new TrieNode(); public void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { node.children.putIfAbsent(c, new TrieNode()); node = node.children.get(c); node.count++; } } public int countWordsStartingWith(String prefix) { TrieNode node = root; for (char c : prefix.toCharArray()) { if (!node.children.containsKey(c)) { return 0; } node = node.children.get(c); } return node.count; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("apple"); trie.insert("app"); trie.insert("application"); trie.insert("banana"); trie.insert("apply"); System.out.println(trie.countWordsStartingWith("app")); } }
Attempts:
2 left
💡 Hint
Count increments at each node for inserted words with prefix.
✗ Incorrect
Each inserted word increments count at nodes for its prefix. The prefix "app" node count is 4.
