Hint: Children map keys are characters (string), values are TrieNodes [OK]
Common Mistakes:
Using array instead of Map for children
Using wrong types for endOfWord
Initializing endOfWord to true by default
3. Given the following TypeScript TrieNode class and code snippet, what will be the output of console.log(root.children.has('a')) after execution?
class TrieNode {
children: Map;
endOfWord: boolean;
constructor() {
this.children = new Map();
this.endOfWord = false;
}
}
const root = new TrieNode();
root.children.set('a', new TrieNode());
medium
A. undefined
B. false
C. true
D. Error
Solution
Step 1: Understand children map after insertion
We add a key 'a' with a new TrieNode as value to root.children map.
Step 2: Check if 'a' key exists
Calling root.children.has('a') returns true because 'a' was added.
Final Answer:
true -> Option C
Quick Check:
Map has key 'a' = true [OK]
Hint: Map.has(key) returns true if key was set before [OK]
Common Mistakes:
Assuming has returns the value instead of boolean
Confusing undefined with false
Expecting error due to missing key
4. Identify the error in the following TypeScript TrieNode class code snippet:
class TrieNode {
children: Map;
endOfWord: boolean;
constructor() {
this.children = new Map();
this.endOfWord = 'false';
}
}
medium
A. endOfWord is assigned a string instead of boolean
B. children map is not initialized
C. Missing semicolon after constructor
D. Map keys should be number, not string
Solution
Step 1: Check endOfWord type and assignment
endOfWord is declared boolean but assigned string 'false', causing type error.
Step 2: Verify children initialization and syntax
children map is correctly initialized; semicolons are optional in TypeScript; keys as string are correct.
Final Answer:
endOfWord is assigned a string instead of boolean -> Option A
Quick Check:
Boolean variable assigned string = error [OK]
Hint: Boolean variables must be true or false, not strings [OK]
Common Mistakes:
Assigning 'false' (string) instead of false (boolean)
Thinking Map keys must be numbers
Confusing missing semicolons as errors
5. You want to design a TrieNode class in TypeScript that supports Unicode characters as keys and efficiently checks if a node marks the end of a word. Which of the following is the best design choice?
hard
A. Use a Set<string> for children and a boolean endOfWord initialized to true
B. Use an object with fixed keys for children and a string endOfWord to store word
C. Use an array of TrieNode for children indexed by character code and endOfWord as number
D. Use a Map for children and a boolean endOfWord initialized to false
Solution
Step 1: Consider Unicode support for children keys
Map supports any string keys including Unicode characters efficiently.
Step 2: Check endOfWord type for marking word end
Boolean endOfWord initialized to false is standard to mark if node ends a word.
Step 3: Evaluate other options
Objects with fixed keys limit flexibility; arrays indexed by char code waste space; Set<string> cannot map to child nodes.
Final Answer:
Use a Map for children and a boolean endOfWord initialized to false -> Option D
Quick Check:
Unicode keys + boolean flag = Use a Map for children and a boolean endOfWord initialized to false [OK]
Hint: Map handles Unicode keys best [OK]
Common Mistakes:
Using arrays limits Unicode support
Using Set for children loses child node mapping
Using string instead of boolean for endOfWord
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.