A Trie is a tree-like data structure that stores a dynamic set of strings, where each node represents a character. It is used to efficiently search and store words by sharing common prefixes.
Click to reveal answer
intermediate
How does a Trie help in finding the longest word in a dictionary?
A Trie helps by storing all words so that we can easily check if all prefixes of a word exist. The longest word is the one whose every prefix is also a word in the Trie.
Click to reveal answer
intermediate
What is the key condition to consider a word as the longest word in the dictionary using Trie?
Every prefix of the word must be present as a complete word in the Trie. This ensures the word can be built one character at a time from other words.
Click to reveal answer
beginner
In C, what is the typical structure of a Trie node for lowercase English letters?
A Trie node usually contains an array of 26 pointers (one for each letter 'a' to 'z') and a boolean flag to mark the end of a word.
Click to reveal answer
advanced
Why do we prefer Trie over sorting when finding the longest word with all prefixes present?
Trie allows checking prefix existence efficiently without repeated string comparisons, making it faster for large dictionaries.
Click to reveal answer
What does each node in a Trie represent?
AA whole word
BA character of a word
CA number
DA sentence
✗ Incorrect
Each node in a Trie represents a single character of a word.
What must be true for a word to be considered the longest word in the dictionary using Trie?
AAll its prefixes are also words in the Trie
BIt is the first word inserted
CIt has the most vowels
DIt is the shortest word
✗ Incorrect
The longest word must have all its prefixes present as words in the Trie.
How many children pointers does a Trie node have for lowercase English letters?
A26
B52
C10
D1
✗ Incorrect
Each Trie node has 26 pointers, one for each lowercase English letter.
Which of these is NOT a benefit of using Trie for longest word search?
AEfficient prefix checking
BFaster than sorting for prefix checks
CAutomatically sorts words alphabetically
DMemory efficient for large datasets
✗ Incorrect
Trie does not automatically sort words alphabetically; it stores them by prefix.
What does the boolean flag in a Trie node indicate?
ADepth of the node
BStart of a word
CNumber of children
DEnd of a valid word
✗ Incorrect
The boolean flag marks the end of a valid word in the Trie.
Explain how to use a Trie to find the longest word in a dictionary where all prefixes are also words.
Think about building words step-by-step using prefixes.
You got /3 concepts.
Describe the structure of a Trie node in C and how it supports storing words.
Focus on how each character links to the next.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure when finding the longest word in a dictionary?
easy
A. To sort words alphabetically
B. To count the total number of words
C. To efficiently store and search words by their prefixes
D. To reverse the words in the dictionary
Solution
Step 1: Understand Trie usage
A Trie stores words by breaking them into characters and linking nodes for prefixes.
Step 2: Relate to longest word search
This structure allows quick prefix checks, which helps find the longest word built from other words.
Final Answer:
To efficiently store and search words by their prefixes -> Option C
Quick Check:
Trie = prefix search [OK]
Hint: Trie is best for prefix-based word searches [OK]
Common Mistakes:
Thinking Trie sorts words alphabetically
Confusing Trie with simple arrays or lists
Assuming Trie counts words only
2. Which of the following is the correct way to declare a Trie node structure in C for lowercase English letters?
easy
A. typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode;
B. typedef struct TrieNode { struct TrieNode* children[52]; int isEndOfWord; } TrieNode;
C. typedef struct TrieNode { struct TrieNode* children[10]; int isEndOfWord; } TrieNode;
D. typedef struct TrieNode { struct TrieNode* children; int isEndOfWord; } TrieNode;
Solution
Step 1: Identify children array size
Lowercase English letters are 26, so children array size must be 26.
Step 2: Check structure fields
Each node has an array of pointers and a flag for word end, matching typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode;.
Final Answer:
typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; -> Option A
Quick Check:
26 children for a-z [OK]
Hint: Use 26 children for lowercase letters [OK]
Common Mistakes:
Using 52 for uppercase + lowercase without need
Using 10 children for digits only
Not using array for children pointers
3. Given the following code snippet inserting words into a Trie and searching for the longest word built one character at a time, what will be the output?
char* words[] = {"a", "ap", "app", "appl", "apple", "apply"};
// Assume insert and search functions are correct
// Function finds longest word where all prefixes exist
printf("%s", longestWord(words, 6));
medium
A. apple
B. apply
C. appl
D. app
Solution
Step 1: Check all prefixes for each word
Words like "apple" and "apply" have all prefixes present: a, ap, app, appl.
Step 2: Compare lengths of valid words
"apple" and "apply" both have length 5, but "apple" comes first lexicographically.
Final Answer:
apple -> Option A
Quick Check:
Longest valid word = apple [OK]
Hint: Check all prefixes exist, pick longest lex order first [OK]
Common Mistakes:
Choosing 'apply' ignoring lex order
Picking shorter words like 'app'
Not verifying all prefixes exist
4. The following code tries to find the longest word in a dictionary using a Trie but always returns an empty string. What is the bug?
void insert(TrieNode* root, char* word) {
TrieNode* node = root;
for (int i = 0; word[i]; i++) {
int index = word[i] - 'a';
if (!node->children[index])
node->children[index] = createNode();
node = node->children[index];
}
node->isEndOfWord = 1;
}
char* longestWord(char** words, int n) {
TrieNode* root = createNode();
for (int i = 0; i < n; i++)
insert(root, words[i]);
// Missing code to find longest word
return "";
}
medium
A. The children array size is incorrect
B. The longestWord function does not search the Trie for the longest word
C. The insert function does not mark end of word correctly
D. The root node is not initialized
Solution
Step 1: Analyze insert function
Insert correctly adds words and marks end of word, so no issue here.
Step 2: Check longestWord function
It inserts words but returns empty string without searching Trie for longest word.
Final Answer:
The longestWord function does not search the Trie for the longest word -> Option B
Quick Check:
Missing search logic causes empty output [OK]
Hint: Check if search logic exists after insertion [OK]
Common Mistakes:
Assuming insert alone finds longest word
Ignoring missing traversal in longestWord
Thinking root node is uninitialized
5. You want to find the longest word in a dictionary such that every prefix of the word is also a word in the dictionary. Which approach using a Trie is best to solve this efficiently?
hard
A. Use BFS on Trie without checking end of word flags
B. Sort words by length, then check each word's prefixes in a hash set
C. Insert words into Trie and count total nodes to find longest word
D. Insert all words into Trie, then do DFS checking only nodes marking end of words to build longest word
Solution
Step 1: Insert all words into Trie
Build Trie with all dictionary words, marking end of word nodes.
Step 2: Use DFS to find longest word
Traverse Trie depth-first, only continuing on nodes that mark end of words to ensure all prefixes exist.
Step 3: Track longest word during traversal
Keep updating longest word found by length and lex order during DFS.
Final Answer:
Insert all words into Trie, then do DFS checking only nodes marking end of words to build longest word -> Option D
Quick Check:
Trie + DFS + endOfWord check = longest word [OK]
Hint: Use DFS on Trie nodes with endOfWord true only [OK]
Common Mistakes:
Ignoring prefix checks during traversal
Using BFS without endOfWord validation
Relying on sorting without Trie
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.