Word Search in Trie in DSA C# - Time & Space Complexity
We want to understand how long it takes to find a word in a Trie data structure.
How does the search time change as the word length or Trie size grows?
Analyze the time complexity of the following code snippet.
public bool Search(string word) {
TrieNode current = root;
foreach (char c in word) {
if (!current.Children.ContainsKey(c)) {
return false;
}
current = current.Children[c];
}
return current.IsEndOfWord;
}
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: 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 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 depends on the total number of words in the Trie."
[OK] Correct: The search only follows the characters of the word, so it depends on word length, not how many words are stored.
Knowing how Trie search scales helps you explain efficient word lookups clearly and confidently in interviews.
"What if we changed the search to find all words with a given prefix? 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-like structure where each node represents a character.Step 2: Identify Trie usage in word search
It allows quick prefix-based search, making it efficient to find words starting with given letters.Final Answer:
To efficiently store and search words by their prefixes -> Option AQuick Check:
Trie = prefix search [OK]
- Confusing Trie with sorting structures
- Thinking Trie stores words linearly
- Assuming Trie is for arithmetic
Solution
Step 1: Check data types for children
Trie children should map characters to TrieNode objects, so Dictionary is correct.Step 2: Check word end marker
Boolean flag IsWordEnd indicates if node ends a word, so bool is correct type.Final Answer:
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsWordEnd = false; } -> Option AQuick Check:
Children dictionary + bool flag = public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsWordEnd = false; } [OK]
- Using List<int> instead of Dictionary
- Using string or int for children
- Using int instead of bool for IsWordEnd
Trie trie = new Trie();
trie.Insert("cat");
trie.Insert("car");
trie.Insert("dog");
bool result = trie.Search("cap");
Console.WriteLine(result);Solution
Step 1: Understand inserted words
Words "cat", "car", and "dog" are inserted into the Trie.Step 2: Search for "cap"
"cap" shares prefix "ca" but 'p' is not inserted after 'a', so search returns false.Final Answer:
False -> Option BQuick Check:
Word not found = False [OK]
- Assuming prefix match means word found
- Expecting true for partial matches
- Confusing compilation/runtime errors
public bool Search(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c))
return false;
node = node.Children[c];
}
return true;
}Solution
Step 1: Analyze search logic
The method checks if all characters exist in Trie nodes but does not check if last node marks end of a word.Step 2: Identify missing condition
Without checking node.IsWordEnd, it returns true for prefixes that are not complete words.Final Answer:
It returns true even if the word is only a prefix, not a complete word -> Option CQuick Check:
Missing IsWordEnd check = It returns true even if the word is only a prefix, not a complete word [OK]
- Ignoring IsWordEnd flag
- Assuming prefix means word found
- Not handling empty strings
Solution
Step 1: Locate prefix node in Trie
Traverse characters 'p', 'r', 'e' to reach the node representing last prefix character.Step 2: Collect words using DFS
From this node, perform depth-first search to find all complete words that start with "pre".Final Answer:
Traverse the Trie to the node representing 'e' in "pre", then perform a DFS to collect all words below -> Option DQuick Check:
Prefix node + DFS = Traverse the Trie to the node representing 'e' in "pre", then perform a DFS to collect all words below [OK]
- Checking all words individually
- Using unrelated data structures
- Rebuilding Trie unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
