Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print all elements of the array using a for loop.
DSA Go
arr := []int{1, 2, 3, 4, 5}
for [1] := 0; i < len(arr); i++ {
fmt.Println(arr[i])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not declared in the loop.
Using a variable that does not match the loop condition.
✗ Incorrect
The variable 'i' is commonly used as the index in for loops to traverse arrays.
2fill in blank
mediumComplete the code to traverse the array backwards and print each element.
DSA Go
arr := []int{10, 20, 30, 40, 50}
for i := len(arr) - 1; [1] >= 0; i-- {
fmt.Println(arr[i])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable in the condition than in the loop declaration.
Incorrect loop condition causing infinite loops.
✗ Incorrect
The loop variable 'i' is used to traverse the array backwards starting from the last index.
3fill in blank
hardFix the error in the code to correctly print every second element of the array.
DSA Go
arr := []int{5, 10, 15, 20, 25, 30}
for i := 0; i < len(arr); [1] {
fmt.Println(arr[i])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i++' which increments by 1, not 2.
Using decrement which causes infinite loop.
✗ Incorrect
To print every second element, increment the index by 2 each time.
4fill in blank
hardFill both blanks to create a loop that prints elements at even indices only.
DSA Go
arr := []int{2, 4, 6, 8, 10, 12}
for [1] := 0; i < len(arr); [2] {
fmt.Println(arr[i])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i++' which prints all elements, not just even indices.
Using a different variable name in the loop.
✗ Incorrect
Start with 'i' at 0 and increment by 2 to access even indices.
5fill in blank
hardFill all three blanks to create a loop that prints elements at odd indices only.
DSA Go
arr := []int{3, 6, 9, 12, 15, 18}
for [1] := 1; [2] < len(arr); [3] {
fmt.Println(arr[i])
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 instead of 1.
Incrementing by 1 instead of 2.
✗ Incorrect
Start 'i' at 1, loop while 'i' is less than length, increment by 2 to get odd indices.
