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) {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
node = node[1];
}
node.isEnd = true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .children.set(char) instead of .children.get(char) to move to the child node.
Trying to access node.isEnd instead of child nodes.
✗ Incorrect
We move to the child node corresponding to the current character using node.children.get(char).
2fill in blank
mediumComplete the code to check if a word exists in the Trie.
DSA Typescript
search(word: string): boolean {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) {
return false;
}
node = node.children.get(char)!;
}
return node[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning node.children instead of node.isEnd.
Using node.has or node.get which are Map methods, not properties of TrieNode.
✗ Incorrect
To confirm the word exists, we check if the last node marks the end of a word using node.isEnd.
3fill in blank
hardFix the error in the DFS function to find the longest word built one character at a time.
DSA Typescript
dfs(node: TrieNode, path: string): string {
let longest = path;
for (const [char, child] of node.children) {
if (child[1]) {
const word = this.dfs(child, path + char);
if (word.length > longest.length || (word.length === longest.length && word < longest)) {
longest = word;
}
}
}
return longest;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using child.children or child.has which are incorrect here.
Not checking if child.isEnd, causing invalid words to be included.
✗ Incorrect
We only continue DFS if the child node marks the end of a valid word using child.isEnd.
4fill in blank
hardFill both blanks to create a dictionary comprehension that filters words with length greater than 3 and maps them to their lengths.
DSA Typescript
const filteredLengths = {word[1] word of words if word.length [2] 3}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Using '=>' which is for arrow functions, not object literals.
✗ Incorrect
We map each word to its length using ': word.length' and filter words with length greater than 3 using '> 3'.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.
DSA Typescript
const result = { [1]: [2] for const word of words if word.length [3] 2 }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'word.toUpperCase()' for keys.
Using '<' instead of '>' for filtering.
✗ Incorrect
We map uppercase words (word.toUpperCase()) to their lengths (word.length) filtering words longer than 2 (word.length > 2).