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 Insertion and Search
What is the output of the following C code that inserts words into a Trie and searches for a word?
DSA C
/* Trie node structure */ #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, "apple"); insert(root, "app"); insert(root, "bat"); printf("%d\n", search(root, "app")); return 0; }
Attempts:
2 left
💡 Hint
Check if the word 'app' was inserted and if the search function returns 1 for found words.
✗ Incorrect
The word 'app' was inserted into the Trie, so searching for 'app' returns 1 indicating it is found.
🧠 Conceptual
intermediateTrie Node Children Count
In a Trie node for lowercase English letters, how many children pointers does each node have?
Attempts:
2 left
💡 Hint
Consider the number of lowercase English letters.
✗ Incorrect
Each Trie node has 26 children pointers, one for each lowercase English letter from 'a' to 'z'.
❓ Predict Output
advancedOutput of Trie Search for Non-Inserted Word
What is the output of this C code when searching for the word 'batman' after inserting 'bat' and 'ball'?
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, "bat"); insert(root, "ball"); printf("%d\n", search(root, "batman")); return 0; }
Attempts:
2 left
💡 Hint
Check if 'batman' was inserted or not.
✗ Incorrect
The word 'batman' was not inserted, so the search returns 0 indicating not found.
🔧 Debug
advancedIdentify the Bug in Trie Search Function
What error will this Trie search function cause when searching for a word?
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] == NULL) return 0; pCrawl = pCrawl->children[index]; } return pCrawl->isEndOfWord; } // Assume root is NULL and search is called with root and "test"
Attempts:
2 left
💡 Hint
What happens if root is NULL and you try to access its children?
✗ Incorrect
If root is NULL, accessing pCrawl->children causes a segmentation fault (crash) because you dereference a NULL pointer.
🚀 Application
expertNumber of Words Stored in Trie
Given a Trie where the following words are inserted: "cat", "car", "cart", "dog", "dot", what is the total number of words stored in the Trie?
Attempts:
2 left
💡 Hint
Count each inserted word as one stored word.
✗ Incorrect
Each inserted word counts as one stored word regardless of shared prefixes.
