0
0
DSA C++programming~20 mins

Longest Word in Dictionary Using Trie in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery: Longest Word Finder
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A"banana"
B"apply"
C"apple"
D"app"
Attempts:
2 left
💡 Hint
Check which words have all their prefixes present in the dictionary.
🧠 Conceptual
intermediate
1: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?
ABecause Trie stores words in a tree structure allowing prefix checks efficiently.
BBecause Trie sorts words alphabetically automatically.
CBecause Trie uses hashing to find words quickly.
DBecause Trie compresses words to save memory.
Attempts:
2 left
💡 Hint
Think about how prefix checking is done.
🔧 Debug
advanced
2: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); } } }
AIt should return immediately when node->isEnd is true.
BIt does not check lexicographical order when lengths are equal, so result may not be lex smallest.
CIt should not add characters to current string during recursion.
DIt should use BFS instead of DFS for correct result.
Attempts:
2 left
💡 Hint
Check the condition when updating the result string.
Predict Output
advanced
2: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;
}
A"world"
B"banana"
C"worl"
D"wor"
Attempts:
2 left
💡 Hint
Check if all prefixes of "world" are present.
🚀 Application
expert
2: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?
AO(n * log n) due to sorting words before insertion
BO(n^2) because each word is compared with all others
CO(m^2) because DFS explores all prefixes repeatedly
DO(n * m) for building the Trie + O(n * m) for DFS traversal, total O(n * m)
Attempts:
2 left
💡 Hint
Consider insertion and traversal costs separately.