Word Search in Trie in DSA Go - Time & Space Complexity
We want to understand how long it takes to search for a word in a Trie data structure.
The question is: how does the search time grow as the word length increases?
Analyze the time complexity of the following code snippet.
func (t *TrieNode) Search(word string) bool {
current := t
for _, ch := range word {
if current.children[ch-'a'] == nil {
return false
}
current = current.children[ch-'a']
}
return current.isEnd
}
This code checks if a given word exists in the Trie by following nodes for each letter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the word.
- How many times: Exactly once per character in the word (word length).
As the word gets longer, the search checks one more node for each letter.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The operations grow linearly with the word length.
Time Complexity: O(n)
This means the search time grows directly with the length of the word you are looking for.
[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 this helps you explain why Tries are efficient for prefix and word searches, a common topic in coding interviews.
"What if the Trie stored Unicode characters instead of only lowercase letters? How would the time complexity change?"
Practice
Trie data structure in word search problems?Solution
Step 1: Understand Trie structure
A Trie stores characters of words in a tree form, sharing prefixes.Step 2: Identify Trie use in word search
This structure allows fast prefix checks and word existence queries.Final Answer:
To store words efficiently for quick prefix-based searches -> Option CQuick Check:
Trie = prefix search efficiency [OK]
- Confusing Trie with sorting structures
- Thinking Trie counts word frequency
- Assuming Trie reverses words
Solution
Step 1: Check struct field types
Children should be a map from rune to pointer to TrieNode for dynamic nodes.Step 2: Verify initialization syntax
Use make(map[rune]*TrieNode) and isEnd as bool with false initial value.Final Answer:
type TrieNode struct { children map[rune]*TrieNode; isEnd bool } root := &TrieNode{children: make(map[rune]*TrieNode), isEnd: false} -> Option DQuick Check:
Correct map and pointer usage = type TrieNode struct { children map[rune]*TrieNode; isEnd bool } root := &TrieNode{children: make(map[rune]*TrieNode), isEnd: false} [OK]
- Using slice instead of map for children
- Using non-pointer TrieNode in map values
- Incorrect map key type (string instead of rune)
func (t *TrieNode) Search(word string) bool {
node := t
for _, ch := range word {
if _, ok := node.children[ch]; !ok {
return false
}
node = node.children[ch]
}
return node.isEnd
}Solution
Step 1: Understand insertion of "car" and "cat"
Both words share prefix 'ca', nodes created for 'c', 'a', then separate nodes for 'r' and 't' with isEnd true.Step 2: Trace search for "cat"
Traverse nodes for 'c', 'a', 't' successfully, last node has isEnd true, so returns true.Final Answer:
true -> Option AQuick Check:
Word inserted and found = true [OK]
- Assuming search returns true for prefixes only
- Confusing isEnd flag usage
- Thinking search returns true if partial path exists
func (t *TrieNode) Search(word string) bool {
node := t
for _, ch := range word {
if node.children[ch] == nil {
return false
}
node = node.children[ch]
}
return true
}Solution
Step 1: Analyze search return condition
Function returns true after traversing all characters without checking isEnd flag.Step 2: Understand consequence of missing isEnd check
Words that are prefixes but not complete words will incorrectly return true.Final Answer:
Returns true even if the word is only a prefix, not a full word -> Option AQuick Check:
Missing isEnd check causes false positives [OK]
- Forgetting to check isEnd flag
- Assuming traversal success means word exists
- Ignoring nil map checks (not in this code)
Solution
Step 1: Insert all words into Trie
Words share prefixes: "app" and "apple" share 'app', "bat", "bath", "baton" share 'bat'.Step 2: Search for "bat"
Since "bat" is inserted, its node's isEnd is true, so search returns true.Final Answer:
Searching "bat" returns true; nodes for 'b', 'a', 't' share prefixes with other words -> Option BQuick Check:
Shared prefix nodes and isEnd true = Searching "bat" returns true; nodes for 'b', 'a', 't' share prefixes with other words [OK]
- Thinking prefix words are not found
- Assuming separate branches for each word
- Believing insertion order affects search result
Ready to prove it?
Pass the test and this pattern moves unknown
