Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the Trie.
DSA Java
public void insert(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
if (node.children[ch - 'a'] == null) {
node.children[ch - 'a'] = new TrieNode();
}
node = node.[1];
}
node.isEnd = true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root instead of moving down the children array.
Trying to access a non-existent 'parent' or 'child' field.
✗ Incorrect
We move to the child node corresponding to the current character by accessing children[ch - 'a'].
2fill in blank
mediumComplete the code to check if a word exists in the Trie.
DSA Java
public boolean search(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
if (node.children[ch - 'a'] == null) {
return false;
}
node = node.[1];
}
return node.isEnd;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root instead of the child node.
Not checking if the child node is null before moving.
✗ Incorrect
To check each character, we move down the children array at index ch - 'a'.
3fill in blank
hardFix the error in the method that finds the longest word in the dictionary using DFS.
DSA Java
private void dfs(TrieNode node, StringBuilder path) {
if (node == null) return;
if (!node.isEnd && node != root) return;
if (path.length() > longestWord.length() ||
(path.length() == longestWord.length() && path.toString().compareTo(longestWord) < 0)) {
longestWord = path.toString();
}
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
path.append((char) (i + 'a'));
dfs(node.[1], path);
path.deleteCharAt(path.length() - 1);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using children[0] which always goes to the first child.
Using a non-existent 'child' field.
✗ Incorrect
We must recurse into the child node at index i, so use children[i].
4fill in blank
hardFill both blanks to complete the TrieNode class and initialize the root.
DSA Java
class TrieNode { boolean isEnd; TrieNode[] [1]; public TrieNode() { isEnd = false; [2] = new TrieNode[26]; } } TrieNode root = new [1]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the array in declaration and initialization.
Using 'root' as the array name.
✗ Incorrect
The children array holds child nodes, so the field and initialization use 'children'.
5fill in blank
hardFill all three blanks to complete the method that finds the longest word in the dictionary.
DSA Java
public String longestWord(String[] words) {
root = new TrieNode();
for (String word : words) {
insert(word);
}
longestWord = "";
dfs(root, new StringBuilder());
return longestWord;
}
private String longestWord = "";
private void dfs(TrieNode node, StringBuilder path) {
if (node == null) return;
if (!node.isEnd && node != [1]) return;
if (path.length() > longestWord.length() ||
(path.length() == longestWord.length() && path.toString().compareTo(longestWord) < 0)) {
longestWord = path.toString();
}
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
path.append((char) (i + 'a'));
dfs(node.children[i], path);
path.deleteCharAt(path.length() - 1);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing node to 'this' or 'longestWord' instead of root.
Using wrong variables in DFS recursion.
✗ Incorrect
We compare node to root to allow the empty prefix; the dfs call uses node.children[i], and the path is updated accordingly.
