A Trie is a tree-like data structure that stores strings by sharing common prefixes. Each node represents a character, and paths from root to nodes form words or prefixes.
Click to reveal answer
beginner
How does a Hash Map handle prefix matching?
A Hash Map stores complete keys and values. For prefix matching, it requires checking all keys to find those starting with the prefix, which can be slow for large data.
Click to reveal answer
intermediate
Why is Trie more efficient than Hash Map for prefix matching?
Trie allows direct traversal along the prefix characters, quickly reaching all words with that prefix without scanning all keys, making prefix queries faster.
Click to reveal answer
intermediate
What is a downside of using Trie compared to Hash Map?
Trie can use more memory because it stores nodes for each character, even if some prefixes are rare, while Hash Map stores only full keys.
Click to reveal answer
beginner
In what scenario is Hash Map preferred over Trie for prefix matching?
When the dataset is small or prefix queries are rare, Hash Map is simpler and uses less memory, making it a better choice than Trie.
Click to reveal answer
Which data structure allows fast prefix matching by traversing characters?
AArray
BHash Map
CTrie
DStack
✗ Incorrect
Trie stores characters in nodes allowing direct prefix traversal.
What is a main disadvantage of using Hash Map for prefix matching?
AUses too much memory
BNeeds to scan all keys for prefix
CCannot store strings
DSlow insertion
✗ Incorrect
Hash Map requires checking all keys to find prefix matches.
Which data structure typically uses more memory for prefix matching?
ATrie
BHash Map
CLinked List
DQueue
✗ Incorrect
Trie stores nodes for each character, increasing memory use.
When is Hash Map preferred over Trie for prefix matching?
ASmall dataset or rare prefix queries
BLarge dataset with many prefixes
CWhen memory is unlimited
DWhen keys are numeric
✗ Incorrect
Hash Map is simpler and efficient for small or infrequent prefix queries.
What does each node in a Trie represent?
AA hash value
BA full word
CA number
DA character
✗ Incorrect
Each Trie node represents a single character.
Explain how Trie and Hash Map differ in handling prefix matching.
Think about how each structure stores and searches strings.
You got /4 concepts.
Describe scenarios where Trie is better than Hash Map and vice versa for prefix matching.
Consider dataset size, query frequency, and memory.
You got /4 concepts.
Practice
(1/5)
1. Which data structure is generally more efficient for prefix matching in a large set of strings?
easy
A. Trie, because it stores characters in a tree structure allowing fast prefix search
B. Hash Map, because it stores keys with direct access for any substring
C. Array, because it stores elements sequentially for quick prefix lookup
D. Stack, because it allows last-in-first-out access which helps prefix matching
Solution
Step 1: Understand prefix matching requirement
Prefix matching means finding all strings starting with a given prefix quickly.
Step 2: Compare Trie and Hash Map for prefix search
Trie stores characters in nodes along paths, enabling fast prefix traversal. Hash Map is good for exact key lookup but not efficient for prefix search.
Final Answer:
Trie, because it stores characters in a tree structure allowing fast prefix search -> Option A
Quick Check:
Prefix search = Trie [OK]
Hint: Trie is best for prefix search, Hash Map for exact keys [OK]
Assume startsWith returns 1 if prefix exists, else 0.
medium
A. 111
B. 110
C. 101
D. 100
Solution
Step 1: Analyze inserted words and prefixes
Words inserted: "apple", "app", "bat". Prefixes checked: "app", "bat", "cat".
Step 2: Check prefix existence
"app" is prefix of "apple" and "app" -> returns 1.
"bat" is inserted word -> returns 1.
"cat" not inserted -> returns 0.
Final Answer:
110 -> Option B
Quick Check:
Prefix found = 1,1,0 [OK]
Hint: Check if prefix matches any inserted word start [OK]
Common Mistakes:
Assuming startsWith returns count instead of boolean
Confusing prefix with exact match
Ignoring that 'cat' was not inserted
4. The following C code attempts to check if a prefix exists in a Trie but always returns 0. What is the bug?
int startsWith(TrieNode* root, char* prefix) {
TrieNode* node = root;
for (int i = 0; prefix[i]; i++) {
if (node->children[prefix[i]] == NULL) {
return 0;
}
node = node->children[prefix[i]];
}
return 1;
}
medium
A. Returning 1 instead of 0 when prefix is empty
B. Not checking if root is NULL before loop
C. Indexing children array without subtracting 'a' from prefix[i]
D. Using prefix[i] instead of prefix[i] - 'a' in loop condition
Solution
Step 1: Identify children array indexing
Children array is indexed by character offset from 'a', so must use prefix[i] - 'a'.
Step 2: Explain impact of wrong indexing
Using prefix[i] directly accesses out-of-bound indices, causing NULL always and returning 0.
Final Answer:
Indexing children array without subtracting 'a' from prefix[i] -> Option C
Quick Check:
Correct indexing = prefix[i] - 'a' [OK]
Hint: Always subtract 'a' when indexing children array [OK]
Common Mistakes:
Forgetting to subtract 'a' from character
Not checking for NULL root
Confusing loop condition with indexing
5. You want to implement a prefix search system for millions of English words. Which approach is better and why?
Use a Trie to store all words and traverse nodes for prefix queries.
Use a Hash Map storing all prefixes as keys with lists of words as values.
Choose the best option.
hard
A. Hash Map, because it uses less memory than Trie
B. Hash Map, because it stores all prefixes explicitly for instant lookup
C. Trie, because it requires storing all prefixes as keys explicitly
D. Trie, because it uses less memory and supports fast prefix traversal
Solution
Step 1: Compare memory usage of Trie vs Hash Map for prefixes
Trie stores characters in shared nodes, avoiding duplicate prefix storage. Hash Map stores every prefix explicitly, causing large memory use.
Step 2: Compare prefix query speed and scalability
Trie allows fast traversal by characters without storing all prefixes. Hash Map lookup is fast but building and storing all prefixes is expensive and slow.
Final Answer:
Trie, because it uses less memory and supports fast prefix traversal -> Option D