Challenge - 5 Problems
Trie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting words into a Trie
What is the output of the following Java code that inserts words into a Trie and prints the Trie structure?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (node.children[index] == null) { node.children[index] = new TrieNode(); } node = node.children[index]; } node.isEndOfWord = true; } void printTrie(TrieNode node, String prefix) { if (node.isEndOfWord) { System.out.println(prefix); } for (int i = 0; i < 26; i++) { if (node.children[i] != null) { printTrie(node.children[i], prefix + (char)(i + 'a')); } } } public static void main(String[] args) { Trie trie = new Trie(); trie.insert("cat"); trie.insert("car"); trie.insert("dog"); trie.printTrie(trie.root, ""); } }
Attempts:
2 left
💡 Hint
Think about how the printTrie method traverses children in alphabetical order.
✗ Incorrect
The printTrie method recursively prints words in alphabetical order because it iterates children from index 0 to 25. 'car' comes before 'cat' alphabetically, so it prints 'car' first, then 'cat', then 'dog'.
❓ Predict Output
intermediateOutput after inserting overlapping prefixes in Trie
What is the output of this Java code after inserting words with overlapping prefixes into a Trie and printing all stored words?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (node.children[index] == null) { node.children[index] = new TrieNode(); } node = node.children[index]; } node.isEndOfWord = true; } void printTrie(TrieNode node, String prefix) { if (node.isEndOfWord) { System.out.println(prefix); } for (int i = 0; i < 26; i++) { if (node.children[i] != null) { printTrie(node.children[i], prefix + (char)(i + 'a')); } } } public static void main(String[] args) { Trie trie = new Trie(); trie.insert("app"); trie.insert("apple"); trie.insert("application"); trie.printTrie(trie.root, ""); } }
Attempts:
2 left
💡 Hint
Words are printed in alphabetical order due to child traversal from 'a' to 'z'.
✗ Incorrect
The Trie stores 'app', 'apple', and 'application'. The printTrie method prints words in alphabetical order. 'app' comes before 'apple' and 'application', so it prints in that order.
🔧 Debug
advancedIdentify the error in Trie insert method
What error will this Java code produce when inserting a word into the Trie?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (node.children[index] == null) { node.children[index] = new TrieNode(); } node = node.children[index]; } node.isEndOfWord = true; } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for accessing characters in the string.
✗ Incorrect
The loop uses i <= word.length(), which tries to access word.charAt(word.length()), causing StringIndexOutOfBoundsException because valid indices are 0 to length-1.
❓ Predict Output
advancedOutput after inserting words with shared prefixes and printing
What is the output of this Java code after inserting words with shared prefixes and printing all words stored in the Trie?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (node.children[index] == null) { node.children[index] = new TrieNode(); } node = node.children[index]; } node.isEndOfWord = true; } void printTrie(TrieNode node, String prefix) { if (node.isEndOfWord) { System.out.println(prefix); } for (int i = 0; i < 26; i++) { if (node.children[i] != null) { printTrie(node.children[i], prefix + (char)(i + 'a')); } } } public static void main(String[] args) { Trie trie = new Trie(); trie.insert("bat"); trie.insert("bath"); trie.insert("batman"); trie.insert("batmobile"); trie.printTrie(trie.root, ""); } }
Attempts:
2 left
💡 Hint
Words are printed in alphabetical order due to child traversal from 'a' to 'z'.
✗ Incorrect
The printTrie method prints words in alphabetical order. 'bat' comes before 'bath', which comes before 'batman' and 'batmobile'.
🧠 Conceptual
expertNumber of nodes after inserting words into Trie
If you insert the words ["go", "gone", "guild", "guilt"] into an empty Trie, how many Trie nodes will be created in total (including the root node)?
Attempts:
2 left
💡 Hint
Count unique characters in the paths, including shared prefixes, plus the root node.
✗ Incorrect
Root node counts as 1. The words share prefix 'g'. The unique nodes created are: root(1), 'g'(2), 'o'(3), 'n'(4), 'e'(5), 'u'(6), 'i'(7), 'l'(8), 'd'(9), 't'(10). Total nodes = 10.
