Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the Trie node's children dictionary.
DSA C#
node.Children[[1]] = new TrieNode();Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole word instead of a single character.
Using the index i instead of the character at that index.
✗ Incorrect
We insert the character at index i of the word as the key in the children dictionary.
2fill in blank
mediumComplete the code to check if the Trie node has a child for the current character.
DSA C#
if (!node.Children.ContainsKey([1])) { return new List<string>(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the whole word instead of a character.
Using the node variable instead of a character.
✗ Incorrect
We check if the children dictionary contains the character at index i of the word.
3fill in blank
hardFix the error in the recursive function to collect all words from the Trie node.
DSA C#
foreach (var child in node.Children) { CollectWords(child.Value, prefix + child.Key, [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the prefix string instead of the results list.
Passing the node or child instead of the results list.
✗ Incorrect
We pass the results list to collect words recursively.
4fill in blank
hardFill both blanks to complete the method that returns autocomplete suggestions for a prefix.
DSA C#
TrieNode node = root; for (int i = 0; i < prefix.Length; i++) { if (!node.Children.ContainsKey([1])) return new List<string>(); node = node.Children[[2]]; } return CollectWords(node, prefix, new List<string>());
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole prefix instead of a single character.
Using the index i or node instead of the character.
✗ Incorrect
We use the character at index i of prefix to check and move to the child node.
5fill in blank
hardFill all three blanks to complete the TrieNode class constructor and the Insert method.
DSA C#
public class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsWordEnd = false; public TrieNode() { Children = new Dictionary<char, TrieNode>(); IsWordEnd = [1]; } } 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 = [2]; } public TrieNode root = new TrieNode[3];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting IsWordEnd to true in constructor.
Not marking the end of word as true in Insert.
Not calling the constructor when creating root.
✗ Incorrect
The constructor sets IsWordEnd to false initially. Insert marks the end of a word as true. The root is created with an empty constructor call.
