Challenge - 5 Problems
Trie Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Searching a Word in Trie
What is the output of the following Java code that inserts words into a Trie and searches for a specific word?
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; } boolean search(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (node.children[index] == null) { return false; } node = node.children[index]; } return node.isEndOfWord; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("apple"); trie.insert("app"); System.out.println(trie.search("app")); } }
Attempts:
2 left
💡 Hint
Think about whether the word 'app' was inserted and if the search checks for complete words.
✗ Incorrect
The word 'app' was inserted into the Trie, so searching for 'app' returns true because the node for 'p' marks the end of a word.
❓ Predict Output
intermediateSearch Result for Non-Inserted Word
What will be the output when searching for a word that was not inserted 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 (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; } boolean search(String word) { TrieNode node = root; for (char c : word.toCharArray()) { int index = c - 'a'; if (node.children[index] == null) { return false; } node = node.children[index]; } return node.isEndOfWord; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("apple"); trie.insert("app"); System.out.println(trie.search("apples")); } }
Attempts:
2 left
💡 Hint
Check if the word 'apples' was inserted or if the Trie contains nodes for all its letters.
✗ Incorrect
The word 'apples' was not inserted, so the search returns false because the Trie does not have a node for 's' after 'apple'.
🔧 Debug
advancedIdentify the Bug in Trie Search Method
What error will occur when running this Trie search method code?
DSA Java
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord = false; } class Trie { TrieNode root = new TrieNode(); boolean search(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (node.children[index] == null) { return false; } node = node.children[index]; } return node.isEndOfWord; } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition in the for loop.
✗ Incorrect
The loop runs from i = 0 to i <= word.length(), which causes an index out of bounds when accessing word.charAt(i) at i == word.length().
🧠 Conceptual
advancedUnderstanding Trie Search Behavior
If a Trie contains the words "cat" and "cater", what will the search method return when searching for "cate"?
Attempts:
2 left
💡 Hint
Consider if "cate" is marked as a complete word in the Trie.
✗ Incorrect
The search method returns false because "cate" is a prefix but not a complete word inserted in the Trie.
🚀 Application
expertResult of Searching After Multiple Insertions
After inserting the words "dog", "door", "dorm", and "dot" into a Trie, what will be the output of searching for "dor"?
Attempts:
2 left
💡 Hint
Check if "dor" was inserted or marked as a complete word.
✗ Incorrect
The search returns false because "dor" is a prefix but not a complete word in the Trie.
