What is a Trie node and what does it typically store?
A Trie node is a building block of a Trie data structure. It typically stores an array of pointers to child nodes (one for each possible character) and a boolean flag indicating if the node marks the end of a word.
Click to reveal answer
beginner
How do you initialize a Trie node in C?
You allocate memory for the node, set all child pointers to NULL, and set the end-of-word flag to false (0). This prepares the node to be used in the Trie.
Click to reveal answer
beginner
Why do we set all child pointers to NULL when initializing a Trie node?
Setting child pointers to NULL means the node has no children yet. This helps avoid accessing invalid memory and makes it easy to check if a child exists.
Click to reveal answer
beginner
What does the 'end of word' flag in a Trie node represent?
It indicates whether the path from the root to this node forms a complete word stored in the Trie.
Click to reveal answer
beginner
In a Trie node designed for lowercase English letters, how many child pointers are needed?
26 child pointers, one for each letter from 'a' to 'z'.
Click to reveal answer
What is the first step when initializing a Trie node in C?
AAllocate memory for the node
BSet all child pointers to NULL
CSet the end-of-word flag to false
DInsert the first character
✗ Incorrect
You must first allocate memory for the node before setting its fields.
Why do Trie nodes have multiple child pointers?
ATo point to possible next characters in words
BTo store the length of the word
CTo keep track of word frequency
DTo store multiple words in one node
✗ Incorrect
Each child pointer corresponds to a possible next character in the stored words.
What does a NULL child pointer in a Trie node mean?
AThe child node exists
BThe node is the end of a word
CThe child node does not exist
DThe node is empty
✗ Incorrect
NULL means no child node exists for that character.
What data type is commonly used for the 'end of word' flag in C Trie nodes?
Afloat
Bchar
Cint
Dbool or int
✗ Incorrect
A boolean or int is used to mark if the node ends a word (0 or 1).
How many child pointers does a Trie node have for lowercase English letters?
A52
B26
C10
D128
✗ Incorrect
There are 26 letters in the English alphabet, so 26 child pointers.
Describe how to design and initialize a Trie node in C for storing lowercase English words.
Think about what each node needs to represent and how to prepare it for use.
You got /5 concepts.
Explain why initializing all child pointers to NULL is important in a Trie node.
Consider what happens if pointers are not initialized.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a Trie node in a Trie data structure?
easy
A. To store the length of the string
B. To store integer values for sorting
C. To hold pointers to parent nodes only
D. To store children nodes representing characters of strings
Solution
Step 1: Understand Trie node role
A Trie node represents a character and links to children nodes for next characters in strings.
Step 2: Identify correct purpose
It stores children nodes to build paths for strings, not integer values or lengths.
Final Answer:
To store children nodes representing characters of strings -> Option D
Quick Check:
Trie node = children nodes [OK]
Hint: Trie nodes link characters as children nodes [OK]
Common Mistakes:
Thinking Trie nodes store whole strings
Confusing with storing string length
Assuming nodes only point to parents
2. Which of the following is the correct way to declare a Trie node struct in C with 26 children pointers and a boolean flag for end of word?
easy
A. typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode;
B. typedef struct TrieNode { struct TrieNode children[26]; int isEndOfWord; } TrieNode;
C. typedef struct TrieNode { struct TrieNode* children; int isEndOfWord; } TrieNode;
Children must be pointers to TrieNode, so use struct TrieNode* children[26];
Step 2: Check end of word flag type
Using int for boolean is common in C; bool requires stdbool.h and is less common.
Final Answer:
typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; -> Option A
Quick Check:
Children pointers array + int flag = typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; [OK]
Hint: Use pointers array for children and int for boolean in C [OK]
Common Mistakes:
Declaring children as struct array instead of pointers
Using bool without including stdbool.h
Declaring children as single pointer instead of array
3. Given the following code snippet, what will be the output?
typedef struct TrieNode {
struct TrieNode* children[26];
int isEndOfWord;
} TrieNode;
TrieNode* getNode() {
TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
node->isEndOfWord = 0;
for (int i = 0; i < 26; i++)
node->children[i] = NULL;
return node;
}
int main() {
TrieNode* root = getNode();
printf("%d\n", root->isEndOfWord);
printf("%p\n", root->children[0]);
return 0;
}
medium
A. 1
0x0 (NULL pointer address)
B. Garbage value
Garbage pointer
C. 0
0x0 (NULL pointer address)
D. 0
Non-null pointer address
Solution
Step 1: Analyze getNode initialization
isEndOfWord is set to 0 explicitly; children array elements set to NULL (0).
Step 2: Check main output
Printing isEndOfWord prints 0; printing children[0] prints NULL pointer (0x0).
Final Answer:
0
0x0 (NULL pointer address) -> Option C
Quick Check:
Initialized zero and NULL = 0
0x0 (NULL pointer address) [OK]
Hint: Uninitialized pointers cause garbage; here all set NULL [OK]
Common Mistakes:
Assuming uninitialized pointers have zero value
Confusing isEndOfWord default value
Expecting children pointers to have non-null addresses
4. Identify the error in the following Trie node initialization code and select the fix:
typedef struct TrieNode {
struct TrieNode* children[26];
int isEndOfWord;
} TrieNode;
TrieNode* getNode() {
TrieNode* node = malloc(sizeof(TrieNode));
node->isEndOfWord = 0;
for (int i = 0; i <= 26; i++)
node->children[i] = NULL;
return node;
}
medium
A. Loop condition should be i < 26 to avoid out-of-bounds access
B. Missing cast for malloc return value
C. isEndOfWord should be initialized to 1
D. children array size should be 27
Solution
Step 1: Check loop bounds
Loop runs from i = 0 to i <= 26, which means 27 iterations, but children array size is 26, causing out-of-bounds access.
Step 2: Identify correct fix
Loop should run i < 26 to stay within array bounds and avoid memory errors.
Final Answer:
Loop condition should be i < 26 to avoid out-of-bounds access -> Option A
Quick Check:
Array size 26 means max index 25 [OK]
Hint: Array size 26 means loop i < 26, not <= 26 [OK]
Common Mistakes:
Using <= instead of < in loops
Ignoring array bounds causing runtime errors
Assuming malloc cast is mandatory in C
5. You want to initialize a Trie node that supports lowercase English letters and also store the frequency of words ending at that node. Which of the following struct definitions correctly extends the basic Trie node design?
hard
A. typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; int frequency; } TrieNode;
B. typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; int frequency; } TrieNode;
C. typedef struct TrieNode { struct TrieNode* children[26]; int frequency; } TrieNode;
D. typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; int* frequency; } TrieNode;
Solution
Step 1: Check children and end flag
Children array of pointers size 26 is correct; isEndOfWord as int is standard in C.
Step 2: Add frequency field correctly
Frequency should be an int to count occurrences; pointer to int is unnecessary complexity.
Final Answer:
typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; int frequency; } TrieNode; -> Option B
Quick Check:
Use int for frequency and isEndOfWord [OK]
Hint: Add int frequency field directly, keep isEndOfWord as int [OK]
Common Mistakes:
Using pointer for frequency unnecessarily
Omitting isEndOfWord flag
Using bool without stdbool.h
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.