Trie Search Operation in DSA Go - Time & Space Complexity
We want to know how long it takes to find a word in a trie.
How does the search time change when the word gets longer?
Analyze the time complexity of the following code snippet.
func (t *Trie) Search(word string) bool {
node := t.root
for _, ch := range word {
if node.children[ch] == nil {
return false
}
node = node.children[ch]
}
return node.isEnd
}
This code checks if a word exists in the trie by following each character step-by-step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the input word.
- How many times: Exactly once per character in the word.
Each extra character 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 number of steps grows directly with the word length.
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 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 trie search time helps you explain efficient word lookups and autocomplete features clearly.
"What if we changed the trie to store only lowercase letters instead of all characters? How would the time complexity change?"
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 stored in the Trie.Step 2: Differentiate from other Trie operations
Other operations like counting words or checking emptiness are not the purpose of search.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 checks Trie emptiness
- Assuming search returns word length
Solution
Step 1: Recall Go map access syntax
In Go, to check if a key exists in a map, you check if the value is not nil (for pointer or map values).Step 2: Apply to Trie children map
Trie children are stored in a map from rune to *TrieNode, so checking if node.children[char] != nil confirms existence.Final Answer:
if node.children[char] != nil -> Option AQuick Check:
Go map key exists check [OK]
- Using contains or has methods (not in Go maps)
- Checking for equality to nil incorrectly
- Confusing map key existence with value presence
func (t *Trie) Search(word string) bool {
node := t.root
for _, ch := range word {
if node.children[ch] == nil {
return false
}
node = node.children[ch]
}
return node.isEnd
}
What will be the output of Search("cat") if the Trie contains the words "car", "cat", and "cap"?Solution
Step 1: Trace the search for "cat" in the Trie
The loop checks each character 'c', 'a', 't' in order. Since "cat" is inserted, all nodes exist and node.isEnd is true at 't'.Step 2: Return value after traversal
Because node.isEnd is true at the last character, the function returns true.Final Answer:
true -> Option BQuick Check:
Word "cat" exists in Trie = true [OK]
- Ignoring isEnd flag and returning true always
- Assuming false if any child exists
- Confusing runtime errors with nil checks
func (t *Trie) Search(word string) bool {
node := t.root
for i := 0; i <= len(word); i++ {
ch := rune(word[i])
if node.children[ch] == nil {
return false
}
node = node.children[ch]
}
return node.isEnd
}Solution
Step 1: Analyze the loop condition
The loop runs from i = 0 to i <= len(word), which means it tries to access word[len(word)] causing index out of range error.Step 2: Correct loop boundary
The loop should run with i < len(word) to avoid accessing beyond the string length.Final Answer:
Index out of range error due to loop condition -> Option DQuick Check:
Loop boundary error causes runtime panic [OK]
- Using <= instead of < in loop
- Ignoring string length limits
- Assuming rune conversion causes error
Solution
Step 1: Understand prefix search goal
Prefix search should return true if the prefix exists as a path in the Trie, regardless of isEnd flag.Step 2: Modify search to return true after prefix traversal
After checking all prefix characters exist, return true immediately without checking isEnd.Final Answer:
Return true immediately after traversing all prefix characters without checking isEnd -> Option AQuick Check:
Prefix search ignores isEnd, returns true if path exists [OK]
- Checking isEnd for prefix search
- Returning false even if prefix path exists
- Confusing prefix length with word length
Ready to prove it?
Pass the test and this pattern moves unknown
