Challenge - 5 Problems
Trie Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Trie Node Initialization
What is the output of this Go code that initializes a Trie node and prints its children count?
DSA Go
package main import ( "fmt" ) type TrieNode struct { children map[rune]*TrieNode isEnd bool } func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode), isEnd: false} } func main() { node := NewTrieNode() fmt.Println(len(node.children)) }
Attempts:
2 left
💡 Hint
Think about what happens when you create a new map in Go and check its length before adding any elements.
✗ Incorrect
The NewTrieNode function creates a TrieNode with an empty map for children. The length of an empty map is 0, so printing len(node.children) outputs 0.
❓ Predict Output
intermediate2:00remaining
Trie Node isEnd Field Default Value
What is the output of this Go code that creates a Trie node and prints the isEnd field?
DSA Go
package main import ( "fmt" ) type TrieNode struct { children map[rune]*TrieNode isEnd bool } func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode)} } func main() { node := NewTrieNode() fmt.Println(node.isEnd) }
Attempts:
2 left
💡 Hint
Boolean fields in Go default to what value if not explicitly set?
✗ Incorrect
In Go, boolean fields default to false if not set. The isEnd field is not set in NewTrieNode, so it prints false.
🔧 Debug
advanced2:00remaining
Identify the Error in Trie Node Initialization
What error does this Go code produce when trying to add a child node to the TrieNode's children map?
DSA Go
package main import ( "fmt" ) type TrieNode struct { children map[rune]*TrieNode isEnd bool } func main() { node := &TrieNode{} node.children['a'] = &TrieNode{} fmt.Println(len(node.children)) }
Attempts:
2 left
💡 Hint
Maps must be initialized before adding keys in Go.
✗ Incorrect
The children map is nil because it was not initialized with make. Assigning to a nil map causes a runtime panic.
❓ Predict Output
advanced2:00remaining
Output After Adding Child to Trie Node
What is the output of this Go code after adding a child node to the TrieNode's children map?
DSA Go
package main import ( "fmt" ) type TrieNode struct { children map[rune]*TrieNode isEnd bool } func NewTrieNode() *TrieNode { return &TrieNode{children: make(map[rune]*TrieNode)} } func main() { node := NewTrieNode() node.children['b'] = NewTrieNode() fmt.Println(len(node.children)) }
Attempts:
2 left
💡 Hint
Check if the children map is initialized before adding a child.
✗ Incorrect
The children map is initialized with make, so adding a child increases its length to 1.
🧠 Conceptual
expert2:00remaining
Why Initialize Children Map in Trie Node?
Why is it important to initialize the children map in a TrieNode struct in Go before adding child nodes?
Attempts:
2 left
💡 Hint
Think about what happens when you try to write to a nil map in Go.
✗ Incorrect
Maps in Go default to nil. Writing to a nil map causes a runtime panic. Initializing with make creates a usable map.