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 C# code that inserts words into a Trie and searches for a specific word?
DSA C#
using System; using System.Collections.Generic; class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsWordEnd = false; } class Trie { private TrieNode root = new TrieNode(); public void Insert(string word) { TrieNode node = root; foreach (char c in word) { if (!node.Children.ContainsKey(c)) { node.Children[c] = new TrieNode(); } node = node.Children[c]; } node.IsWordEnd = true; } public bool Search(string word) { TrieNode node = root; foreach (char c in word) { if (!node.Children.ContainsKey(c)) { return false; } node = node.Children[c]; } return node.IsWordEnd; } } class Program { static void Main() { Trie trie = new Trie(); trie.Insert("apple"); trie.Insert("app"); trie.Insert("application"); Console.WriteLine(trie.Search("app")); Console.WriteLine(trie.Search("appl")); Console.WriteLine(trie.Search("apple")); } }
Attempts:
2 left
💡 Hint
Remember that Search returns true only if the word ends exactly at a node marked as word end.
✗ Incorrect
The word "app" is inserted and marked as a word end, so Search("app") returns true. "appl" is a prefix but not a word end, so Search("appl") returns false. "apple" is inserted and marked as word end, so Search("apple") returns true.
❓ Predict Output
intermediateTrie Search Result for Non-Inserted Word
Given the following Trie insertions, what will be the output of searching for the word "bat"?
DSA C#
Trie trie = new Trie(); trie.Insert("batman"); trie.Insert("batch"); trie.Insert("bath"); Console.WriteLine(trie.Search("bat"));
Attempts:
2 left
💡 Hint
Check if "bat" was inserted as a complete word or only as a prefix.
✗ Incorrect
"bat" is a prefix of inserted words but was never inserted as a complete word, so Search returns false.
🔧 Debug
advancedIdentify the Bug in Trie Search Method
What error will the following Trie Search method cause when searching for a word not present in the Trie?
DSA C#
public bool Search(string word) { TrieNode node = root; foreach (char c in word) { node = node.Children[c]; } return node.IsWordEnd; }
Attempts:
2 left
💡 Hint
What happens if the character key does not exist in the dictionary?
✗ Incorrect
Accessing node.Children[c] without checking if c exists causes a KeyNotFoundException if c is missing.
🧠 Conceptual
advancedTrie Node Memory Usage
Which of the following statements about memory usage in a Trie node is correct?
Attempts:
2 left
💡 Hint
Consider how children are stored to optimize memory for sparse tries.
✗ Incorrect
Using a dictionary for children stores only existing child nodes, saving memory compared to fixed arrays.
🚀 Application
expertLongest Word with All Prefixes in Trie
Given a list of words inserted into a Trie, which approach correctly finds the longest word such that all its prefixes are also present in the Trie?
Attempts:
2 left
💡 Hint
Think about how to verify all prefixes exist efficiently using Trie structure.
✗ Incorrect
DFS on Trie nodes checking word end flags ensures all prefixes exist, allowing to find longest valid word.
