Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new TrieNode.
DSA Java
class TrieNode { boolean isWord; TrieNode[] children = new TrieNode[26]; public TrieNode() { isWord = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isWord to true by default, which incorrectly marks all nodes as word ends.
✗ Incorrect
When creating a new TrieNode, isWord should be false initially because no word ends at this node yet.
2fill in blank
mediumComplete the code to insert a character into the Trie children array.
DSA Java
public void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[[1]] == null) {
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isWord = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the character c directly as an index, which causes errors.
Using the node or word variable incorrectly as an index.
✗ Incorrect
We use the integer index calculated from the character to access the correct child node.
3fill in blank
hardFix the error in the search method to check if a word exists in the Trie.
DSA Java
public boolean search(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
return [1];
}
node = node.children[index];
}
return node.isWord;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true when the path is missing, causing false positives.
Returning null or checking node == null incorrectly.
✗ Incorrect
If a character path is missing, the word does not exist, so return false.
4fill in blank
hardFill both blanks to implement a method that checks if any word starts with a given prefix.
DSA Java
public boolean startsWith(String prefix) {
TrieNode node = root;
for (char c : prefix.toCharArray()) {
int index = c - 'a';
if (node.children[[1]] == null) {
return [2];
}
node = node.children[index];
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using character c directly as index.
Returning true when the path is missing.
✗ Incorrect
We check the child at index and return false if missing, otherwise true if prefix exists.
5fill in blank
hardFill all three blanks to implement a method that deletes a word from the Trie recursively.
DSA Java
private boolean delete(TrieNode node, String word, int depth) {
if (node == null) {
return [1];
}
if (depth == word.length()) {
if (node.isWord) {
node.isWord = false;
return nodeHasNoChildren(node);
}
return false;
}
int index = word.charAt(depth) - 'a';
boolean shouldDeleteCurrentNode = delete(node.children[[2]], word, depth + 1);
if (shouldDeleteCurrentNode) {
node.children[[3]] = null;
return !node.isWord && nodeHasNoChildren(node);
}
return false;
}
private boolean nodeHasNoChildren(TrieNode node) {
for (TrieNode child : node.children) {
if (child != null) {
return false;
}
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true when node is null, causing incorrect deletion.
Using depth instead of index to access children.
Not setting the child to null after deletion.
✗ Incorrect
Return false if node is null. Use index to access children for recursive delete and null assignment.
