Challenge - 5 Problems
Prefix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Prefix Search Result
What is the output of the following TypeScript code that inserts words into a Trie and searches for words starting with a prefix?
DSA Typescript
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); 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.get(char)!; } node.isEndOfWord = true; } searchPrefix(prefix: string): string[] { let node = this.root; for (const char of prefix) { if (!node.children.has(char)) { return []; } node = node.children.get(char)!; } const results: string[] = []; const dfs = (currentNode: TrieNode, path: string) => { if (currentNode.isEndOfWord) { results.push(path); } for (const [char, childNode] of currentNode.children) { dfs(childNode, path + char); } }; dfs(node, prefix); return results; } } const trie = new Trie(); trie.insert("apple"); trie.insert("app"); trie.insert("apex"); trie.insert("bat"); trie.insert("bath"); const result = trie.searchPrefix("ap"); console.log(result);
Attempts:
2 left
💡 Hint
Think about how the DFS collects words in alphabetical order based on children map iteration.
✗ Incorrect
The DFS visits children in insertion order of Map keys, which in JavaScript/TypeScript preserves insertion order. Since 'app' was inserted before 'apple' and 'apex', the results array collects words in the order: 'app', 'apple', 'apex'.
🧠 Conceptual
intermediate1:00remaining
Number of Words with Given Prefix
If a Trie contains the words ["cat", "car", "cart", "dog", "dove"], how many words start with the prefix "car"?
Attempts:
2 left
💡 Hint
Count all words that begin exactly with 'car'.
✗ Incorrect
The words starting with 'car' are 'car' and 'cart', so there are 2 words.
🔧 Debug
advanced1:30remaining
Identify the Bug in Trie Insertion
What error will occur when running this TypeScript code snippet for inserting a word into a Trie?
DSA Typescript
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); 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.get(char); // Bug here } node.isEndOfWord = true; } } const trie = new Trie(); trie.insert("hello");
Attempts:
2 left
💡 Hint
Check the return type of Map.get and how it's used.
✗ Incorrect
Map.get returns TrieNode | undefined. Without the non-null assertion operator (!), node can become undefined, causing a TypeError when accessing isEndOfWord.
📝 Syntax
advanced1:30remaining
Correct Syntax for DFS in Trie Search
Which option correctly defines the DFS function inside the searchPrefix method to collect all words starting with a prefix?
DSA Typescript
dfs = (currentNode: TrieNode, path: string) => {
if (currentNode.isEndOfWord) {
results.push(path);
}
for (const [char, childNode] of currentNode.children) {
dfs(childNode, path + char);
}
};Attempts:
2 left
💡 Hint
Arrow functions with const are common for nested functions in TypeScript.
✗ Incorrect
Option C correctly uses a const arrow function with proper syntax. Option C is valid but uses function declaration, which is also correct but less common inside methods. Option C is invalid syntax. Option C is valid but less idiomatic than A.
🚀 Application
expert2:30remaining
Find All Words with Prefix and Sort Alphabetically
Given a Trie with words inserted in any order, which code modification ensures that searchPrefix returns all words starting with the prefix sorted alphabetically?
DSA Typescript
searchPrefix(prefix: string): string[] {
let node = this.root;
for (const char of prefix) {
if (!node.children.has(char)) {
return [];
}
node = node.children.get(char)!;
}
const results: string[] = [];
const dfs = (currentNode: TrieNode, path: string) => {
if (currentNode.isEndOfWord) {
results.push(path);
}
for (const [char, childNode] of currentNode.children) {
dfs(childNode, path + char);
}
};
dfs(node, prefix);
return results;
}Attempts:
2 left
💡 Hint
Think about controlling the order of traversal rather than sorting after collection.
✗ Incorrect
Sorting keys before recursion ensures DFS visits children in alphabetical order, so results are collected sorted without extra sorting step. Option A sorts after collection but is less efficient. Option A changes data structure but does not guarantee order. Option A adds complexity.