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 Trie Insertion and Search
What will be the output of the following Go code that inserts words into a Trie and searches for a prefix?
DSA Go
package main import "fmt" // TrieNode represents each node in the Trie type TrieNode struct { children map[rune]*TrieNode isEnd bool } // Trie structure type Trie struct { root *TrieNode } // Initialize a new TrieNode func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode)} } // Initialize a new Trie func NewTrie() *Trie { return &Trie{root: NewTrieNode()} } // Insert a word into the Trie 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 } // Search for a prefix in the Trie func (t *Trie) StartsWith(prefix string) bool { node := t.root for _, ch := range prefix { if _, ok := node.children[ch]; !ok { return false } node = node.children[ch] } return true } func main() { trie := NewTrie() trie.Insert("dog") trie.Insert("deer") trie.Insert("deal") fmt.Println(trie.StartsWith("de")) fmt.Println(trie.StartsWith("do")) fmt.Println(trie.StartsWith("cat")) }
Attempts:
2 left
💡 Hint
Think about how the Insert method builds the Trie and how StartsWith checks prefixes.
✗ Incorrect
The words "dog", "deer", and "deal" are inserted. The prefixes "de" and "do" exist in the Trie, so StartsWith returns true for them. "cat" is not inserted, so it 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' boolean indicates that the path from root to this node forms a complete word in the Trie.
🔧 Debug
advanced2:00remaining
Find the Bug in Trie Search Method
What error will the following Go code produce when searching for a prefix in a Trie?
DSA Go
func (t *Trie) StartsWith(prefix string) bool { node := t.root for _, ch := range prefix { if node.children[ch] == nil { return false } node = node.children[ch] } return node.isEnd }
Attempts:
2 left
💡 Hint
Check the return condition and what the method is supposed to do.
✗ Incorrect
The method returns node.isEnd, which means it only returns true if the prefix is a complete word, not just a prefix. This is incorrect for a StartsWith method.
🚀 Application
advanced2:30remaining
Autocomplete Suggestions Using Trie
Given a Trie with words inserted, which approach correctly collects all words starting with a given prefix?
Attempts:
2 left
💡 Hint
Think about how to efficiently find all words starting with a prefix in a Trie.
✗ Incorrect
The correct method is to find the node representing the prefix, then explore all descendant nodes to collect complete words.
❓ Predict Output
expert3:00remaining
Output of Trie Autocomplete Function
What is the output of the following Go code that performs autocomplete suggestions for prefix "ca"?
DSA Go
package main import "fmt" // TrieNode and Trie definitions omitted for brevity // Assume Insert and StartsWith methods exist as before func (t *Trie) collectWords(node *TrieNode, prefix string, results *[]string) { if node.isEnd { *results = append(*results, prefix) } for ch, child := range node.children { t.collectWords(child, prefix+string(ch), results) } } func (t *Trie) Autocomplete(prefix string) []string { node := t.root for _, ch := range prefix { if _, ok := node.children[ch]; !ok { return []string{} } node = node.children[ch] } var results []string t.collectWords(node, prefix, &results) return results } func main() { trie := NewTrie() trie.Insert("cat") trie.Insert("car") trie.Insert("cart") trie.Insert("dog") trie.Insert("dove") suggestions := trie.Autocomplete("ca") fmt.Println(suggestions) }
Attempts:
2 left
💡 Hint
Check which words start with "ca" and how collectWords gathers them.
✗ Incorrect
Words starting with "ca" are "cat", "car", and "cart". The function collects all these words correctly.