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 C# code that builds a Trie from a list of words and finds the longest word where every prefix is also a word in the list?
DSA C#
using System; using System.Collections.Generic; class TrieNode { public TrieNode[] children = new TrieNode[26]; public bool isWord = false; } class Trie { private TrieNode root = new TrieNode(); public void Insert(string word) { TrieNode node = root; foreach (char c in word) { 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 result = ""; DFS(root, "", ref result); return result; } private void DFS(TrieNode node, string path, ref string result) { if (node == null) return; if (path.Length > result.Length || (path.Length == result.Length && string.Compare(path, result) < 0)) { result = path; } for (int i = 0; i < 26; i++) { if (node.children[i] != null && node.children[i].isWord) { DFS(node.children[i], path + (char)(i + 'a'), ref result); } } } } class Program { static void Main() { string[] words = {"a", "app", "ap", "apply", "apple", "appl"}; Trie trie = new Trie(); foreach (string w in words) { trie.Insert(w); } Console.WriteLine(trie.LongestWord()); } }
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list and compare their lengths.
✗ Incorrect
The code inserts all words into the Trie and then uses DFS to find the longest word where every prefix is also a word. "apple" is the longest such word because all prefixes "a", "ap", "app", "appl" are present and marked as words.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
In the context of implementing a Trie for the longest word problem, what does the boolean field 'isWord' in each Trie node represent?
Attempts:
2 left
💡 Hint
Think about how the Trie distinguishes complete words from prefixes.
✗ Incorrect
The 'isWord' boolean marks if the path from root to this node forms a complete word inserted in the Trie. This helps to verify if prefixes are valid words.
🔧 Debug
advancedIdentify the Bug in Trie DFS Method
What error will occur when running this DFS method in the Trie class to find the longest word, and why?
private void DFS(TrieNode node, string path, ref string result) {
if (node == null) return;
if (path.Length > result.Length || (path.Length == result.Length && string.Compare(path, result) < 0)) {
result = path;
}
for (int i = 0; i <= 26; i++) {
if (node.children[i] != null && node.children[i].isWord) {
DFS(node.children[i], path + (char)(i + 'a'), ref result);
}
}
}
Attempts:
2 left
💡 Hint
Check the loop boundary condition for array indexing.
✗ Incorrect
The children array has size 26 indexed 0 to 25. Looping with i <= 26 tries to access index 26, causing IndexOutOfRangeException.
🚀 Application
advancedLongest Word with All Prefixes Present
Given the list of words ["w", "wo", "wor", "worl", "world", "banana", "ban", "bana", "banan"], which word will the Trie-based algorithm return as the longest word where every prefix is also a word?
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list.
✗ Incorrect
"world" is the longest word where all prefixes "w", "wo", "wor", "worl" are present. "banana" is missing some prefixes like "banan" is present but "banana" itself is not fully validated by prefixes.
🧠 Conceptual
expertWhy Use DFS in Trie for Longest Word?
Why is Depth-First Search (DFS) the preferred method to find the longest word in a Trie where every prefix is also a word?
Attempts:
2 left
💡 Hint
Consider how DFS explores paths and checks conditions along the way.
✗ Incorrect
DFS explores each branch fully, so it can check if all prefixes are valid words before updating the longest word found. BFS would require additional checks and bookkeeping.
