Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the Trie node's children.
DSA Java
node.children[[1] - 'a'] = new TrieNode();
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer index instead of the character variable.
Using a variable name that is not defined in the loop.
✗ Incorrect
The variable 'ch' represents the current character being inserted, so we subtract 'a' to get the index.
2fill in blank
mediumComplete the code to check if the current node marks the end of a word.
DSA Java
if (node.[1]) { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not exist in the TrieNode class.
Confusing the flag with the children array.
✗ Incorrect
The boolean flag 'isEnd' is commonly used to mark the end of a word in a Trie node.
3fill in blank
hardFix the error in the search method to correctly traverse the Trie.
DSA Java
node = node.children[[1] - 'a'];
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 instead of the loop variable.
Using an index out of bounds like i+1 or i-1.
✗ Incorrect
We use word.charAt(i) to get the current character at index i during traversal.
4fill in blank
hardFill both blanks to add a word to the Trie and mark its end.
DSA Java
for (char [1] : word.toCharArray()) { if (node.children[[2] - 'a'] == null) { node.children[[2] - 'a'] = new TrieNode(); } node = node.children[[2] - 'a']; } node.isEnd = true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for loop and indexing.
Using a variable that is not a character.
✗ Incorrect
The variable 'ch' is used to iterate over characters and index children accordingly.
5fill in blank
hardFill all three blanks to implement the autocomplete helper method that collects words.
DSA Java
private void [1](TrieNode node, String prefix, List<String> results) { if (node.[2]) { results.add(prefix); } for (int i = 0; i < 26; i++) { if (node.children[i] != null) { char [3] = (char) (i + 'a'); [1](node.children[i], prefix + [3], results); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like 'search' instead of 'dfs'.
Using incorrect boolean flag name.
Using a variable name other than 'ch' for characters.
✗ Incorrect
The method is named 'dfs' for depth-first search, checks 'isEnd' flag, and uses 'ch' as the character variable.
