Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Word Search in Trie after Insertions
What is the output of the following Go code that inserts words into a Trie and searches for a specific word?
DSA Go
package main import "fmt" type TrieNode struct { children map[rune]*TrieNode isEnd bool } type Trie struct { root *TrieNode } func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode)} } func NewTrie() *Trie { return &Trie{root: NewTrieNode()} } func (t *Trie) Insert(word string) { node := t.root for _, ch := range word { if _, ok := node.children[ch]; !ok { node.children[ch] = NewTrieNode() } node = node.children[ch] } node.isEnd = true } func (t *Trie) Search(word string) bool { node := t.root for _, ch := range word { if _, ok := node.children[ch]; !ok { return false } node = node.children[ch] } return node.isEnd } func main() { trie := NewTrie() trie.Insert("go") trie.Insert("gone") trie.Insert("gopher") fmt.Println(trie.Search("go")) fmt.Println(trie.Search("gone")) fmt.Println(trie.Search("goph")) }
Attempts:
2 left
💡 Hint
Remember that Search returns true only if the word is fully present and marked as an end.
✗ Incorrect
The words "go" and "gone" are inserted fully and marked as end nodes, so searching them returns true. "goph" is a prefix but not a complete word inserted, so search returns false.
🧠 Conceptual
intermediate1:30remaining
Understanding Trie Node Structure
Which of the following best describes the role of the 'isEnd' boolean in a Trie node?
Attempts:
2 left
💡 Hint
Think about how the Trie distinguishes between prefixes and complete words.
✗ Incorrect
The 'isEnd' flag marks that the path to this node forms a complete word inserted into the Trie.
🔧 Debug
advanced2:00remaining
Identify the Bug in Trie Search Method
What error will the following Go code produce when searching for a word not in the Trie?
DSA Go
func (t *Trie) Search(word string) bool { node := t.root for _, ch := range word { node = node.children[ch] } return node.isEnd }
Attempts:
2 left
💡 Hint
What happens if the character is not found in children map?
✗ Incorrect
The code does not check if the child node exists before accessing it, so accessing a missing key returns nil and causes a runtime panic.
🚀 Application
advanced1:00remaining
Number of Words Stored in Trie
Given a Trie with the words inserted: ["cat", "car", "cart", "dog", "dot"], how many words does the Trie contain?
Attempts:
2 left
💡 Hint
Count each unique complete word inserted.
✗ Incorrect
All five words are distinct and inserted fully, so the Trie contains 5 words.
❓ Predict Output
expert2:30remaining
Output of Trie Search with Prefixes and Words
What is the output of this Go program that inserts words and searches for prefixes and words?
DSA Go
package main import "fmt" type TrieNode struct { children map[rune]*TrieNode isEnd bool } type Trie struct { root *TrieNode } func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode)} } func NewTrie() *Trie { return &Trie{root: NewTrieNode()} } func (t *Trie) Insert(word string) { node := t.root for _, ch := range word { if _, ok := node.children[ch]; !ok { node.children[ch] = NewTrieNode() } node = node.children[ch] } node.isEnd = true } func (t *Trie) Search(word string) bool { node := t.root for _, ch := range word { if _, ok := node.children[ch]; !ok { return false } node = node.children[ch] } return node.isEnd } func main() { trie := NewTrie() trie.Insert("apple") trie.Insert("app") trie.Insert("application") fmt.Println(trie.Search("app")) fmt.Println(trie.Search("apple")) fmt.Println(trie.Search("appl")) fmt.Println(trie.Search("application")) }
Attempts:
2 left
💡 Hint
Only words fully inserted and marked as end return true.
✗ Incorrect
"app", "apple", and "application" are inserted words. "appl" is a prefix but not a complete word, so search returns false for it.