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 leaves form words.
Click to reveal answer
beginner
Why can't a hash map efficiently handle prefix searches on strings?
Hash maps store keys independently and do not share prefixes. So, searching for all keys with a common prefix requires checking every key, which is slow.
Click to reveal answer
intermediate
How does a Trie improve prefix search compared to a hash map?
Trie nodes share prefixes, so searching for all words with a prefix is fast by following the prefix path once, then exploring its subtree.
Click to reveal answer
beginner
What is a real-life example where Trie is better than a hash map?
Autocomplete in search engines uses Trie to quickly find all words starting with typed letters, which hash maps cannot do efficiently.
Click to reveal answer
intermediate
What is a limitation of hash maps when used for string keys?
Hash maps do not support ordered traversal or prefix queries efficiently because keys are stored without structure linking similar prefixes.
Click to reveal answer
What does each node in a Trie represent?
AA whole string
BA character of a string
CA number
DA hash value
✗ Incorrect
Each node in a Trie represents a single character of a string.
Why is prefix search slow in a hash map?
ABecause hash maps use trees internally
BBecause hash maps sort keys alphabetically
CBecause hash maps store keys independently without prefix sharing
DBecause hash maps store keys as linked lists
✗ Incorrect
Hash maps store keys independently, so prefix search requires checking all keys.
Which data structure is best for autocomplete features?
ATrie
BHash Map
CStack
DQueue
✗ Incorrect
Trie efficiently supports prefix search needed for autocomplete.
What is a key advantage of Trie over hash map for strings?
ASupports fast prefix queries
BUses less memory always
CFaster insertion for all data types
DAutomatically sorts keys numerically
✗ Incorrect
Trie supports fast prefix queries by sharing prefixes in nodes.
Which operation is inefficient in hash maps for string keys?
AInserting a new key
BFinding a key exactly
CDeleting a key
DFinding all keys with a given prefix
✗ Incorrect
Hash maps cannot efficiently find all keys sharing a prefix.
Explain why Trie data structure exists and what problem it solves that hash maps cannot for strings.
Think about how strings with common beginnings are stored and searched.
You got /4 concepts.
Describe a real-world scenario where using a Trie is better than a hash map for string operations.
Consider typing in a search box and getting suggestions.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a Trie data structure instead of a Hash Map for storing strings?
easy
A. Because Trie is faster for all types of data retrieval
B. Because Trie supports efficient prefix search which Hash Map cannot do easily
C. Because Hash Map cannot store strings as keys
D. Because Trie uses less memory than Hash Map for all data types
Solution
Step 1: Understand Trie and Hash Map capabilities
Trie stores strings in a tree form allowing prefix-based queries efficiently. Hash Map stores key-value pairs but does not support prefix search efficiently.
Step 2: Compare prefix search ability
Trie can quickly find all strings starting with a prefix. Hash Map requires checking all keys or extra structures, which is inefficient.
Final Answer:
Because Trie supports efficient prefix search which Hash Map cannot do easily -> Option B
Quick Check:
Prefix search = Trie [OK]
Hint: Trie excels at prefix search, Hash Map does not [OK]
Common Mistakes:
Thinking Hash Map cannot store strings as keys
Believing Trie always uses less memory
Assuming Trie is faster for all queries
2. Which of the following is the correct way to declare a Trie node structure in C?
easy
A. struct TrieNode { char* children; int isEndOfWord; };
B. struct TrieNode { int children; bool isEndOfWord; };
C. struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; };
D. struct TrieNode { struct TrieNode children; bool isEndOfWord; };
Solution
Step 1: Understand Trie node structure
A Trie node typically has an array of pointers to child nodes (for each alphabet letter) and a boolean to mark end of word.
Step 2: Check each option
struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; }; correctly declares an array of 26 pointers and a boolean flag. Others have wrong types or missing pointers.
Hint: Trie node has pointer array for children and bool flag [OK]
Common Mistakes:
Using int instead of pointer array for children
Declaring children as single struct instead of array
Confusing data types for isEndOfWord
3. What is the output of the following C code snippet that inserts "cat" and "car" into a Trie and checks prefix "ca"?
insert("cat");
insert("car");
bool result = startsWith("ca");
printf("%d", result);
medium
A. 1
B. 0
C. Compilation error
D. Undefined behavior
Solution
Step 1: Understand insert and startsWith functions
Inserting "cat" and "car" adds nodes for 'c', 'a', 't' and 'r'. startsWith("ca") checks if any word starts with "ca".
Step 2: Check if prefix "ca" exists
Both words start with "ca", so startsWith("ca") returns true (1).
Final Answer:
1 -> Option A
Quick Check:
Prefix "ca" found = 1 [OK]
Hint: If words share prefix, startsWith returns 1 [OK]
Common Mistakes:
Assuming startsWith returns 0 if prefix is partial
Confusing insert with search function
Ignoring that both words share prefix
4. Given this buggy Trie insertion code in C, what is the main error?
void insert(struct TrieNode* root, char* word) {
struct TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (node->children[index] == NULL) {
node->children[index] = malloc(sizeof(struct TrieNode));
}
node = node->children[index];
}
node->isEndOfWord = true;
}
medium
A. isEndOfWord is set before loop
B. Index calculation is incorrect
C. Root node is not passed correctly
D. New nodes are not initialized to NULL
Solution
Step 1: Analyze node allocation
When malloc allocates new TrieNode, its children pointers and isEndOfWord are uninitialized garbage values.
Step 2: Identify missing initialization
New nodes must have children array set to NULL and isEndOfWord set to false to avoid errors during traversal.
Final Answer:
New nodes are not initialized to NULL -> Option D
Quick Check:
Uninitialized nodes cause bugs [OK]
Hint: Always initialize new Trie nodes after malloc [OK]
Common Mistakes:
Thinking index calculation is wrong
Setting isEndOfWord too early
Assuming root node passing is incorrect
5. You want to store a large dictionary of words and quickly find all words starting with a given prefix. Why is Trie a better choice than Hash Map, and what is a key limitation of Hash Map in this scenario?
hard
A. Trie allows prefix search in O(k) time; Hash Map cannot do prefix search efficiently
B. Trie uses less memory; Hash Map cannot store strings as keys
C. Hash Map is slower for exact matches; Trie cannot store full words
D. Hash Map requires sorting keys; Trie automatically sorts keys
Solution
Step 1: Understand prefix search complexity
Trie can find all words with a prefix in time proportional to prefix length (O(k)), independent of dictionary size.
Step 2: Identify Hash Map limitation
Hash Map supports exact key lookup efficiently but cannot efficiently find keys by prefix without scanning all keys.
Final Answer:
Trie allows prefix search in O(k) time; Hash Map cannot do prefix search efficiently -> Option A
Quick Check:
Prefix search = Trie advantage [OK]
Hint: Trie = fast prefix search; Hash Map = exact match only [OK]
Common Mistakes:
Believing Hash Map cannot store strings as keys
Thinking Trie uses less memory always
Confusing sorting with prefix search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.