Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a TrieNode class with a children map.
DSA Typescript
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; constructor() { this.children = new [1](); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array instead of Map for children.
Using Set which does not store key-value pairs.
✗ Incorrect
A TrieNode uses a Map to store children nodes keyed by characters for efficient lookup.
2fill in blank
mediumComplete the code to add a child node for a given character in the TrieNode.
DSA Typescript
addChild(char: string) {
if (!this.children.has(char)) {
this.children.set(char, new [1]());
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Map or Set instead of TrieNode for child nodes.
Using an undefined class name like Node.
✗ Incorrect
Each child is a new TrieNode instance representing the next character.
3fill in blank
hardFix the error in the constructor to initialize isEndOfWord correctly.
DSA Typescript
constructor() {
this.children = new Map();
this.isEndOfWord = [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to true by default.
Using null or undefined which are not booleans.
✗ Incorrect
isEndOfWord should start as false because the node initially does not mark the end of a word.
4fill in blank
hardFill both blanks to check if a character exists and retrieve its child node.
DSA Typescript
getChild(char: string): TrieNode | undefined {
if (this.children.[1](char)) {
return this.children.[2](char);
}
return undefined;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using set() instead of get() to retrieve a value.
Using delete() which removes a key.
✗ Incorrect
Use has() to check existence and get() to retrieve the child node from the Map.
5fill in blank
hardFill all three blanks to implement a method that marks the node as end of a word and adds a child if missing.
DSA Typescript
markEndAndAddChild(char: string) {
this.isEndOfWord = [1];
if (!this.children.[2](char)) {
this.children.[3](char, new TrieNode());
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to false instead of true.
Using get() instead of has() to check existence.
Using delete() instead of set() to add child.
✗ Incorrect
Set isEndOfWord to true, check existence with has(), and add child with set().