📖 Scenario: You are building a simple word search tool that can quickly check if a word exists in a collection. To do this efficiently, you will use a Trie data structure, which stores words in a tree-like format.Imagine a phone contact list where you want to find if a name exists by typing letters one by one. The Trie helps you find matches fast.
🎯 Goal: Create a Trie to store a list of words, then write a function to search if a given word exists in the Trie.
📋 What You'll Learn
Create a TrieNode class with a children map and an end-of-word flag
Create a Trie class with insert and search methods
Insert given words into the Trie
Search for a specific word in the Trie and return true if found, false otherwise
💡 Why This Matters
🌍 Real World
Tries are used in autocomplete features, spell checkers, and search engines to quickly find words or prefixes.
💼 Career
Understanding Tries helps in roles involving text processing, search optimization, and building efficient data retrieval systems.
Progress0 / 4 steps
1
Create TrieNode class
Create a class called TrieNode with a property children initialized as an empty Map<string, TrieNode> and a boolean property isEndOfWord set to false.
DSA Typescript
Hint
Use Map to store children nodes and a boolean to mark the end of a word.
2
Create Trie class with insert method
Create a class called Trie with a property root initialized as a new TrieNode(). Add a method insert(word: string): void that inserts the given word into the Trie by creating nodes for each character and marking the last node's isEndOfWord as true.
DSA Typescript
Hint
Loop through each character of the word, add nodes if missing, and mark the last node as end of word.
3
Add search method to Trie
Add a method search(word: string): boolean to the Trie class that returns true if the word exists in the Trie (all characters found and last node's isEndOfWord is true), otherwise returns false.
DSA Typescript
Hint
Traverse the Trie for each character. If missing, return false. At end, check if node marks end of word.
4
Insert words and search for a word
Create a Trie instance called trie. Insert the words "apple", "app", and "bat" into trie. Then print the result of trie.search("app").
DSA Typescript
Hint
Insert each word using insert and then print the result of search("app").
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in word search problems?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a linked list format
D. To perform arithmetic operations on words
Solution
Step 1: Understand the Trie structure
A Trie stores characters in a tree where each path represents a prefix of words.
Step 2: Identify the use in word search
This structure allows quick prefix-based search, making it efficient for word search problems.
Final Answer:
To efficiently store and search words by their prefixes -> Option A
Quick Check:
Trie = prefix search [OK]
Hint: Trie is all about prefixes and fast word lookup [OK]
Common Mistakes:
Confusing Trie with sorting structures
Thinking Trie stores words linearly
Assuming Trie performs arithmetic
2. Which of the following is the correct TypeScript syntax to declare a Trie node class with a children map and an end-of-word boolean?
easy
A. class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = false; } }
B. class TrieNode { children: string[]; isEndOfWord: number; constructor() { this.children = []; this.isEndOfWord = 0; } }
C. class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = true; } }
D. class TrieNode { children: object; isEndOfWord: string; constructor() { this.children = {}; this.isEndOfWord = 'false'; } }
Solution
Step 1: Check children type
Children should be a Map from string to TrieNode for efficient character mapping.
Step 2: Check isEndOfWord type and initialization
It should be a boolean initialized to false to mark word endings correctly.
Final Answer:
class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = false; } } -> Option A
Quick Check:
Correct types and initialization [OK]
Hint: Use Map and boolean for end flag [OK]
Common Mistakes:
Using array instead of Map for children
Using wrong types for isEndOfWord
Initializing isEndOfWord incorrectly
3. Given the following TypeScript code snippet for searching a word in a Trie, what will be the output when searching for the word "cat" if only "car" and "cap" are inserted?
class TrieNode {
children = new Map();
isEndOfWord = false;
}
class Trie {
root = new TrieNode();
insert(word: string) {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) {
node.children.set(ch, new TrieNode());
}
node = node.children.get(ch)!;
}
node.isEndOfWord = true;
}
search(word: string): boolean {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
return node.isEndOfWord;
}
}
const trie = new Trie();
trie.insert("car");
trie.insert("cap");
console.log(trie.search("cat"));
medium
A. true
B. false
C. undefined
D. Runtime error
Solution
Step 1: Trace insertion of "car" and "cap"
Both words create nodes for 'c', 'a', then diverge at 'r' and 'p' respectively, marking those ends as true.
Step 2: Trace search for "cat"
Search finds 'c' and 'a', but 't' is not a child of 'a', so search returns false.
Final Answer:
false -> Option B
Quick Check:
Word not inserted = false [OK]
Hint: If last char missing, search returns false [OK]
Common Mistakes:
Assuming partial prefix means true
Confusing isEndOfWord with node existence
Expecting undefined instead of false
4. Identify the bug in the following TypeScript Trie search method that causes it to always return false:
search(word: string): boolean {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
return node.isEndOfWord === false;
}
medium
A. The method should return true before the loop ends
B. The loop should iterate backwards over the word
C. The children map should be an array instead
D. The return condition should be node.isEndOfWord, not its negation
Solution
Step 1: Analyze the return statement
The method returns node.isEndOfWord === false, which means it returns true if the word is NOT ending here.
Step 2: Correct the return logic
It should return node.isEndOfWord to confirm the word ends at this node.
Final Answer:
The return condition should be node.isEndOfWord, not its negation -> Option D
Quick Check:
Return true if word ends here [OK]
Hint: Return node.isEndOfWord directly to confirm word end [OK]
Common Mistakes:
Returning negation of isEndOfWord
Changing loop direction unnecessarily
Using wrong data structure for children
5. You want to implement a function that finds all words in a Trie starting with a given prefix. Which approach correctly combines Trie traversal and backtracking to collect all matching words?
hard
A. Search each word in the Trie individually to check prefix match
B. Store all words in an array and filter by prefix each time
C. Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking
D. Use a hash map to map prefixes to words without Trie traversal
Solution
Step 1: Traverse Trie to prefix node
Follow characters of prefix to reach the node representing last prefix character.
Step 2: Use backtracking from prefix node
Recursively explore all children nodes to collect complete words starting with prefix.
Final Answer:
Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking -> Option C
Quick Check:
Prefix traversal + backtracking = Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking [OK]
Hint: Find prefix node, then backtrack to collect words [OK]
Common Mistakes:
Checking all words instead of prefix traversal
Filtering array instead of Trie traversal
Using hash map without Trie structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.