Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Node Initialization
What is the output of this C code that initializes a Trie node and prints its initial state?
DSA C
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ALPHABET_SIZE 26 typedef struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; } TrieNode; TrieNode* getNode() { TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } int main() { TrieNode* root = getNode(); int count = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i] == NULL) { count++; } } printf("isEndOfWord: %d, NULL children count: %d\n", root->isEndOfWord, count); free(root); return 0; }
Attempts:
2 left
💡 Hint
Check how the node's isEndOfWord is initialized and how children pointers are set.
✗ Incorrect
The Trie node is initialized with isEndOfWord set to false (0) and all children pointers set to NULL. Since ALPHABET_SIZE is 26, all children are NULL, so count is 26.
🧠 Conceptual
intermediateUnderstanding Trie Node Children Array
In a Trie node design in C, why do we use an array of pointers of size 26 for children?
Attempts:
2 left
💡 Hint
Think about how Trie nodes represent letters in words.
✗ Incorrect
Each index in the children array corresponds to a letter 'a' to 'z'. This allows quick access to child nodes representing each letter.
🔧 Debug
advancedIdentify the Bug in Trie Node Initialization
What error will this C code cause when initializing a Trie node?
DSA C
#include <stdlib.h> #include <stdbool.h> #define ALPHABET_SIZE 26 typedef struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; } TrieNode; TrieNode* getNode() { TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for children initialization.
✗ Incorrect
The loop runs from 0 to ALPHABET_SIZE inclusive, but children array indices go from 0 to ALPHABET_SIZE-1. Accessing children[26] causes buffer overflow.
❓ Predict Output
advancedOutput After Inserting a Word in Trie
What is the output of this C code after inserting the word "cat" into the Trie and checking the isEndOfWord flag?
DSA C
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ALPHABET_SIZE 26 typedef struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; } TrieNode; TrieNode* getNode() { TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } void insert(TrieNode* root, const char* key) { TrieNode* pCrawl = root; for (int i = 0; key[i] != '\0'; i++) { int index = key[i] - 'a'; if (!pCrawl->children[index]) { pCrawl->children[index] = getNode(); } pCrawl = pCrawl->children[index]; } pCrawl->isEndOfWord = true; } int main() { TrieNode* root = getNode(); insert(root, "cat"); printf("isEndOfWord at 't' node: %d\n", root->children['c' - 'a']->children['a' - 'a']->children['t' - 'a']->isEndOfWord); free(root); return 0; }
Attempts:
2 left
💡 Hint
Check how the insert function sets the isEndOfWord flag at the last character node.
✗ Incorrect
The insert function sets isEndOfWord to true at the node representing 't' in "cat". So printing it outputs 1.
🧠 Conceptual
expertMemory Usage Optimization in Trie Node Design
Which approach best reduces memory usage in a Trie node design for English lowercase letters while maintaining efficient search?
Attempts:
2 left
💡 Hint
Think about storing only necessary children to save space.
✗ Incorrect
Using a hash map or dictionary stores only existing children, reducing memory compared to fixed arrays with many NULLs, while keeping search efficient.
