Challenge - 5 Problems
Trie Autocomplete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Insertion and Search
What is the output of the following Java code that inserts words into a Trie and searches for a 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"); System.out.println(trie.startsWith("app")); System.out.println(trie.startsWith("apl")); } }
Attempts:
2 left
💡 Hint
Check if the prefix exists in the Trie after inserting the words.
✗ Incorrect
The prefix "app" exists because both "app" and "apple" were inserted. The prefix "apl" does not exist in the Trie, so startsWith returns false.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the structure of a Trie node used in an autocomplete system?
Attempts:
2 left
💡 Hint
Think about how each character leads to the next in a Trie.
✗ Incorrect
Each Trie node typically has an array (or map) of child nodes for each possible character and a boolean flag to mark if a word ends at that node.
🔧 Debug
advancedIdentify the Error in Trie Search Method
What error will the following Java method cause when searching for a word in a Trie?
DSA Java
boolean search(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
if (node.children[idx] == null) return false;
node = node.children[idx];
}
return node.isEnd;
}Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop uses i <= word.length(), which causes an index out of bounds when i equals word.length().
🚀 Application
advancedAutocomplete Suggestions Using Trie
Given a Trie with inserted words, which method correctly collects all words starting with a given prefix?
DSA Java
void collectWords(TrieNode node, String prefix, List<String> results) {
if (node.isEnd) results.add(prefix);
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
collectWords(node.children[i], prefix + (char)(i + 'a'), results);
}
}
}Attempts:
2 left
💡 Hint
Think about how to explore all possible continuations of the prefix.
✗ Incorrect
The method recursively explores all children nodes, adding words when the end of a word is reached.
❓ Predict Output
expertOutput of Autocomplete System with Multiple Insertions
What is the output of the following Java code that inserts words and prints autocomplete suggestions for prefix "ca"?
DSA Java
import java.util.*; 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; } void collectWords(TrieNode node, String prefix, List<String> results) { if (node.isEnd) results.add(prefix); for (int i = 0; i < 26; i++) { if (node.children[i] != null) { collectWords(node.children[i], prefix + (char)(i + 'a'), results); } } } List<String> autocomplete(String prefix) { TrieNode node = root; for (char c : prefix.toCharArray()) { int idx = c - 'a'; if (node.children[idx] == null) return new ArrayList<>(); node = node.children[idx]; } List<String> results = new ArrayList<>(); collectWords(node, prefix, results); return results; } } 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"); List<String> suggestions = trie.autocomplete("ca"); System.out.println(suggestions); } }
Attempts:
2 left
💡 Hint
Check the order in which children are traversed in collectWords.
✗ Incorrect
The collectWords method traverses children from index 0 to 25, which corresponds to 'a' to 'z'. So 'car' (c-a-r) is found before 'cart' (c-a-r-t) and 'cat' (c-a-t) because 'r' < 't' in ASCII. The correct lex order is 'car', 'cart', 'cat'.
