Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of nested loop array traversal
What is the output of the following Go code that traverses a 2D array?
DSA Go
package main import "fmt" func main() { arr := [][]int{{1, 2}, {3, 4}} for i := 0; i < len(arr); i++ { for j := 0; j < len(arr[i]); j++ { fmt.Print(arr[i][j], " ") } } }
Attempts:
2 left
💡 Hint
Think about how nested loops iterate row by row and column by column.
✗ Incorrect
The outer loop goes through each row, and the inner loop goes through each element in that row. So the elements print in order: 1, 2, then 3, 4.
❓ Predict Output
intermediateOutput of reverse traversal of an array
What is the output of this Go code that traverses an array backwards?
DSA Go
package main import "fmt" func main() { arr := []int{5, 10, 15, 20} for i := len(arr) - 1; i >= 0; i-- { fmt.Print(arr[i], " ") } }
Attempts:
2 left
💡 Hint
The loop starts from the last index and moves to the first.
✗ Incorrect
The loop starts at the last element (index 3) and moves backward to index 0, printing elements in reverse order.
🔧 Debug
advancedIdentify the runtime error in array traversal
What error will this Go code produce when run?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3} for i := 0; i <= len(arr); i++ { fmt.Print(arr[i], " ") } }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs from 0 to len(arr) inclusive, but valid indices are 0 to len(arr)-1. Accessing arr[len(arr)] causes an index out of range runtime error.
🧠 Conceptual
advancedUnderstanding stride in array traversal
In Go, what will be the output of this code that traverses an array with a step of 2?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30, 40, 50} for i := 0; i < len(arr); i += 2 { fmt.Print(arr[i], " ") } }
Attempts:
2 left
💡 Hint
The loop increments index by 2 each time, skipping every other element.
✗ Incorrect
The loop prints elements at indices 0, 2, and 4, which are 10, 30, and 50 respectively.
🚀 Application
expertFind the sum of elements at even indices
What is the value of variable sum after running this Go code that sums elements at even indices?
DSA Go
package main import "fmt" func main() { arr := []int{3, 6, 9, 12, 15, 18} sum := 0 for i := 0; i < len(arr); i++ { if i%2 == 0 { sum += arr[i] } } fmt.Print(sum) }
Attempts:
2 left
💡 Hint
Add elements at indices 0, 2, and 4 only.
✗ Incorrect
Elements at even indices are arr[0]=3, arr[2]=9, arr[4]=15. Their sum is 3+9+15=27.
