Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the Trie.
DSA Typescript
insert(word: string): void {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
node = node.children.[1](char);
}
node.isEndOfWord = true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to access a child node.
Using 'has' which returns a boolean, not the node.
✗ Incorrect
We use 'get' to move to the child node corresponding to the current character.
2fill in blank
mediumComplete the code to check if a prefix exists in the Trie.
DSA Typescript
startsWith(prefix: string): boolean {
let node = this.root;
for (const char of prefix) {
if (!node.children.[1](char)) {
return false;
}
node = node.children.get(char)!;
}
return true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns undefined if key not found, but here we want a boolean check.
Using 'set' which adds or updates a key.
✗ Incorrect
'has' checks if the Map contains the given character key.
3fill in blank
hardFix the error in the Hash Map prefix search function to correctly check prefixes.
DSA Typescript
function hasPrefix(map: Map<string, boolean>, prefix: string): boolean {
for (let i = 1; i <= prefix.length; i++) {
const sub = prefix.slice(0, i);
if (!map.[1](sub)) {
return false;
}
}
return true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns undefined if key not found, but here a boolean check is needed.
Using 'set' which modifies the Map.
✗ Incorrect
We use 'has' to check if the map contains the prefix substring as a key.
4fill in blank
hardFill both blanks to create a Trie search method that returns true if a word exists.
DSA Typescript
search(word: string): boolean {
let node = this.root;
for (const char of word) {
if (!node.children.[1](char)) {
return false;
}
node = node.children.[2](char)!;
}
return node.isEndOfWord;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' before checking existence causes errors if key missing.
Using 'set' or 'delete' which modify the Map.
✗ Incorrect
First check if the child exists with 'has', then get the child node with 'get'.
5fill in blank
hardFill all three blanks to build a Hash Map prefix insertion that stores all prefixes of a word.
DSA Typescript
function insertPrefixes(map: Map<string, boolean>, word: string): void {
for (let i = 1; i <= word.length; i++) {
const prefix = word.[1](0, i);
map.[2](prefix, true);
}
map.[3](word, true);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' or 'has' instead of 'set' to add keys.
Using wrong string method like 'substr' or 'substring' inconsistently.
✗ Incorrect
Use 'slice' to get prefixes, 'set' to add them to the map. The last line also uses 'set' to mark the full word.