Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Word Found Using Trie
What is the output of the following code that builds a trie from a list of words and finds the longest word where all prefixes are also words?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_word = True def longest_word(self): result = "" stack = [(self.root, "")] while stack: node, path = stack.pop() if node.is_word or node == self.root: if len(path) > len(result) or (len(path) == len(result) and path < result): result = path for char in sorted(node.children.keys(), reverse=True): stack.append((node.children[char], path + char)) return result words = ["a", "app", "ap", "apply", "apple", "appl"] trie = Trie() for w in words: trie.insert(w) print(trie.longest_word())
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list.
✗ Incorrect
The longest word where all prefixes are words is "apple" because "a", "ap", "app", "appl", "apple" are all present. Both "apple" and "apply" qualify since all their prefixes are words, but "apple" is lexicographically smaller.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the role of the 'is_word' attribute in a trie node when finding the longest word in a dictionary?
Attempts:
2 left
💡 Hint
Think about how to know if a prefix is a valid word.
✗ Incorrect
The 'is_word' attribute is True if the path from the root to this node forms a complete word in the dictionary. This helps to check if all prefixes exist when searching for the longest word.
🔧 Debug
advancedIdentify the Bug in Trie Longest Word Search
The following code attempts to find the longest word in a dictionary using a trie, but it returns an empty string. What is the bug?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_word = True def longest_word(self): result = "" stack = [(self.root, "")] while stack: node, path = stack.pop() if node.is_word or node == self.root: if len(path) > len(result) or (len(path) == len(result) and path < result): result = path for char in sorted(node.children.keys(), reverse=True): stack.append((node.children[char], path + char)) return result words = ["a", "ap", "app", "apple", "apply"] trie = Trie() for w in words: trie.insert(w) print(trie.longest_word())
Attempts:
2 left
💡 Hint
Check the condition that allows traversal from the root node.
✗ Incorrect
The root node is not marked as a word, so the search never starts from it. The condition 'if node.is_word' skips the root node, which prevents any traversal and results in an empty string.
🚀 Application
advancedLongest Word with All Prefixes Present
Given the list of words ["w", "wo", "wor", "worl", "world", "banana", "ban", "bana", "banan"], which word will be returned by a trie-based algorithm that finds the longest word where every prefix is also a word?
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list.
✗ Incorrect
"world" is the longest word where all prefixes ("w", "wo", "wor", "worl", "world") are present. "banana" is not valid because prefixes like "b" and "ba" are not present in the list.
🧠 Conceptual
expertWhy Use DFS with Stack in Trie Longest Word Search?
Why does the trie-based longest word search algorithm use a stack and depth-first search (DFS) instead of breadth-first search (BFS)?
Attempts:
2 left
💡 Hint
Consider how the algorithm prioritizes longer words and lex order.
✗ Incorrect
Using DFS with a stack and sorting children in reverse lexicographical order allows the algorithm to explore longer words first and update the result accordingly. This ensures the longest word with the smallest lexicographical order is found.
