Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Trie node with an empty children map.
DSA Typescript
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } const node = new TrieNode(); node.children.[1]('a', new TrieNode());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'push' which are not methods of Map.
Trying to assign directly without using a method.
✗ Incorrect
The Map object uses the 'set' method to add key-value pairs. Here, 'a' is the key and a new TrieNode is the value.
2fill in blank
mediumComplete the code to check if a character exists in the children map of a Trie node.
DSA Typescript
function hasChild(node: TrieNode, char: string): boolean {
return node.children.[1](char);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' or 'includes' which are not Map methods.
Trying to access children[char] directly without checking.
✗ Incorrect
The Map object has a 'has' method to check if a key exists.
3fill in blank
hardFix the error in the insert method to correctly add characters to the Trie.
DSA Typescript
class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let current = this.root; for (const char of word) { if (!current.children.[1](char)) { current.children.set(char, new TrieNode()); } current = current.children.get(char)!; } current.isEndOfWord = true; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns a value, not a boolean.
Using 'set' which adds or updates a value, not checks existence.
✗ Incorrect
We need to check if the children map has the character key before adding a new node. The 'has' method is used for this check.
4fill in blank
hardFill both blanks to complete the search method that returns true if the word exists in the Trie.
DSA Typescript
class Trie { root: TrieNode = new TrieNode(); search(word: string): boolean { let current = this.root; for (const char of word) { if (!current.children.[1](char)) { return false; } current = current.children.[2](char)!; } return current.isEndOfWord; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to retrieve a node.
Using 'contains' which is not a Map method.
✗ Incorrect
To check if a character exists, use 'has'. To get the child node, use 'get'.
5fill in blank
hardFill all three blanks to complete the method that checks if any word in the Trie starts with the given prefix.
DSA Typescript
class Trie { root: TrieNode = new TrieNode(); startsWith(prefix: string): boolean { let current = this.root; for (const char of prefix) { if (!current.children.[1](char)) { return false; } current = current.children.[2](char)!; } return [3]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false at the end instead of true.
Using 'set' instead of 'get' to retrieve nodes.
✗ Incorrect
We check if each character exists with 'has', get the node with 'get', and if all characters are found, return true.