Bird
Raised Fist0
DSA Goprogramming~10 mins

Array Reversal Techniques in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Reversal Techniques
Start with array
↓
Set left = 0, right = len-1
↓
Check if left < right?
No→Done
Yes↓
Swap elements at left and right
↓
Increment left, Decrement right
↩Back to Check
Start with two pointers at the ends of the array, swap elements, then move pointers inward until they meet or cross.
Execution Sample
DSA Go
arr := []int{1, 2, 3, 4, 5}
left, right := 0, len(arr)-1
for left < right {
  arr[left], arr[right] = arr[right], arr[left]
  left++
  right--
}
This code reverses the array by swapping elements from the ends moving towards the center.
Execution Table
StepOperationleftrightArray StateSwap Performed
1Initialize pointers04[1, 2, 3, 4, 5]No
2Check left < right04[1, 2, 3, 4, 5]No
3Swap arr[left] and arr[right]04[5, 2, 3, 4, 1]Yes (arr[0]<->arr[4])
4Increment left, Decrement right13[5, 2, 3, 4, 1]No
5Check left < right13[5, 2, 3, 4, 1]No
6Swap arr[left] and arr[right]13[5, 4, 3, 2, 1]Yes (arr[1]<->arr[3])
7Increment left, Decrement right22[5, 4, 3, 2, 1]No
8Check left < right22[5, 4, 3, 2, 1]No (2 < 2 is False)
9End loop22[5, 4, 3, 2, 1]No
💡 left (2) is not less than right (2), so loop ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7Final
left001122
right443322
arr[1,2,3,4,5][5,2,3,4,1][5,2,3,4,1][5,4,3,2,1][5,4,3,2,1][5,4,3,2,1]
Key Moments - 3 Insights
Why do we stop the loop when left is not less than right?
Because when left equals or passes right, all elements have been swapped. See execution_table row 8 where left=2 and right=2, loop ends.
Why do we swap arr[left] and arr[right] instead of just reversing by copying?
Swapping in place saves memory and is efficient. The execution_table rows 3 and 6 show swaps happening directly in the original array.
What happens if the array has an odd number of elements?
The middle element stays in place because left and right meet at the center without swapping. See step 8 where left=right=2 and no swap occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 6?
A[5, 4, 3, 2, 1]
B[5, 2, 3, 4, 1]
C[1, 2, 3, 4, 5]
D[5, 4, 1, 2, 3]
💡 Hint
Check the 'Array State' column at step 6 in execution_table.
At which step does the condition left < right become false?
AStep 7
BStep 9
CStep 8
DStep 6
💡 Hint
Look at the 'Check left < right' operation and condition result in execution_table row 8.
If the array length was even, how would the final left and right values differ?
Aleft would be zero, right would be length-1
Bleft and right would cross (left > right)
Cleft and right would be equal
Dleft and right would not change
💡 Hint
Consider how pointers move inward and stop when left is no longer less than right.
Concept Snapshot
Array Reversal Technique:
- Use two pointers: left at start, right at end
- While left < right:
  - Swap arr[left] and arr[right]
  - Move left++ and right--
- Stops when pointers meet or cross
- Works in-place, O(n) time, O(1) space
Full Transcript
This visualization shows how to reverse an array by swapping elements from the ends moving inward. We start with two pointers, left at index 0 and right at the last index. While left is less than right, we swap the elements at these positions, then move left forward and right backward. The process repeats until the pointers meet or cross, meaning the array is fully reversed. The execution table tracks each step, showing pointer positions, swaps, and array state changes. Key moments clarify why the loop stops when pointers meet and how odd-length arrays keep the middle element unchanged. The quiz tests understanding of array states and pointer movements during reversal.

Practice

(1/5)
1. What does reversing an array mean in simple terms?
easy
A. Changing the order of elements so the last becomes first and first becomes last
B. Sorting the array in ascending order
C. Removing duplicate elements from the array
D. Adding new elements at the end of the array

Solution

  1. Step 1: Understand array order

    An array has elements arranged in a sequence from first to last.
  2. Step 2: Define reversal

    Reversing means flipping this order so the last element comes first and the first goes last.
  3. Final Answer:

    Changing the order of elements so the last becomes first and first becomes last -> Option A
  4. Quick Check:

    Reverse = Flip order [OK]
Hint: Reverse means flip the order of elements [OK]
Common Mistakes:
  • Confusing reversal with sorting
  • Thinking reversal removes elements
  • Believing reversal adds elements
2. Which of the following Go code snippets correctly reverses an array named arr of integers?
easy
A. for i := len(arr)-1; i >= 0; i-- { arr[i] = arr[i] }
B. for i := 0; i < len(arr); i++ { arr[i] = arr[len(arr)-1-i] }
C. for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] }
D. for i := 0; i < len(arr); i++ { arr[i] = arr[i+1] }

Solution

  1. Step 1: Identify correct swapping logic

    for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } swaps elements from start and end moving towards the center, which is the correct reversal method.
  2. Step 2: Check other options for errors

    for i := 0; i < len(arr); i++ { arr[i] = arr[len(arr)-1-i] } overwrites elements without swapping, losing data. for i := 0; i < len(arr); i++ { arr[i] = arr[i+1] } shifts elements incorrectly. for i := len(arr)-1; i >= 0; i-- { arr[i] = arr[i] } does nothing effective.
  3. Final Answer:

    for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } -> Option C
  4. Quick Check:

    Swap pairs from ends to center = for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } [OK]
Hint: Swap pairs from start and end until middle [OK]
Common Mistakes:
  • Not swapping but overwriting elements
  • Looping over full array instead of half
  • Ignoring index bounds causing errors
3. What is the output of the following Go code?
arr := []int{1, 2, 3, 4, 5}
for i := 0; i < len(arr)/2; i++ {
    arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i]
}
fmt.Println(arr)
medium
A. [5 4 3 2 1]
B. [1 2 3 4 5]
C. [5 2 3 4 1]
D. [1 5 3 2 4]

Solution

  1. Step 1: Trace swaps in loop

    Loop runs i=0 and i=1 (since len=5, half=2). - i=0: swap arr[0] and arr[4] -> arr becomes [5 2 3 4 1] - i=1: swap arr[1] and arr[3] -> arr becomes [5 4 3 2 1]
  2. Step 2: Check final array after loop

    After both swaps, array is [5 4 3 2 1]. But the code prints after loop, so output is [5 4 3 2 1].
  3. Final Answer:

    [5 4 3 2 1] -> Option A
  4. Quick Check:

    Swapped ends inward = reversed array [OK]
Hint: Swap pairs from ends, middle stays same if odd length [OK]
Common Mistakes:
  • Only swapping first and last once
  • Not swapping pairs correctly
  • Confusing output with original array
4. Find the error in this Go code that tries to reverse an array arr:
for i := 0; i <= len(arr)/2; i++ {
    arr[i] = arr[len(arr)-1-i]
}
medium
A. The loop should run only till i < len(arr)/2, not <=
B. It overwrites elements without swapping, losing data
C. The code swaps elements correctly, no error
D. The index len(arr)-1-i is out of range

Solution

  1. Step 1: Analyze loop and assignment

    The code assigns arr[i] = arr[len(arr)-1-i] without swapping, so original arr[i] is lost.
  2. Step 2: Understand consequence

    Without swapping, the array is partially overwritten and not reversed correctly.
  3. Final Answer:

    It overwrites elements without swapping, losing data -> Option B
  4. Quick Check:

    Overwrite without swap loses data [OK]
Hint: Always swap pairs, don't overwrite [OK]
Common Mistakes:
  • Using <= instead of < in loop
  • Not swapping both elements
  • Ignoring data loss from overwrite
5. Given a slice arr := []int{1, 2, 3, 4, 5, 6}, write a Go function that reverses the slice in place and returns it. Which of the following is the correct implementation?
hard
A. func reverse(arr []int) []int { for i := 0; i < len(arr); i++ { arr[i] = arr[len(arr)-1-i] } return arr }
B. func reverse(arr []int) []int { for i := 0; i <= len(arr)/2; i++ { arr[i] = arr[len(arr)-1-i] } return arr }
C. func reverse(arr []int) []int { for i := len(arr)-1; i >= 0; i-- { arr[i] = arr[i] } return arr }
D. func reverse(arr []int) []int { for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } return arr }

Solution

  1. Step 1: Check correct swapping logic

    func reverse(arr []int) []int { for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } return arr } swaps elements from start and end moving inward, correctly reversing the slice in place.
  2. Step 2: Verify other options

    func reverse(arr []int) []int { for i := 0; i < len(arr); i++ { arr[i] = arr[len(arr)-1-i] } return arr } overwrites without swapping, losing data. func reverse(arr []int) []int { for i := len(arr)-1; i >= 0; i-- { arr[i] = arr[i] } return arr } does nothing effective. func reverse(arr []int) []int { for i := 0; i <= len(arr)/2; i++ { arr[i] = arr[len(arr)-1-i] } return arr } overwrites without swapping and uses <= causing index issues.
  3. Final Answer:

    func reverse(arr []int) []int { for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } return arr } -> Option D
  4. Quick Check:

    Swap pairs from ends to center and return [OK]
Hint: Swap pairs up to half length, then return [OK]
Common Mistakes:
  • Overwriting elements without swapping
  • Looping beyond half length
  • Not returning the modified slice
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