What is the main idea behind reversing an array in place?
Swapping elements from the start and end moving towards the center until all elements are reversed.
Click to reveal answer
beginner
Show the Go code snippet to reverse an array in place using two pointers.
func reverse(arr []int) {
left, right := 0, len(arr)-1
for left < right {
arr[left], arr[right] = arr[right], arr[left]
left++
right--
}
}
Click to reveal answer
intermediate
Why is reversing an array in place more memory efficient than creating a new reversed array?
Because it does not require extra space proportional to the array size; it swaps elements within the same array.
Click to reveal answer
beginner
What is the time complexity of reversing an array using the two-pointer technique?
O(n), where n is the number of elements in the array, because each element is visited once during swapping.
Click to reveal answer
intermediate
Explain how recursion can be used to reverse an array.
Recursively swap the first and last elements, then call the function on the subarray excluding those two elements until the base case is reached.
Click to reveal answer
Which technique swaps elements from both ends towards the center to reverse an array?
ABubble sort
BTwo-pointer technique
CLinear search
DBinary search
✗ Incorrect
The two-pointer technique swaps elements from the start and end moving inward to reverse the array.
What is the space complexity of reversing an array in place?
AO(1)
BO(log n)
CO(n)
DO(n log n)
✗ Incorrect
In-place reversal uses constant extra space, so space complexity is O(1).
Which of these is NOT a valid way to reverse an array?
AUsing a new array and copying elements in reverse order
BSwapping elements in place with two pointers
CSorting the array in descending order
DUsing recursion to swap elements
✗ Incorrect
Sorting changes element order based on value, not reversing the array.
In Go, what is the correct way to swap two elements arr[i] and arr[j]?
Aarr[i], arr[j] = arr[j], arr[i]
Btemp := arr[i]; arr[i] = arr[j]; arr[j] = temp
Carr[i] = arr[j]; arr[j] = arr[i]
Dswap(arr[i], arr[j])
✗ Incorrect
Go supports multiple assignment to swap values directly: arr[i], arr[j] = arr[j], arr[i]
What is the base case in a recursive array reversal?
AWhen the array is sorted
BWhen the array is empty
CWhen the array length is odd
DWhen the left index is greater than or equal to the right index
✗ Incorrect
The recursion stops when the left index meets or passes the right index, meaning all swaps are done.
Describe step-by-step how to reverse an array in place using two pointers.
Think about swapping first and last elements, then second and second last, and so on.
You got /4 concepts.
Explain the advantages and disadvantages of reversing an array using recursion versus iteration.
Consider memory use and readability.
You got /4 concepts.
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
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 A
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
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 C
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)
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 A
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
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 B
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.