Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the children array for a Trie node.
DSA Java
private 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 needs an array of size 26 for each letter.
2fill in blank
mediumComplete the constructor to initialize the isEndOfWord flag to false.
DSA Java
public TrieNode() {
this.isEndOfWord = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to true by default.
Using null or 0 which are invalid for boolean.
✗ Incorrect
When a Trie node is created, it should not mark the end of a word initially, so false is correct.
3fill in blank
hardFix the error in the method to check if a child node exists for a given character.
DSA Java
public boolean containsKey(char ch) {
return children[[1]] != null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'A' instead of 'a' which is uppercase and causes wrong indexing.
Using the character directly which is not an integer index.
✗ Incorrect
To map 'a' to 0, subtract 'a' from the character to get the correct index.
4fill in blank
hardFill both blanks to insert a child node for a character and mark the end of a word.
DSA Java
public void put(char ch, TrieNode node) {
children[[1]] = node;
}
public void setEnd() {
isEndOfWord = [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using character directly as index.
Setting isEndOfWord to false when marking end.
✗ Incorrect
Use 'ch - 'a'' to find the index and set isEndOfWord to true when marking the end.
5fill in blank
hardFill all three blanks to get a child node, check if node is end, and initialize children array.
DSA Java
public TrieNode get(char ch) {
return children[[1]];
}
public boolean isEnd() {
return [2];
}
public TrieNode() {
children = new TrieNode[[3]];
isEndOfWord = false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using character directly as index.
Returning wrong variable in isEnd method.
Initializing children array with wrong size.
✗ Incorrect
Use 'ch - 'a'' for index, return isEndOfWord flag, and initialize children array with size 26.
