Trie Search Operation in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to find a word in a trie.
The question is: how does the search time grow as the word length changes?
Analyze the time complexity of the following code snippet.
class TrieNode {
children: Map = new Map();
isWordEnd: boolean = false;
}
function search(root: TrieNode, word: string): boolean {
let node = root;
for (const char of word) {
if (!node.children.has(char)) return false;
node = node.children.get(char)!;
}
return node.isWordEnd;
}
This code checks if a word exists in a trie by following each character step-by-step.
- Primary operation: Looping through each character of the input word.
- How many times: Exactly once per character, so as many times as the word length.
Each extra letter means one more step to check in the trie.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The time grows directly with the word length, one step per character.
Time Complexity: O(n)
This means the search time grows linearly with the length of the word you are looking for.
[X] Wrong: "Searching a word in a trie depends on the total number of words stored."
[OK] Correct: The search only depends on the length of the word, not how many words are in the trie.
Knowing this helps you explain why tries are fast for prefix searches and dictionary lookups.
"What if the trie used an array instead of a map for children? How would the time complexity change?"
Practice
search operation in a Trie data structure do?Solution
Step 1: Understand the purpose of search in Trie
The search operation is designed to check if a specific word is present in the Trie data structure.Step 2: Differentiate from other operations
Insertion adds words, deletion removes words, and counting totals words, but search only checks existence.Final Answer:
Checks if a given word exists in the Trie -> Option AQuick Check:
Search = Check existence [OK]
- Confusing search with insert operation
- Thinking search deletes words
- Assuming search counts words
Solution
Step 1: Identify input and output types for search
The search method takes a word as a string and returns true or false, so input is string and output is boolean.Step 2: Match method signature with correct types
Onlysearch(word: string): booleanmatches the expected input and output types.Final Answer:
search(word: string): boolean -> Option DQuick Check:
Input string, output boolean [OK]
- Using wrong input type like number or boolean
- Returning void instead of boolean
- Confusing return type with string
const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');
console.log(trie.search('car'));
console.log(trie.search('cap'));Solution
Step 1: Check inserted words in Trie
The words 'cat', 'car', and 'dog' are inserted into the Trie.Step 2: Search for 'car' and 'cap'
'car' exists in the Trie, so search returns true. 'cap' was not inserted, so search returns false.Final Answer:
true\nfalse -> Option BQuick Check:
Inserted words found, missing words not found [OK]
- Assuming 'cap' is found because it starts like 'car'
- Mixing true/false order in output
- Forgetting to insert words before search
search(word: string): boolean {
let node = this.root;
for (let i = 0; i <= word.length; i++) {
const char = word[i];
if (!node.children.has(char)) return false;
node = node.children.get(char)!;
}
return node.isEndOfWord;
}Solution
Step 1: Analyze loop boundary in search method
The loop runs from i = 0 to i <= word.length, which means it accesses word[word.length], an undefined index.Step 2: Correct loop condition to avoid out-of-bounds
The loop should run while i < word.length to access valid characters only.Final Answer:
Loop condition should bei < word.length, not <= -> Option AQuick Check:
Loop boundary off by one [OK]
- Using <= causes undefined access
- Ignoring empty string edge case
- Misusing non-null assertion without checks
Solution
Step 1: Understand prefix search goal
Prefix search should confirm if any word starts with the given prefix, not necessarily a complete word.Step 2: Adjust search logic for prefix
Return true once all prefix characters are found in sequence, without requiring isEndOfWord to be true.Final Answer:
Return true as soon as all prefix characters are found, without checking isEndOfWord -> Option CQuick Check:
Prefix found means true, no full word check needed [OK]
- Checking isEndOfWord for prefix search
- Rejecting short prefixes arbitrarily
- Requiring prefix to match exactly one word
Ready to prove it?
Pass the test and this pattern moves unknown
