Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the children array of a Trie node.
DSA Java
node.children[[1] - 'a'] = new TrieNode();
Attempts:
3 left
💡 Hint
Common Mistakes
Using loop variable 'i' instead of character 'ch'.
Using an undefined variable 'index'.
✗ Incorrect
The variable 'ch' represents the current character to insert, so subtracting 'a' gives the correct index.
2fill in blank
mediumComplete the code to check if a prefix exists in the Trie.
DSA Java
if (node.children[[1] - 'a'] == null) return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using loop index 'i' instead of character 'ch'.
Using an undefined variable 'index'.
✗ Incorrect
We check the child node corresponding to the character 'ch' to see if the prefix path exists.
3fill in blank
hardFix the error in the method to return true if the prefix is found.
DSA Java
return node.[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'isWord' which indicates a complete word, not prefix.
Using undefined flags like 'isEnd' or 'endOfWord'.
✗ Incorrect
The method should return true if the prefix exists, so it returns the 'isWord' flag.
4fill in blank
hardFill both blanks to complete the loop that traverses the Trie for each character in the prefix.
DSA Java
for (char [1] : prefix.toCharArray()) { if (node.children[[2] - 'a'] == null) return false; node = node.children[[2] - 'a']; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and indexing.
Using loop index 'i' which is not defined in this loop.
✗ Incorrect
The loop variable 'ch' is used to access children nodes by subtracting 'a' to get the index.
5fill in blank
hardFill all three blanks to complete the method that inserts a word into the Trie.
DSA Java
for (int [1] = 0; [2] < word.length(); [3]++) { char ch = word.charAt(i); if (node.children[ch - 'a'] == null) { node.children[ch - 'a'] = new TrieNode(); } node = node.children[ch - 'a']; } node.isWord = true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts causing errors.
Using undefined variables like 'index' or 'j' inconsistently.
✗ Incorrect
The loop variable 'i' is used consistently for initialization, condition, and increment.
