Challenge - 5 Problems
Trie Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Search for Existing Word
What is the output of the following C# code that inserts words into a Trie and searches for the word "cat"?
DSA C#
using System; using System.Collections.Generic; class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsEndOfWord = 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.IsEndOfWord = 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.IsEndOfWord; } } class Program { static void Main() { Trie trie = new Trie(); trie.Insert("cat"); trie.Insert("car"); trie.Insert("dog"); Console.WriteLine(trie.Search("cat")); } }
Attempts:
2 left
💡 Hint
Check if the word "cat" was inserted before searching.
✗ Incorrect
The word "cat" was inserted into the Trie, so searching for it returns true.
❓ Predict Output
intermediateOutput of Trie Search for Non-Existing Word
What is the output of the following C# code that inserts words into a Trie and searches for the word "bat"?
DSA C#
using System; using System.Collections.Generic; class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsEndOfWord = 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.IsEndOfWord = 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.IsEndOfWord; } } class Program { static void Main() { Trie trie = new Trie(); trie.Insert("cat"); trie.Insert("car"); trie.Insert("dog"); Console.WriteLine(trie.Search("bat")); } }
Attempts:
2 left
💡 Hint
Check if the word "bat" was inserted before searching.
✗ Incorrect
The word "bat" was not inserted into the Trie, so searching for it returns false.
🧠 Conceptual
advancedUnderstanding Trie Search Behavior
In a Trie, what does it mean if the search for a word returns true?
Attempts:
2 left
💡 Hint
Think about the meaning of the end-of-word marker in Trie nodes.
✗ Incorrect
A search returns true only if the path for the word exists and the last node is marked as the end of a word.
❓ Predict Output
advancedOutput of Trie Search for Prefix Only Word
What is the output of the following C# code that inserts "car" and searches for "ca"?
DSA C#
using System; using System.Collections.Generic; class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsEndOfWord = 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.IsEndOfWord = 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.IsEndOfWord; } } class Program { static void Main() { Trie trie = new Trie(); trie.Insert("car"); Console.WriteLine(trie.Search("ca")); } }
Attempts:
2 left
💡 Hint
Check if "ca" is marked as a complete word in the Trie.
✗ Incorrect
The search returns false because "ca" is only a prefix and not marked as a complete word.
🧠 Conceptual
expertWhy Trie Search is Efficient for Prefix Queries
Why is searching for a prefix in a Trie generally faster than searching in a list of strings?
Attempts:
2 left
💡 Hint
Think about how Trie nodes represent shared parts of words.
✗ Incorrect
Trie nodes represent shared prefixes, so searching a prefix only traverses nodes once, avoiding repeated checks on each word.
