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 Search for Existing Word
What is the output of the following C code that searches for the word "cat" in a Trie after inserting "cat" and "car"?
DSA C
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 typedef struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; int isEndOfWord; } TrieNode; TrieNode* getNode() { TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); node->isEndOfWord = 0; 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 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; } int search(TrieNode* root, const char* key) { TrieNode* pCrawl = root; for (int level = 0; key[level] != '\0'; level++) { int index = key[level] - 'a'; if (!pCrawl->children[index]) return 0; pCrawl = pCrawl->children[index]; } return (pCrawl != NULL && pCrawl->isEndOfWord); } int main() { TrieNode* root = getNode(); insert(root, "cat"); insert(root, "car"); int result = search(root, "cat"); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check if the search function returns 1 when the word is found.
✗ Incorrect
The word "cat" was inserted, so searching for "cat" returns 1 indicating it exists in the Trie.
❓ Predict Output
intermediateOutput of Trie Search for Non-Existing Word
What is the output of the following C code that searches for the word "cap" in a Trie after inserting "cat" and "car"?
DSA C
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 typedef struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; int isEndOfWord; } TrieNode; TrieNode* getNode() { TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); node->isEndOfWord = 0; 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 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; } int search(TrieNode* root, const char* key) { TrieNode* pCrawl = root; for (int level = 0; key[level] != '\0'; level++) { int index = key[level] - 'a'; if (!pCrawl->children[index]) return 0; pCrawl = pCrawl->children[index]; } return (pCrawl != NULL && pCrawl->isEndOfWord); } int main() { TrieNode* root = getNode(); insert(root, "cat"); insert(root, "car"); int result = search(root, "cap"); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check if the search function returns 0 when the word is not found.
✗ Incorrect
The word "cap" was not inserted, so searching returns 0 indicating it does not exist in the Trie.
🧠 Conceptual
advancedUnderstanding Trie Search Behavior
In a Trie, what does the search operation return if the searched word is a prefix of an inserted word but not marked as a complete word itself?
Attempts:
2 left
💡 Hint
Check the isEndOfWord flag in the Trie node after traversal.
✗ Incorrect
Search returns 0 if the node exists but isEndOfWord is not set, meaning the prefix is not a complete word.
🔧 Debug
advancedIdentify the Bug in Trie Search Function
What error will occur when running this search function if the input word contains uppercase letters?
DSA C
int search(TrieNode* root, const char* key) { TrieNode* pCrawl = root; for (int level = 0; key[level] != '\0'; level++) { int index = key[level] - 'a'; if (!pCrawl->children[index]) return 0; pCrawl = pCrawl->children[index]; } return (pCrawl != NULL && pCrawl->isEndOfWord); }
Attempts:
2 left
💡 Hint
Consider how uppercase letters affect the index calculation.
✗ Incorrect
Uppercase letters have ASCII values less than 'a', causing negative index and out of bounds access.
🚀 Application
expertResulting Trie Node Count After Insertions
After inserting the words "bat", "bath", "baton", and "bar" into an empty Trie, how many Trie nodes are created in total?
Attempts:
2 left
💡 Hint
Count nodes shared by prefixes only once.
✗ Incorrect
The words share prefixes 'bat' and 'ba', nodes are reused. Total nodes created are 8.
