Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to traverse the slice forward and print each element.
DSA Go
for i := 0; i [1] len(numbers); i++ { fmt.Println(numbers[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes index out of range error.
Using > or >= will not enter the loop.
✗ Incorrect
The loop should run from 0 up to but not including len(numbers), so the condition is i < len(numbers).
2fill in blank
mediumComplete the code to traverse the slice backward and print each element.
DSA Go
for i := len(numbers) - 1; i [1] 0; i-- { fmt.Println(numbers[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > excludes the first element at index 0.
Using < or <= will not run the loop backward.
✗ Incorrect
To traverse backward, loop from i = len(numbers) - 1 down to 0 inclusive, so the condition is i >= 0.
3fill in blank
hardFix the error in the backward traversal loop condition to include the first element.
DSA Go
for i := len(numbers) - 1; i [1] 0; i-- { fmt.Println(numbers[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > excludes the first element at index 0.
Using < or <= will not traverse backward.
✗ Incorrect
Using i >= 0 ensures the loop includes the first element at index 0.
4fill in blank
hardFill both blanks to create a forward traversal that skips the first element and stops before the last.
DSA Go
for i := [1]; i [2] len(numbers) - 1; i++ { fmt.Println(numbers[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 includes the first element.
Using <= includes the last element.
✗ Incorrect
Start from index 1 to skip the first element, and loop while i is less than len(numbers) - 1 to stop before the last element.
5fill in blank
hardFill all three blanks to create a backward traversal that skips the last element and stops after the first.
DSA Go
for i := [1]; i [2] 0; i [3] { fmt.Println(numbers[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from len(numbers) - 1 includes the last element.
Using >= includes the first element.
Forgetting to decrement i causes infinite loop.
✗ Incorrect
Start from len(numbers) - 2 to skip the last element, loop while i > 0 to stop after the first, and decrement i each iteration.
