Word Search in Trie in DSA C - Time & Space Complexity
When searching for a word in a trie, we want to know how the time taken grows as the word length changes.
We ask: How many steps does it take to find a word in the trie?
Analyze the time complexity of the following code snippet.
// Search for a word in a trie
bool search(TrieNode* root, const char* word) {
TrieNode* current = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (current->children[index] == NULL) {
return false;
}
current = current->children[index];
}
return current->isEndOfWord;
}
This code checks each letter of the word in the trie nodes one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character of the input word.
- How many times: Exactly once per character, so as many times as the word length.
Each additional letter in the word adds one more step to check in the trie.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the word length.
Time Complexity: O(n)
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 linear time search helps you explain efficient prefix lookups and autocomplete features clearly.
"What if the trie stored uppercase and lowercase letters separately? 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-like structure where each node represents a character.Step 2: Identify Trie usage
It allows quick prefix-based search by traversing nodes matching characters in order.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 stores whole words in one node
Solution
Step 1: Understand Trie search logic
Search each character by moving through child nodes; if any character node missing, word not found.Step 2: Eliminate incorrect options
Hash and sorting are unrelated to Trie search; root node alone can't hold whole word.Final Answer:
Traverse nodes for each character; if path ends early, word not found -> Option DQuick Check:
Trie search = character traversal [OK]
- Using hash or sorting instead of Trie traversal
- Checking only root node
- Assuming word is stored in one node
bool search(TrieNode* root, const char* word) {
TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (!node->children[index]) return false;
node = node->children[index];
}
return node->isEndOfWord;
}
// Assume Trie is built with words "cat", "car", "dog"
printf("%d", search(root, "cat"));Solution
Step 1: Analyze search function logic
Function traverses Trie nodes for each character; returns true if last node marks end of word.Step 2: Check if "cat" is in Trie
"cat" was inserted, so traversal succeeds and isEndOfWord is true, returning 1.Final Answer:
1 -> Option AQuick Check:
Word found = 1 [OK]
- Assuming search returns 0 for found words
- Confusing isEndOfWord flag
- Ignoring null checks in traversal
bool search(TrieNode* root, const char* word) {
TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (node->children[index] == NULL) return false;
node = node->children[index];
}
return true;
}Solution
Step 1: Review return condition after loop
Function returns true after traversal without verifying if current node marks end of a valid word.Step 2: Understand Trie word end marking
Words end at nodes with isEndOfWord true; missing this check causes false positives.Final Answer:
It returns true without checking if node marks end of word -> Option CQuick Check:
Must check isEndOfWord before returning true [OK]
- Skipping isEndOfWord check
- Not handling NULL root
- Incorrect index calculation
isEndOfWord.Solution
Step 1: Identify words inserted in Trie
Words inserted are "apple", "app", "ape", "bat", "bath"; only these have isEndOfWord true at last character.Step 2: Check each option word
"ap" is prefix but not a word; "bat", "bath", "apple" are present; "bathroom" not inserted.Final Answer:
"bat", "bath", and "apple" only -> Option BQuick Check:
Search true only for inserted words [OK]
- Assuming prefixes are valid words
- Including words not inserted
- Ignoring isEndOfWord flag
Ready to prove it?
Pass the test and this pattern moves unknown
