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 Longest Word Search
What is the output of the following C code that inserts words into a Trie and finds the longest word where every prefix is also a word?
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 isValidPrefix(TrieNode* root, const char* word, int length) { if (!root) return 0; TrieNode* pCrawl = root; for (int i = 0; i < length; i++) { int index = word[i] - 'a'; pCrawl = pCrawl->children[index]; if (!pCrawl || !pCrawl->isEndOfWord) return 0; } return 1; } void findLongestWordUtil(TrieNode* root, char words[][20], int n, char* longestWord) { for (int i = 0; i < n; i++) { int len = strlen(words[i]); if (len > strlen(longestWord) && isValidPrefix(root, words[i], len)) { strcpy(longestWord, words[i]); } } } int main() { char words[][20] = {"a", "app", "ap", "apply", "apple"}; int n = sizeof(words) / sizeof(words[0]); TrieNode* root = getNode(); for (int i = 0; i < n; i++) { insert(root, words[i]); } char longestWord[20] = ""; findLongestWordUtil(root, words, n, longestWord); printf("%s\n", longestWord); return 0; }
Attempts:
2 left
💡 Hint
Check which word has all prefixes present as words in the Trie and is the longest.
✗ Incorrect
The code inserts all words into the Trie. Then it checks each word to see if all its prefixes are present as words (isEndOfWord == 1). Among these, it picks the longest word. "apple" and "apply" fail because the prefix "appl" is not a word. "app" has prefixes "a", "ap", "app" all present. Thus, the output is "app".
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the purpose of the 'isEndOfWord' flag in a Trie node when finding the longest word in a dictionary?
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word ends.
✗ Incorrect
The 'isEndOfWord' flag is set to 1 when a node represents the last character of a valid word. This helps in identifying complete words during traversal.
🔧 Debug
advancedIdentify the Bug in Longest Word Search
Given the following snippet from a Trie-based longest word finder, what is the bug that causes incorrect results?
int isValidPrefix(TrieNode* root, const char* word, int length) {
TrieNode* pCrawl = root;
for (int i = 0; i < length; i++) {
int index = word[i] - 'a';
pCrawl = pCrawl->children[index];
if (!pCrawl || !pCrawl->isEndOfWord)
return 0;
}
return 1;
}
The function is used to check if all prefixes of 'word' are valid words in the Trie.
Attempts:
2 left
💡 Hint
Consider what prefixes mean compared to the full word.
✗ Incorrect
The function does not check if root is NULL. If root is NULL, the first access pCrawl->children[index] dereferences NULL, causing a segmentation fault.
🚀 Application
advancedLongest Word with All Prefixes Present
You have a list of words: ["w", "wo", "wor", "worl", "world", "word", "worst"]. Using a Trie, which word will be identified as the longest word where every prefix is also a word?
Attempts:
2 left
💡 Hint
Check each word's prefixes and see if all are present in the list.
✗ Incorrect
"world" has prefixes "w", "wo", "wor", "worl" all present in the list. "worst" fails because "wors" is not in the list. "word" is valid but shorter than "world". "worl" is shorter than "world". So "world" is the longest valid word.
🧠 Conceptual
expertWhy Use Trie for Longest Word in Dictionary?
Why is a Trie data structure preferred over a hash set for finding the longest word in a dictionary where every prefix is also a word?
Attempts:
2 left
💡 Hint
Think about how prefix checking is done in both data structures.
✗ Incorrect
Trie stores words in a tree structure where each node represents a character, making prefix checks fast by traversing nodes. Hash sets require checking each prefix substring separately, which is less efficient.
