Array Reversal Techniques in DSA Go - Time & Space Complexity
We want to understand how the time needed to reverse an array changes as the array gets bigger.
How does the number of steps grow when we reverse more elements?
Analyze the time complexity of the following code snippet.
func reverseArray(arr []int) {
left, right := 0, len(arr)-1
for left < right {
arr[left], arr[right] = arr[right], arr[left]
left++
right--
}
}
This code reverses the elements of an array by swapping pairs from the ends moving toward the center.
- Primary operation: Swapping two elements in the array.
- How many times: Approximately half the length of the array (n/2 times).
As the array size grows, the number of swaps grows roughly half as fast.
| Input Size (n) | Approx. Operations (swaps) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The operations grow linearly with input size, just half as many swaps as elements.
Time Complexity: O(n)
This means the time to reverse the array grows directly in proportion to the number of elements.
[X] Wrong: "Reversing an array takes n squared steps because of swapping."
[OK] Correct: Each swap touches only two elements, and the loop runs about n/2 times, so the steps grow linearly, not quadratically.
Understanding this helps you explain how simple loops affect performance and shows you can analyze basic array operations clearly.
"What if we used recursion to reverse the array instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand array order
An array has elements arranged in a sequence from first to last.Step 2: Define reversal
Reversing means flipping this order so the last element comes first and the first goes last.Final Answer:
Changing the order of elements so the last becomes first and first becomes last -> Option AQuick Check:
Reverse = Flip order [OK]
- Confusing reversal with sorting
- Thinking reversal removes elements
- Believing reversal adds elements
arr of integers?Solution
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.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.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 CQuick 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]
- Not swapping but overwriting elements
- Looping over full array instead of half
- Ignoring index bounds causing errors
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)Solution
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]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].Final Answer:
[5 4 3 2 1] -> Option AQuick Check:
Swapped ends inward = reversed array [OK]
- Only swapping first and last once
- Not swapping pairs correctly
- Confusing output with original array
arr:
for i := 0; i <= len(arr)/2; i++ {
arr[i] = arr[len(arr)-1-i]
}Solution
Step 1: Analyze loop and assignment
The code assigns arr[i] = arr[len(arr)-1-i] without swapping, so original arr[i] is lost.Step 2: Understand consequence
Without swapping, the array is partially overwritten and not reversed correctly.Final Answer:
It overwrites elements without swapping, losing data -> Option BQuick Check:
Overwrite without swap loses data [OK]
- Using <= instead of < in loop
- Not swapping both elements
- Ignoring data loss from overwrite
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?Solution
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.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.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 DQuick Check:
Swap pairs from ends to center and return [OK]
- Overwriting elements without swapping
- Looping beyond half length
- Not returning the modified slice
Ready to prove it?
Pass the test and this pattern moves unknown
