A Trie is a tree-like data structure used to store a dynamic set of strings where keys are usually strings. It allows fast retrieval of words by their prefixes.
Click to reveal answer
beginner
How does prefix search work in a Trie?
Prefix search in a Trie involves traversing the tree nodes following the characters of the prefix. If traversal succeeds, all words under that node share the prefix.
Click to reveal answer
intermediate
What is the time complexity of searching a prefix in a Trie?
The time complexity is O(m), where m is the length of the prefix, because we traverse one node per character.
Click to reveal answer
intermediate
Why is Trie preferred over hash tables for prefix search?
Trie allows efficient prefix search by sharing common prefixes in nodes, while hash tables do not support prefix queries efficiently.
Click to reveal answer
beginner
What does each node in a Trie typically contain?
Each node contains an array or map of child nodes for each possible character and a boolean flag indicating if the node marks the end of a word.
Click to reveal answer
What does a Trie node usually store?
AA hash of the word
BOnly the character it represents
CPointers to child nodes and end-of-word flag
DThe entire word string
✗ Incorrect
Trie nodes store pointers to child nodes for each character and a flag to mark if a word ends at that node.
What is the main advantage of using a Trie for prefix search?
AFast prefix lookup by traversing nodes
BUses less memory than arrays
CStores words in sorted order automatically
DSupports fast numeric calculations
✗ Incorrect
Trie allows fast prefix lookup by following nodes matching prefix characters.
If a prefix is not found in a Trie, what does it mean?
ANo word in the Trie starts with that prefix
BAll words start with that prefix
CThe Trie is empty
DThe prefix is a complete word
✗ Incorrect
If traversal for the prefix fails, no word in the Trie starts with that prefix.
What is the time complexity to insert a word of length n into a Trie?
AO(log n)
BO(1)
CO(n^2)
DO(n)
✗ Incorrect
Insertion requires traversing or creating nodes for each character, so O(n).
Which data structure is NOT suitable for prefix search?
ATrie
BHash Table
CPrefix Tree
DRadix Tree
✗ Incorrect
Hash tables do not support efficient prefix search as they do exact key lookups.
Explain how prefix search works using a Trie data structure.
Think about walking down the tree following prefix letters.
You got /3 concepts.
Describe the structure of a Trie node and its role in prefix search.
Focus on what each node stores and how it helps find prefixes.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in prefix search?
easy
A. To perform arithmetic operations quickly
B. To sort words alphabetically
C. To store numbers in sorted order
D. To efficiently find all words that start with a given prefix
Solution
Step 1: Understand the role of Trie in prefix search
A Trie stores characters of words in a tree structure, allowing quick prefix lookups.
Step 2: Identify the main use case
It helps find all words starting with a prefix efficiently, unlike sorting or arithmetic.
Final Answer:
To efficiently find all words that start with a given prefix -> Option D
Quick Check:
Trie = Prefix search efficiency [OK]
Hint: Trie is built for prefix-based word retrieval [OK]
Common Mistakes:
Confusing Trie with sorting algorithms
Thinking Trie is for arithmetic
Assuming Trie stores numbers only
2. Which of the following is the correct way to declare a Trie node structure in C for prefix search?
easy
A. typedef struct TrieNode { int data; struct TrieNode* next; } TrieNode;
C. typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode;
D. typedef struct TrieNode { int value; struct TrieNode* left; struct TrieNode* right; } TrieNode;
Solution
Step 1: Identify Trie node components
A Trie node has an array of pointers for each alphabet letter and a flag for word end.
Step 2: Match with given options
typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; matches this structure with 26 children pointers and an end-of-word flag.
Final Answer:
typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; -> Option C
Quick Check:
Trie node = children array + end flag [OK]
Hint: Trie nodes have 26 children pointers for letters a-z [OK]
Common Mistakes:
Using linked list pointers instead of array for children
Confusing Trie node with linked list or binary tree node
Missing end-of-word flag
3. Given the following code snippet inserting words "cat", "car", and "dog" into a Trie, what will be the output of searching prefix "ca"?
Words "cat", "car", and "dog" are inserted, building branches for 'c' and 'd'.
Step 2: Search prefix "ca"
Both "cat" and "car" start with "ca", so they are returned by searchPrefix.
Final Answer:
["cat", "car"] -> Option A
Quick Check:
Prefix "ca" matches cat and car [OK]
Hint: Words starting with prefix are returned [OK]
Common Mistakes:
Returning only one matching word
Returning words not matching prefix
Empty result for existing prefix
4. Consider this buggy C code snippet for inserting a word into a Trie. What is the main error?
void insert(TrieNode* root, char* word) {
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(TrieNode));
}
node = node->children[index];
}
node->isEndOfWord = 1;
}
medium
A. New nodes are not initialized after malloc
B. Index calculation is incorrect
C. Loop condition should be i < strlen(word)
D. isEndOfWord flag is set too early
Solution
Step 1: Analyze node creation
malloc allocates memory but does not initialize children pointers or isEndOfWord flag.
Step 2: Identify impact
Uninitialized children pointers can cause undefined behavior during traversal or insertion.
Final Answer:
New nodes are not initialized after malloc -> Option A
Quick Check:
malloc needs initialization [OK]
Hint: Always initialize new Trie nodes after malloc [OK]
Common Mistakes:
Assuming malloc zeroes memory
Ignoring initialization of isEndOfWord
Misunderstanding loop condition
5. You want to find all words in a Trie that start with prefix "app" and have length exactly 5. Which approach correctly combines prefix search and length filtering?
hard
A. Collect all words in Trie, then filter those starting with "app" and length 5
B. Traverse Trie to node for "app", then collect words of length 5 from there
C. Search for "app" as a full word, then check length 5
D. Use binary search on sorted word list for prefix "app" and length 5
Solution
Step 1: Use Trie prefix traversal
Traverse the Trie to the node representing the prefix "app" to limit search space.
Step 2: Collect words of length 5 from that node
From this node, recursively collect words that complete to length 5.
Final Answer:
Traverse Trie to node for "app", then collect words of length 5 from there -> Option B
Quick Check:
Prefix traversal + length filter = Traverse Trie to node for "app", then collect words of length 5 from there [OK]
Hint: Start at prefix node, then filter by length [OK]
Common Mistakes:
Filtering all words instead of prefix subtree
Searching prefix as full word
Using binary search on Trie data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.