Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a TrieNode struct with a children map.
DSA Go
type TrieNode struct {
children map[rune]*[1]
isEnd bool
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different struct name for children map values.
Not using pointers for child nodes.
✗ Incorrect
The children map should hold pointers to TrieNode structs to represent child nodes.
2fill in blank
mediumComplete the code to initialize a new TrieNode with an empty children map.
DSA Go
func NewTrieNode() *TrieNode {
return &TrieNode{
children: make(map[rune]*[1]),
isEnd: false,
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using make with wrong type.
Not initializing the map, causing nil map errors.
✗ Incorrect
The children map must be initialized with make for the TrieNode type.
3fill in blank
hardFix the error in the code to add a child node for a character key.
DSA Go
func (node *TrieNode) AddChild(ch rune) {
if node.children == nil {
node.children = make(map[rune]*[1])
}
node.children[ch] = NewTrieNode()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect struct name in map initialization.
Not initializing the map before adding children.
✗ Incorrect
The children map must hold pointers to TrieNode structs.
4fill in blank
hardFill both blanks to check if a child node exists and return it.
DSA Go
func (node *TrieNode) GetChild(ch rune) (*TrieNode, bool) {
child, [1] := node.children[ch]
return child, [2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names other than 'ok' for the comma-ok idiom.
Returning the wrong variable for existence check.
✗ Incorrect
The comma-ok idiom uses 'ok' to check if a key exists in a map.
5fill in blank
hardFill all three blanks to insert a word into the trie.
DSA Go
func (node *TrieNode) Insert(word string) {
current := node
for _, ch := range word {
child, [1] := current.children[ch]
if ![2] {
child = NewTrieNode()
current.children[ch] = [3]
}
current = child
}
current.isEnd = true
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for existence check.
Not assigning the new node to the children map.
✗ Incorrect
Use 'ok' for existence check and assign the new node to current.children[ch].