Trie Search Operation in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to find a word in a trie. This helps us know how fast searches are as words get longer.
The question is: How does search time grow when the word length changes?
Analyze the time complexity of the following code snippet.
function search(root, word) {
let node = root;
for (let char of word) {
if (!node.children[char]) {
return false;
}
node = node.children[char];
}
return node.isEndOfWord === true;
}
This code checks if a word exists in a trie by moving through nodes for each letter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the word.
- How many times: Once for every letter in the word (word length).
As the word gets longer, the search checks more letters one by one.
| Input Size (word length) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of steps grows directly with the word length.
Time Complexity: O(m)
This means the search time grows linearly with the length of the word being searched.
[X] Wrong: "Searching a word in a trie takes time proportional to the number of words stored."
[OK] Correct: The search depends only on the length of the word, not how many words are in the trie.
Knowing trie search time helps you explain why tries are fast for prefix searches and autocomplete features. This skill shows you understand efficient word lookups.
"What if the trie stored words with very long common prefixes? How would that affect the search time complexity?"
Practice
Solution
Step 1: Understand the purpose of Trie search
The search operation in a Trie is designed to check if a specific word is present in the Trie by traversing nodes corresponding to each character.Step 2: Identify what the search confirms
It confirms the presence or absence of the entire word, not the count or length of words stored.Final Answer:
If a given word exists in the Trie -> Option CQuick Check:
Trie search checks word existence [OK]
- Confusing search with counting words
- Thinking search returns word length
- Assuming search checks if Trie is empty
Solution
Step 1: Understand Trie node structure in JavaScript
Trie nodes are usually objects where keys are characters. Checking if a character exists uses property access and undefined check.Step 2: Identify correct syntax for existence check
Using node[char] !== undefined correctly checks if the character key exists in the node object.Final Answer:
if (node[char] !== undefined) -> Option BQuick Check:
Check node property with !== undefined [OK]
- Using dot notation with variable char
- Using incorrect method like has() on object
- Checking for null instead of undefined
class TrieNode {
constructor() {
this.children = {};
this.isEnd = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.isEnd = true;
}
search(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}
return node.isEnd;
}
}
const trie = new Trie();
trie.insert('cat');
trie.insert('car');
console.log(trie.search('cat'));
console.log(trie.search('cap'));Solution
Step 1: Insert words 'cat' and 'car' into Trie
Both words are inserted by creating nodes for each character and marking the last node's isEnd as true.Step 2: Search for 'cat' and 'cap'
'cat' exists fully with isEnd true, so search returns true. 'cap' is missing 'p' node, so search returns false.Final Answer:
true false -> Option AQuick Check:
Search 'cat' = true, 'cap' = false [OK]
- Assuming partial matches return true
- Not setting isEnd flag on insert
- Confusing children object access
search(word) {
let node = this.root;
for (const char of word) {
if (node.children[char] === undefined) return false;
node = node.children[char];
}
return true;
}Solution
Step 1: Analyze the search method's return value
The method returns true after traversing all characters without checking if the last node marks the end of a word.Step 2: Correct the return to check word completion
Return node.isEnd to confirm the word ends here, not just prefix exists.Final Answer:
Return node.isEnd instead of true at the end -> Option DQuick Check:
Check isEnd flag to confirm full word [OK]
- Returning true without isEnd check
- Changing comparison operator incorrectly
- Unnecessary loop syntax changes
Solution
Step 1: Identify words present in the Trie
The Trie contains 'apple', 'app', 'bat', and 'bath'. Only these exact words return true on search.Step 2: Check each option's search queries
search('app'), search('bat'), search('bath') searches 'app', 'bat', 'bath' all present. Others include partial or missing words like 'appl' or 'batman' which return false.Final Answer:
search('app'), search('bat'), search('bath') -> Option AQuick Check:
Only full words in Trie return true [OK]
- Assuming partial words return true
- Confusing prefixes with full words
- Including words not inserted in Trie
Ready to prove it?
Pass the test and this pattern moves unknown
