Challenge - 5 Problems
Prefix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prefix Search Result
What is the output of the following Java code that inserts words into a Trie and searches for words with a given prefix?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEnd = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int idx = c - 'a'; if (node.children[idx] == null) { node.children[idx] = new TrieNode(); } node = node.children[idx]; } node.isEnd = true; } boolean startsWith(String prefix) { TrieNode node = root; for (char c : prefix.toCharArray()) { int idx = c - 'a'; if (node.children[idx] == null) return false; node = node.children[idx]; } return true; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("apple"); trie.insert("app"); trie.insert("bat"); trie.insert("bath"); System.out.println(trie.startsWith("app")); System.out.println(trie.startsWith("bat")); System.out.println(trie.startsWith("bath")); System.out.println(trie.startsWith("batman")); } }
Attempts:
2 left
💡 Hint
Check if the prefix exists by traversing the Trie nodes for each character.
✗ Incorrect
The method startsWith returns true if the prefix exists in the Trie. The prefixes "app", "bat", and "bath" exist, but "batman" does not.
🧠 Conceptual
intermediateNumber of Children in Trie Node
In a Trie node designed for lowercase English letters, how many children pointers does each node have?
Attempts:
2 left
💡 Hint
Think about the number of lowercase English alphabets.
✗ Incorrect
Each Trie node has 26 children pointers, one for each lowercase English letter from 'a' to 'z'.
🔧 Debug
advancedIdentify the Bug in Trie Insert Method
What error will occur when running the following insert method in a Trie if the input word contains uppercase letters?
DSA Java
void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int idx = c - 'a';
if (node.children[idx] == null) {
node.children[idx] = new TrieNode();
}
node = node.children[idx];
}
node.isEnd = true;
}Attempts:
2 left
💡 Hint
Check how the index is calculated for uppercase letters.
✗ Incorrect
Uppercase letters have ASCII values less than 'a', so subtracting 'a' results in a negative index, causing ArrayIndexOutOfBoundsException.
🚀 Application
advancedCount Words with Given Prefix Using Trie
Given a Trie with inserted words, which approach correctly counts how many words start with a given prefix?
Attempts:
2 left
💡 Hint
Think about how to find all words starting with the prefix.
✗ Incorrect
To count words with a prefix, first find the node representing the prefix, then count all complete words in its subtree.
❓ Predict Output
expertOutput of Trie Search After Multiple Inserts
What is the output of the following Java code that inserts words and searches for prefixes?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEnd = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int idx = c - 'a'; if (node.children[idx] == null) { node.children[idx] = new TrieNode(); } node = node.children[idx]; } node.isEnd = true; } boolean search(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int idx = c - 'a'; if (node.children[idx] == null) return false; node = node.children[idx]; } return node.isEnd; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("cat"); trie.insert("car"); trie.insert("cart"); trie.insert("dog"); System.out.println(trie.search("car")); System.out.println(trie.search("cart")); System.out.println(trie.search("cat")); System.out.println(trie.search("ca")); System.out.println(trie.search("dog")); System.out.println(trie.search("do")); } }
Attempts:
2 left
💡 Hint
Check if the search method returns true only for complete words inserted.
✗ Incorrect
The search method returns true only if the word is fully present and marked as an end. "ca" and "do" are prefixes but not complete words, so false.
