Challenge - 5 Problems
Trie Mastery: Longest Word Finder
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Longest Word Found Using Trie
What is the output of the following C++ code that inserts words into a Trie and finds the longest word where all prefixes are present?
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; struct TrieNode { TrieNode* children[26] = {nullptr}; bool isEnd = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const string& word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; } node->isEnd = true; } string longestWord() { string result = ""; dfs(root, "", result); return result; } private: void dfs(TrieNode* node, string current, string& result) { if (!node->isEnd && current != "") return; if (current.length() > result.length() || (current.length() == result.length() && current < result)) result = current; for (int i = 0; i < 26; i++) { if (node->children[i]) { dfs(node->children[i], current + char('a' + i), result); } } } }; int main() { vector<string> words = {"a", "banana", "app", "appl", "ap", "apply", "apple"}; Trie trie; for (auto& w : words) trie.insert(w); cout << trie.longestWord() << endl; return 0; }
Attempts:
2 left
💡 Hint
Check which words have all their prefixes present in the dictionary.
✗ Incorrect
The longest word where all prefixes are present is "apple" because "a", "ap", "app", "appl", and "apple" are all in the list. "apply" also qualifies but has the same length and is lexicographically larger than "apple". "banana" is not valid because prefixes like "b" or "ba" are missing.
🧠 Conceptual
intermediate1:30remaining
Why use Trie for Longest Word in Dictionary?
Why is a Trie data structure suitable for finding the longest word in a dictionary where all prefixes are present?
Attempts:
2 left
💡 Hint
Think about how prefix checking is done.
✗ Incorrect
Trie stores words character by character in a tree, so checking if all prefixes exist is efficient by traversing nodes.
🔧 Debug
advanced2:00remaining
Identify the Bug in Trie Longest Word DFS
What is the bug in the following DFS function used to find the longest word in a Trie where all prefixes are present?
void dfs(TrieNode* node, string current, string& result) {
if (!node->isEnd && current != "") return;
if (current.length() >= result.length())
result = current;
for (int i = 0; i < 26; i++) {
if (node->children[i]) {
dfs(node->children[i], current + char('a' + i), result);
}
}
}
Attempts:
2 left
💡 Hint
Check the condition when updating the result string.
✗ Incorrect
The code updates result when current length is >= result length but does not check lex order if lengths are equal, so it may pick a lexicographically larger word.
❓ Predict Output
advanced2:00remaining
Output of Trie Longest Word with Missing Prefixes
What is the output of this code snippet that inserts words and finds the longest word with all prefixes present?
Words: ["w", "wo", "wor", "worl", "world", "banana"]
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; struct TrieNode { TrieNode* children[26] = {nullptr}; bool isEnd = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const string& word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; } node->isEnd = true; } string longestWord() { string result = ""; dfs(root, "", result); return result; } private: void dfs(TrieNode* node, string current, string& result) { if (!node->isEnd && current != "") return; if (current.length() > result.length() || (current.length() == result.length() && current < result)) result = current; for (int i = 0; i < 26; i++) { if (node->children[i]) { dfs(node->children[i], current + char('a' + i), result); } } } }; int main() { vector<string> words = {"w", "wo", "wor", "worl", "world", "banana"}; Trie trie; for (auto& w : words) trie.insert(w); cout << trie.longestWord() << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if all prefixes of "world" are present.
✗ Incorrect
"world" is the longest word where all prefixes "w", "wo", "wor", "worl" are present. "banana" is not valid because prefixes like "b" are missing.
🚀 Application
expert2:30remaining
Longest Word in Dictionary Using Trie - Algorithm Complexity
What is the time complexity of finding the longest word in a dictionary using a Trie where all prefixes are present, given n words with maximum length m?
Attempts:
2 left
💡 Hint
Consider insertion and traversal costs separately.
✗ Incorrect
Building the Trie takes O(n * m) inserting each character of each word. DFS visits each node once, also O(n * m). Total is O(n * m).