Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a node struct for a tree with a value and children.
DSA Go
type Node struct {
Value int
Children [][1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or string instead of Node for Children slice.
Forgetting to use a slice type for multiple children.
✗ Incorrect
The Children field should be a slice of Node structs to represent child nodes in the tree.
2fill in blank
mediumComplete the code to append a child node to a parent node's Children slice.
DSA Go
parent.Children = append(parent.Children, [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the parent node to its own children.
Appending the Children slice instead of a node.
✗ Incorrect
We append the child node variable to the parent's Children slice.
3fill in blank
hardFix the error in this function that prints all node values in a tree recursively.
DSA Go
func printTree(node *Node) {
if node == nil {
return
}
fmt.Println(node.Value)
for _, child := range node.[1] {
printTree(child)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field name for children.
Trying to iterate over a non-slice field.
✗ Incorrect
We must iterate over the Children slice to visit each child node.
4fill in blank
hardFill both blanks to create a linked list node struct with a value and pointer to next node.
DSA Go
type ListNode struct {
Value int
[1] *[2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Child instead of Next for linked list.
Using Node instead of ListNode for pointer type.
✗ Incorrect
The linked list node has a Next pointer to another ListNode struct.
5fill in blank
hardFill all three blanks to create a map from string keys to int values for counting frequencies.
DSA Go
freq := map[[1]][2]{} for _, word := range words { freq[word][3] freq[word] + 1 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using float64 instead of int for counts.
Using wrong operator like += instead of =.
✗ Incorrect
The map keys are strings, values are ints, and we assign updated counts with =.