Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a child nodes array for a Trie node.
DSA C#
public TrieNode() {
children = new TrieNode[[1]];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 52 for uppercase and lowercase combined when only lowercase is needed.
Using 10 which is for digits, not letters.
✗ Incorrect
A Trie node for lowercase English letters uses an array of size 26 for children nodes.
2fill in blank
mediumComplete the code to initialize the end-of-word flag in the Trie node constructor.
DSA C#
public TrieNode() {
children = new TrieNode[26];
[1] = false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'isWord' which are less descriptive.
Using names that do not clearly indicate the purpose.
✗ Incorrect
The common name for the flag marking the end of a word is 'isEndOfWord'.
3fill in blank
hardFix the error in the TrieNode constructor by completing the initialization of all children to null.
DSA C#
public TrieNode() {
children = new TrieNode[26];
isEndOfWord = false;
for (int i = 0; i < 26; i++) {
children[[1]] = null;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name inside the array index.
Trying to assign null to the whole array instead of each element.
✗ Incorrect
The loop index variable 'i' is used to access each child node to set it to null.
4fill in blank
hardFill both blanks to complete the method that checks if a Trie node has any children.
DSA C#
public bool HasChildren() {
for (int [1] = 0; [1] < 26; [1]++) {
if (children[[2]] != null) {
return true;
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and array index causing errors.
Using variables not declared in the loop.
✗ Incorrect
The same loop variable 'i' is used for iteration and indexing children array.
5fill in blank
hardFill all three blanks to complete the Insert method for adding a word to the Trie.
DSA C#
public void Insert(string word) {
TrieNode node = this;
foreach (char [1] in word) {
int index = [2] - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
node = node.children[[3]];
}
node.isEndOfWord = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the character variable directly in the array index instead of 'index'.
Using reserved words or types as variable names.
✗ Incorrect
Use the character variable 'c' in the foreach loop and index calculation, and 'index' for array access.
