Bird
Raised Fist0
DSA Goprogramming~10 mins

Array Traversal Patterns in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Traversal Patterns
Start at index 0
↓
Check: index < length?
No→End traversal
Yes↓
Access element at index
↓
Process element
↓
Increment index
↩Back to Check
Start from the first element, check if index is within array bounds, process the element, then move to the next index until all elements are visited.
Execution Sample
DSA Go
package main

import "fmt"

func main() {
    arr := []int{10, 20, 30, 40}
    for i := 0; i < len(arr); i++ {
        fmt.Println(arr[i])
    }
}
Traverse the array from start to end and print each element.
Execution Table
StepOperationIndexElement AccessedActionOutputArray State
1Start traversal0arr[0] = 10Print element10[10, 20, 30, 40]
2Increment index1arr[1] = 20Print element20[10, 20, 30, 40]
3Increment index2arr[2] = 30Print element30[10, 20, 30, 40]
4Increment index3arr[3] = 40Print element40[10, 20, 30, 40]
5Increment index4Index 4 out of boundsStop traversal[10, 20, 30, 40]
💡 Index reaches 4 which is equal to array length, stopping traversal.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
i (index)012344
Current Elementarr[0] = 10arr[1] = 20arr[2] = 30arr[3] = 40NoneNone
Array State[10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40]
Key Moments - 3 Insights
Why does the traversal stop when index equals the array length?
Because arrays are zero-indexed, valid indices go from 0 to length-1. When index equals length, it is out of bounds, so traversal stops as shown in step 5 of the execution_table.
What happens if we try to access an element at an index outside the array?
Accessing an index outside the array causes an error or panic in Go. The execution_table shows that at step 5, index 4 is out of bounds, so traversal stops before accessing.
Why do we increment the index after processing the element?
Incrementing the index moves to the next element in the array. The execution_table shows after printing element at index i, i is incremented to access the next element in the next step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' after step 3?
A2
B1
C3
D4
💡 Hint
Check the 'i (index)' row in variable_tracker after step 3.
At which step does the traversal stop because the index is out of bounds?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Operation' and 'Action' columns in execution_table for the stopping condition.
If the array had 5 elements instead of 4, how many steps would the traversal take?
A4
B6
C5
D7
💡 Hint
Traversal steps equal array length plus one for the stopping step, see execution_table exit_note.
Concept Snapshot
Array Traversal Patterns:
- Start at index 0
- Loop while index < array length
- Access and process element at current index
- Increment index to move forward
- Stop when index reaches array length (out of bounds)
Full Transcript
Array traversal means visiting each element in an array one by one. We start at the first element, index 0. We check if the index is less than the array length. If yes, we access the element at that index and do something with it, like print it. Then we increase the index by one to move to the next element. We repeat this until the index reaches the array length, which means we have visited all elements. Trying to access an index equal to or greater than the array length is invalid and stops the traversal.

Practice

(1/5)
1. What does the following Go code snippet do?
for i := 0; i < len(arr); i++ { fmt.Println(arr[i]) }
easy
A. Prints each element of the array from start to end
B. Prints the array elements in reverse order
C. Prints only the first element of the array
D. Causes a runtime error due to index out of range

Solution

  1. Step 1: Understand the loop structure

    The loop starts at index 0 and runs while i is less than the length of the array, increasing i by 1 each time.
  2. Step 2: Analyze the print statement inside the loop

    Each iteration prints the element at index i, so all elements from start to end are printed in order.
  3. Final Answer:

    Prints each element of the array from start to end -> Option A
  4. Quick Check:

    Loop from 0 to len(arr)-1 prints all elements [OK]
Hint: Loop from 0 to len(arr) prints all elements in order [OK]
Common Mistakes:
  • Thinking it prints in reverse order
  • Assuming it prints only one element
  • Confusing loop condition causing error
2. Which of the following is the correct syntax to traverse an array arr using a range-based for loop in Go?
easy
A. for value in arr { fmt.Println(value) }
B. for i := 0; i < len(arr); i++ { fmt.Println(arr[i]) }
C. for index, value := range arr { fmt.Println(index, value) }
D. foreach i in arr { fmt.Println(arr[i]) }

Solution

  1. Step 1: Recall Go's range syntax

    Go uses for index, value := range arr to iterate over arrays or slices.
  2. Step 2: Check each option

    for index, value := range arr { fmt.Println(index, value) } matches the correct syntax. for i := 0; i < len(arr); i++ { fmt.Println(arr[i]) } is valid but not range-based. Options C and D are invalid Go syntax.
  3. Final Answer:

    for index, value := range arr { fmt.Println(index, value) } -> Option C
  4. Quick Check:

    Range loop syntax = for index, value := range arr { fmt.Println(index, value) } [OK]
Hint: Use 'for index, value := range arr' for range loops in Go [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Confusing traditional for loop with range loop
  • Using invalid keywords like foreach
3. What is the output of the following Go code?
arr := []int{10, 20, 30, 40}
for i := len(arr) - 1; i >= 0; i-- {
    fmt.Print(arr[i], " ")
}
medium
A. 10 20 30 40
B. 40 30 20 10
C. Runtime error due to invalid index
D. No output

Solution

  1. Step 1: Understand the loop range

    The loop starts at index len(arr)-1 (3) and decrements i until it reaches 0.
  2. Step 2: Trace the printed values

    Indexes printed: 3, 2, 1, 0 corresponding to values 40, 30, 20, 10 printed with spaces.
  3. Final Answer:

    40 30 20 10 -> Option B
  4. Quick Check:

    Reverse loop prints elements backward [OK]
Hint: Loop from len(arr)-1 down to 0 prints array in reverse [OK]
Common Mistakes:
  • Assuming forward order output
  • Off-by-one errors causing runtime error
  • Confusing loop condition direction
4. Identify the error in this Go code snippet that tries to print all elements of an array arr:
for i := 0; i <= len(arr); i++ {
    fmt.Println(arr[i])
}
medium
A. The print statement syntax is incorrect
B. The loop variable should start from 1 instead of 0
C. The array arr is not declared
D. The loop condition should be i < len(arr) to avoid index out of range

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while i <= len(arr), which means i can equal len(arr), an invalid index.
  2. Step 2: Understand array indexing

    Array indexes go from 0 to len(arr)-1. Accessing arr[len(arr)] causes runtime error.
  3. Final Answer:

    The loop condition should be i < len(arr) to avoid index out of range -> Option D
  4. Quick Check:

    Index must be less than length, not less or equal [OK]
Hint: Use i < len(arr) to avoid out-of-range errors [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Starting loop at 1 instead of 0
  • Ignoring array declaration errors
5. Given an array arr := []int{1, 2, 3, 4, 5}, which code snippet correctly prints only the elements at even indexes (0, 2, 4)?
hard
A. for i := 0; i < len(arr); i += 2 { fmt.Print(arr[i], " ") }
B. for i := 1; i < len(arr); i += 2 { fmt.Print(arr[i], " ") }
C. for _, v := range arr { if v%2 == 0 { fmt.Print(v, " ") } }
D. for i := len(arr)-1; i >= 0; i-- { if i%2 == 1 { fmt.Print(arr[i], " ") } }

Solution

  1. Step 1: Understand the requirement

    We want to print elements at even indexes: 0, 2, 4.
  2. Step 2: Check each option

    for i := 0; i < len(arr); i += 2 { fmt.Print(arr[i], " ") } increments i by 2 starting at 0, correctly accessing even indexes. for i := 1; i < len(arr); i += 2 { fmt.Print(arr[i], " ") } accesses odd indexes. for _, v := range arr { if v%2 == 0 { fmt.Print(v, " ") } } prints even values, not indexes. for i := len(arr)-1; i >= 0; i-- { if i%2 == 1 { fmt.Print(arr[i], " ") } } prints odd indexes in reverse.
  3. Final Answer:

    for i := 0; i < len(arr); i += 2 { fmt.Print(arr[i], " ") } -> Option A
  4. Quick Check:

    Increment by 2 from 0 prints even indexes [OK]
Hint: Increment index by 2 starting at 0 for even indexes [OK]
Common Mistakes:
  • Confusing even indexes with even values
  • Starting loop at 1 instead of 0
  • Using incorrect loop increments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard