Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the Trie.
DSA C#
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c)) {
node.Children.Add(c, new TrieNode());
}
node = node[1];
}
node.IsWord = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Children.Add again instead of accessing the child node.
Checking ContainsKey instead of accessing the child node.
✗ Incorrect
We move to the child node corresponding to character c by accessing node.Children[c].
2fill in blank
mediumComplete the code to check if a word exists in the Trie.
DSA C#
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[1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true without checking if the node is a word end.
Trying to access Children[c] after the loop.
✗ Incorrect
After traversing all characters, we check if the node marks the end of a valid word using IsWord.
3fill in blank
hardFix the error in the DFS method to find the longest word built one character at a time.
DSA C#
private void DFS(TrieNode node, string currentWord) {
if (node == null) return;
if (currentWord.Length > longestWord.Length) {
longestWord = currentWord;
}
foreach (var pair in node.Children) {
if (pair.Value[1]) {
DFS(pair.Value, currentWord + pair.Key);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking Children.ContainsKey instead of IsWord.
Not checking if the child node is a word before DFS.
✗ Incorrect
We only continue DFS if the child node marks the end of a valid word, so we check IsWord.
4fill in blank
hardFill both blanks to build the dictionary comprehension that filters words with all prefixes present.
DSA C#
var validWords = words.Where(word => word.All(c => trie.Search(word[1]))).ToList();Attempts:
3 left
💡 Hint
Common Mistakes
Using Substring with wrong indices causing empty or wrong prefixes.
Not including the current character in the prefix.
✗ Incorrect
We check if all prefixes from start to current character index + 1 exist in the trie using Substring(0, index+1).
5fill in blank
hardFill all three blanks to complete the method that finds the longest word using DFS on the Trie.
DSA C#
public string LongestWord(string[] words) {
foreach (var word in words) {
trie[1](word);
}
longestWord = "";
DFS(trie[2], "");
return longestWord;
}
private void DFS(TrieNode node, string currentWord) {
if (node == null) return;
if (currentWord.Length > longestWord.Length || (currentWord.Length == longestWord.Length && string.Compare(currentWord, longestWord) < 0)) {
longestWord = currentWord;
}
foreach (var pair in node.Children.OrderBy(p => p.Key)) {
if (pair.Value[3]) {
DFS(pair.Value, currentWord + pair.Key);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling Search instead of Insert when adding words.
Starting DFS from a wrong node.
Not checking IsWord before DFS recursion.
✗ Incorrect
We insert each word into the trie, start DFS from root, and continue DFS only if child node IsWord is true.
