Word Search in Trie in DSA Javascript - Time & Space Complexity
When searching for a word in a Trie, we want to know how long it takes as the word gets longer.
We ask: How does the search time grow when the word length increases?
Analyze the time complexity of the following code snippet.
function searchWord(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 given word exists in the Trie by following each character step-by-step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character in the word.
- How many times: Once for each character in the word (word length).
As the word gets longer, the search checks more characters one by one.
| Input Size (word length) | Approx. Operations |
|---|---|
| 10 | 10 character checks |
| 100 | 100 character checks |
| 1000 | 1000 character checks |
Pattern observation: The number of operations 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.
Understanding this helps you explain why Tries are efficient for prefix and word searches, a common topic in coding interviews.
"What if the Trie nodes used arrays instead of hash maps for children? How would the time complexity change?"
Practice
Trie data structure in word search?Solution
Step 1: Understand Trie structure
A Trie stores characters of words in a tree where each node represents a character.Step 2: Identify Trie usage
It allows quick prefix-based search by traversing nodes matching characters.Final Answer:
To efficiently store and search words by their prefixes -> Option AQuick Check:
Trie = prefix search [OK]
- Confusing Trie with hash tables
- Thinking Trie sorts words
- Assuming Trie does arithmetic
Solution
Step 1: Understand Trie search method
Search involves traversing nodes for each character and checking if the word ends at last node.Step 2: Eliminate incorrect options
Array.includes and JSON.stringify are not suitable for Trie structure. Math.max is unrelated.Final Answer:
Use a loop to traverse nodes matching each character and check end flag -> Option AQuick Check:
Traverse nodes + check end = correct search [OK]
- Using array methods on Trie
- Trying to convert Trie to string
- Applying math functions on words
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'
Both words are added character by character, marking end nodes for 't' and 'r'.Step 2: Search 'cat' and 'cap'
'cat' exists with isEnd true, 'cap' path breaks at 'p' missing node, returns false.Final Answer:
true false -> Option CQuick Check:
Search 'cat' = true, 'cap' = false [OK]
- Assuming 'cap' exists after inserting 'car'
- Confusing isEnd flag
- Ignoring missing child nodes
search(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}
return true;
}Solution
Step 1: Analyze current search return
Returns true if path exists but does not check if word ends here.Step 2: Correct return value
Must return node.isEnd to confirm the word is complete, not just prefix.Final Answer:
Return node.isEnd instead of true to confirm full word match -> Option DQuick Check:
Check isEnd flag to confirm word end [OK]
- Returning true without checking isEnd
- Changing loop type unnecessarily
- Ignoring empty word cases
Solution
Step 1: Insert all words into Trie
Build Trie so each word is represented by a path of nodes.Step 2: Traverse prefix nodes and collect words
Find node matching prefix end, then recursively collect all words below it.Step 3: Compare other options
Sorting and binary search is possible but less efficient for many queries. Hash maps need many keys. Checking each word is slow.Final Answer:
Insert all words into Trie, traverse prefix nodes, then collect all descendant words -> Option BQuick Check:
Trie + prefix traversal = efficient prefix search [OK]
- Ignoring Trie traversal for prefix
- Using slow linear search for prefix
- Confusing hash map with Trie
Ready to prove it?
Pass the test and this pattern moves unknown
