Bird
Raised Fist0
DSA Cprogramming~10 mins

Trie Insert Operation 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 create a new Trie node.

DSA C
struct TrieNode* getNode() {
    struct TrieNode* pNode = (struct TrieNode*)malloc(sizeof(struct TrieNode));
    pNode->isEndOfWord = [1];
    for (int i = 0; i < 26; i++)
        pNode->children[i] = NULL;
    return pNode;
}
Drag options to blanks, or click blank then click option'
A0
B-1
C1
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to 1 initially, which incorrectly marks the node as end of a word.
2fill in blank
medium

Complete the code to move to the correct child node for the current character.

DSA C
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* pCrawl = root;
    for (int level = 0; key[level] != '\0'; level++) {
        int index = key[level] - 'a';
        if (!pCrawl->children[[1]])
            pCrawl->children[index] = getNode();
        pCrawl = pCrawl->children[index];
    }
    pCrawl->isEndOfWord = 1;
}
Drag options to blanks, or click blank then click option'
Alevel
Bindex
Ckey[level]
DpCrawl
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop variable 'level' directly as index.
Using the character 'key[level]' instead of its index.
3fill in blank
hard

Fix the error in the insert function to correctly mark the end of the word.

DSA C
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* pCrawl = root;
    for (int level = 0; key[level] != '\0'; level++) {
        int index = key[level] - 'a';
        if (!pCrawl->children[index])
            pCrawl->children[index] = getNode();
        pCrawl = pCrawl->children[index];
    }
    pCrawl->isEndOfWord = [1];
}
Drag options to blanks, or click blank then click option'
A0
B-1
C1
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to 0, which means the node is not end of a word.
4fill in blank
hard

Fill both blanks to complete the insert function that adds a word to the Trie.

DSA C
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* pCrawl = root;
    for (int [1] = 0; key[[2]] != '\0'; [1]++) {
        int index = key[[1]] - 'a';
        if (!pCrawl->children[index])
            pCrawl->children[index] = getNode();
        pCrawl = pCrawl->children[index];
    }
    pCrawl->isEndOfWord = 1;
}
Drag options to blanks, or click blank then click option'
Alevel
Bi
Cj
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop declaration and condition.
Using an undeclared variable in the condition.
5fill in blank
hard

Fill all three blanks to complete the insert function with correct indexing and marking end of word.

DSA C
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* pCrawl = root;
    for (int [1] = 0; key[[2]] != '\0'; [1]++) {
        int index = key[[3]] - 'a';
        if (!pCrawl->children[index])
            pCrawl->children[index] = getNode();
        pCrawl = pCrawl->children[index];
    }
    pCrawl->isEndOfWord = 1;
}
Drag options to blanks, or click blank then click option'
Alevel
Bi
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing different variable names for indexing inside the loop.

Practice

(1/5)
1. What is the main purpose of the insert operation in a Trie data structure?
easy
A. To add a word character by character into the Trie
B. To delete a word from the Trie
C. To search for a word in the Trie
D. To count the number of words in the Trie

Solution

  1. Step 1: Understand the insert operation

    The insert operation adds each character of a word into the Trie, creating nodes if they don't exist.
  2. Step 2: Differentiate from other operations

    Deleting removes words, searching checks presence, counting totals words. Insert specifically adds words.
  3. Final Answer:

    To add a word character by character into the Trie -> Option A
  4. Quick Check:

    Insert = Add word [OK]
Hint: Insert means adding word letters step-by-step [OK]
Common Mistakes:
  • Confusing insert with search or delete
  • Thinking insert counts words
  • Assuming insert removes words
2. Which of the following is the correct way to declare a Trie node structure in C for insertion?
easy
A. typedef struct TrieNode { char* word; struct TrieNode* children[26]; } TrieNode;
B. typedef struct TrieNode { int data; struct TrieNode* next; } TrieNode;
C. typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; } TrieNode;
D. typedef struct TrieNode { struct TrieNode* left; struct TrieNode* right; } TrieNode;

Solution

  1. Step 1: Identify Trie node components

    A Trie node has an array of pointers for children (26 letters) and a boolean to mark word end.
  2. Step 2: Match with options

    typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; } TrieNode; matches this structure. Others describe linked list or binary tree nodes, not Trie nodes.
  3. Final Answer:

    typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; } TrieNode; -> Option C
  4. Quick Check:

    Trie node = children array + end flag [OK]
Hint: Trie nodes have 26 children pointers and end-of-word flag [OK]
Common Mistakes:
  • Using linked list node structure instead of array
  • Confusing Trie with binary tree node
  • Missing end-of-word boolean
3. What will be the output of the following C code snippet after inserting the word "cat" into an empty Trie and checking the isEndOfWord flag at the last node?
TrieNode* root = createNode();
insert(root, "cat");
bool flag = root->children['c' - 'a']->children['a' - 'a']->children['t' - 'a']->isEndOfWord;
printf("%d", flag);
medium
A. 0
B. Runtime error
C. Compilation error
D. 1

Solution

  1. Step 1: Insert "cat" into Trie

    Each character creates or uses existing nodes. The last character 't' node's isEndOfWord is set to true (1).
  2. Step 2: Check isEndOfWord flag

    Accessing the node for 't' and printing isEndOfWord prints 1 because the word ends there.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    Inserted word end flag = 1 [OK]
Hint: End node flag is set to true after insert [OK]
Common Mistakes:
  • Assuming isEndOfWord is 0 after insert
  • Indexing children incorrectly
  • Forgetting to set isEndOfWord during insert
4. Consider the following buggy insert function for a Trie in C:
void insert(TrieNode* root, const 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 = true;
}
What is the main bug in this code?
medium
A. New nodes are not initialized, leading to garbage values
B. The loop condition is incorrect and may cause infinite loop
C. The isEndOfWord flag is set too early
D. The root pointer is not updated after insertion

Solution

  1. Step 1: Analyze node allocation

    New nodes are allocated with malloc but not initialized, so children pointers and isEndOfWord have garbage values.
  2. Step 2: Consequences of uninitialized nodes

    Uninitialized children can cause incorrect behavior or crashes during further insert/search operations.
  3. Final Answer:

    New nodes are not initialized, leading to garbage values -> Option A
  4. Quick Check:

    Missing initialization = bug [OK]
Hint: Always initialize new Trie nodes after malloc [OK]
Common Mistakes:
  • Ignoring initialization of new nodes
  • Assuming malloc zeros memory
  • Confusing loop condition correctness
5. You want to insert multiple words into a Trie and then count how many words start with the prefix "pre". Which modification to the Trie insert operation helps achieve this efficiently?
hard
A. Store the entire word at each node instead of characters
B. Add a counter in each node that increments every time a word passes through it during insertion
C. Only mark isEndOfWord without any counters
D. Use a linked list instead of an array for children nodes

Solution

  1. Step 1: Understand prefix counting

    To count words with a prefix, each node can keep a count of how many words pass through it.
  2. Step 2: Modify insert to update counters

    During insertion, increment the counter at each node visited. Then prefix count is the counter at prefix's last node.
  3. Final Answer:

    Add a counter in each node that increments every time a word passes through it during insertion -> Option B
  4. Quick Check:

    Prefix count = node counters [OK]
Hint: Use counters in nodes to track prefix occurrences [OK]
Common Mistakes:
  • Not using counters, making prefix count slow
  • Storing full words at nodes wastes space
  • Using linked list slows access
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