Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Word Found Using Trie
What is the output of the following Java code that builds a Trie from a list of words and finds the longest word where all prefixes are also words in the dictionary?
DSA Java
import java.util.*; class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isWord = false; } class Trie { TrieNode root = new TrieNode(); public 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.isWord = true; } public String longestWord() { String longest = ""; Queue<TrieNode> queue = new LinkedList<>(); Queue<String> words = new LinkedList<>(); queue.add(root); words.add(""); while (!queue.isEmpty()) { TrieNode node = queue.poll(); String word = words.poll(); for (int i = 25; i >= 0; i--) { TrieNode child = node.children[i]; if (child != null && child.isWord) { queue.add(child); words.add(word + (char)(i + 'a')); if (word.length() + 1 > longest.length() || (word.length() + 1 == longest.length() && (word + (char)(i + 'a')).compareTo(longest) < 0)) { longest = word + (char)(i + 'a'); } } } } return longest; } } public class Main { public static void main(String[] args) { String[] words = {"w","wo","wor","worl","world"}; Trie trie = new Trie(); for (String w : words) { trie.insert(w); } System.out.println(trie.longestWord()); } }
Attempts:
2 left
💡 Hint
Think about the longest word where every prefix is also a word in the list.
✗ Incorrect
The code inserts all words into a Trie and uses BFS to find the longest word such that all prefixes are present. "world" is the longest word with all prefixes "w", "wo", "wor", "worl" also present.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the purpose of the boolean 'isWord' in a Trie node when finding the longest word in a dictionary?
Attempts:
2 left
💡 Hint
Think about how the Trie knows a word ends.
✗ Incorrect
The 'isWord' boolean marks that the path from root to this node forms a complete word in the dictionary.
🔧 Debug
advancedIdentify the Error in Trie Longest Word Search
What error will occur if the BFS loop in the longestWord method uses 'for (int i = 0; i < 26; i++)' instead of 'for (int i = 25; i >= 0; i--)' when trying to find the lexicographically smallest longest word?
Attempts:
2 left
💡 Hint
Consider the order in which children are processed in BFS.
✗ Incorrect
Processing children from 25 down to 0 ensures lexicographically smaller words are found last and updated as longest. Reversing the order returns the largest word instead.
📝 Syntax
advancedIdentify Syntax Error in Trie Insert Method
Which option contains a syntax error in the insert method of the Trie class?
DSA Java
public 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.isWord = true;
}Attempts:
2 left
💡 Hint
Check for missing semicolons.
✗ Incorrect
Option B is missing a semicolon at the end of the statement, causing a syntax error.
🚀 Application
expertLongest Word with All Prefixes Present - Result Count
Given the words array {"a", "banana", "app", "appl", "ap", "apply", "apple"}, how many words in this list can be considered the longest word(s) where all prefixes are also present in the dictionary?
Attempts:
2 left
💡 Hint
Check which words have all prefixes present.
✗ Incorrect
Both "apple" and "apply" meet the condition that all their prefixes ("a", "ap", "app", "appl") are present. "banana" does not have prefixes "b", "ba", etc. So two words qualify.
