Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the length counter to zero.
DSA Go
func getLength(head *Node) int {
length := [1]
current := head
for current != nil {
length++
current = current.next
}
return length
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting length at 1 causes incorrect counts for empty lists.
Using nil as initial length causes a type error.
✗ Incorrect
The length counter should start at 0 because the list might be empty, so we count nodes as we traverse.
2fill in blank
mediumComplete the code to move to the next node in the linked list.
DSA Go
func getLength(head *Node) int {
length := 0
current := head
for current != nil {
length++
current = [1]
}
return length
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.next inside the loop causes infinite loop or wrong traversal.
Using length.next is invalid because length is an int.
✗ Incorrect
To traverse the list, update current to current.next to move to the next node.
3fill in blank
hardFix the error in the loop condition to correctly check the end of the linked list.
DSA Go
func getLength(head *Node) int {
length := 0
current := head
for [1] {
length++
current = current.next
}
return length
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current == nil causes the loop to never run.
Using length < 10 limits counting to 10 nodes incorrectly.
✗ Incorrect
The loop should continue while current is not nil, meaning there are nodes left to count.
4fill in blank
hardFill both blanks to create a map of node values to their positions in the list.
DSA Go
func mapValues(head *Node) map[int]int {
result := make(map[int]int)
current := head
pos := 0
for current != nil {
result[[1]] = [2]
current = current.next
pos++
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current.pos is invalid because nodes don't have a pos field.
Using value alone is undefined.
✗ Incorrect
We use current.value as the key and pos as the value to map node values to their positions.
5fill in blank
hardFill all three blanks to create a slice of node values from the linked list.
DSA Go
func listValues(head *Node) []int {
values := []int{}
current := head
for current != nil {
values = append(values, [1])
current = [2]
}
return [3]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning head instead of values returns the wrong data.
Using current instead of current.next causes infinite loop.
✗ Incorrect
Append current.value to values, move current to current.next, and return values at the end.
