Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Word Search
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 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("apple"); trie.insert("app"); trie.insert("application"); System.out.println(trie.search("app")); System.out.println(trie.search("appl")); } }
Attempts:
2 left
💡 Hint
Remember that search returns true only if the word is fully inserted and marked as an end.
✗ Incorrect
The word "app" was inserted and marked as an end, so search("app") returns true. The prefix "appl" is not marked as an end, so search("appl") returns false.
🧠 Conceptual
intermediateTrie Node Children Count
After inserting the words ["cat", "car", "cart", "dog"] into an empty Trie, how many children does the root node have?
Attempts:
2 left
💡 Hint
Count distinct first letters of inserted words.
✗ Incorrect
Words start with 'c' and 'd', so root has children at indices for 'c' and 'd', total 2 children.
🔧 Debug
advancedIdentify the Error in Trie Search
What error will the following Java code produce when searching for a word not inserted in the Trie?
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'; node = node.children[idx]; } return node != null && node.isEnd; } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("hello"); System.out.println(trie.search("hey")); } }
Attempts:
2 left
💡 Hint
Check if the code verifies children before accessing.
✗ Incorrect
The search method does not check if node.children[idx] is null before assigning node = node.children[idx]. For the word "hey", the path diverges at the second character ('e'), and at the third character ('y'), node.children[idx] is null, so node becomes null and the next iteration or access causes a NullPointerException.
📝 Syntax
advancedCorrect Syntax for Trie Insert Method
Which option shows the correct syntax for inserting a word into a Trie in Java?
Attempts:
2 left
💡 Hint
Java uses for-each with colon, not 'in'. Also check condition for creating new nodes.
✗ Incorrect
Option A uses correct for-each syntax and creates new TrieNode only if child is null. Option A uses invalid 'in' keyword. Option A is valid syntax but uses index-based loop (also correct but not the preferred here). Option A incorrectly creates new node if child is NOT null.
🚀 Application
expertNumber of Words with Given Prefix in Trie
Given a Trie with words inserted: ["test", "team", "teach", "tent", "toast"], what is the number of words in the Trie that start with the prefix "te"?
Attempts:
2 left
💡 Hint
Count all words starting with 'te' from the list.
✗ Incorrect
Words starting with 'te' are "test", "team", "teach", and "tent" — total 4 words.
