Bird
Raised Fist0
DSA Cprogramming~10 mins

Trie Node Design and Initialization in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a pointer to a Trie node.

DSA C
struct TrieNode* [1];
Drag options to blanks, or click blank then click option'
Ahead
Broot
Ctrie
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that do not represent the root node.
Forgetting to declare a pointer type.
2fill in blank
medium

Complete the code to allocate memory for a new Trie node.

DSA C
struct TrieNode* node = (struct TrieNode*) malloc(sizeof([1]));
Drag options to blanks, or click blank then click option'
ATrieNode
BNode
CTrie
Dstruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' keyword inside sizeof incorrectly.
Using incorrect struct names.
3fill in blank
hard

Fix the error in the code to initialize all children pointers of a Trie node to NULL.

DSA C
for (int i = 0; i < ALPHABET_SIZE; i++) {
    node->children[[1]] = NULL;
}
Drag options to blanks, or click blank then click option'
AALPHABET_SIZE
B0
Cnode
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using a constant or wrong variable as index.
Using the pointer name instead of index.
4fill in blank
hard

Fill both blanks to complete the function that creates and initializes a new Trie node.

DSA C
struct TrieNode* getNode() {
    struct TrieNode* node = (struct TrieNode*) malloc(sizeof([1]));
    node->isEndOfWord = [2];
    for (int i = 0; i < ALPHABET_SIZE; i++) {
        node->children[i] = NULL;
    }
    return node;
}
Drag options to blanks, or click blank then click option'
ATrieNode
B0
C1
DTrie
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong struct name in sizeof.
Setting isEndOfWord to 1 by mistake.
5fill in blank
hard

Fill all three blanks to complete the function that inserts a key into the Trie.

DSA C
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* node = root;
    for (int i = 0; key[i] != '\0'; i++) {
        int index = key[i] - 'a';
        if (node->children[[1]] == NULL) {
            node->children[[2]] = getNode();
        }
        node = node->children[[3]];
    }
    node->isEndOfWord = 1;
}
Drag options to blanks, or click blank then click option'
Aindex
Bi
C0
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using loop variable 'i' instead of 'index' for children array.
Using constant 0 or root pointer incorrectly.

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

  1. Step 1: Understand Trie node role

    A Trie node represents a character and links to children nodes for next characters in strings.
  2. Step 2: Identify correct purpose

    It stores children nodes to build paths for strings, not integer values or lengths.
  3. Final Answer:

    To store children nodes representing characters of strings -> Option D
  4. 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;
D. typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; } TrieNode;

Solution

  1. Step 1: Check children declaration

    Children must be pointers to TrieNode, so use struct TrieNode* children[26];
  2. Step 2: Check end of word flag type

    Using int for boolean is common in C; bool requires stdbool.h and is less common.
  3. Final Answer:

    typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; } TrieNode; -> Option A
  4. 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

  1. Step 1: Analyze getNode initialization

    isEndOfWord is set to 0 explicitly; children array elements set to NULL (0).
  2. Step 2: Check main output

    Printing isEndOfWord prints 0; printing children[0] prints NULL pointer (0x0).
  3. Final Answer:

    0 0x0 (NULL pointer address) -> Option C
  4. 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

  1. 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.
  2. Step 2: Identify correct fix

    Loop should run i < 26 to stay within array bounds and avoid memory errors.
  3. Final Answer:

    Loop condition should be i < 26 to avoid out-of-bounds access -> Option A
  4. 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

  1. Step 1: Check children and end flag

    Children array of pointers size 26 is correct; isEndOfWord as int is standard in C.
  2. Step 2: Add frequency field correctly

    Frequency should be an int to count occurrences; pointer to int is unnecessary complexity.
  3. Final Answer:

    typedef struct TrieNode { struct TrieNode* children[26]; int isEndOfWord; int frequency; } TrieNode; -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard