If you are preparing for technical interviews, sooner or later someone will ask you to reverse an array. At first glance it looks trivial, but it quietly tests how well you understand indexing, boundaries, and memory.
In Go, this problem becomes a great opportunity: the language gives you clean syntax and multiple assignment, so you can write an in-place solution that is both efficient and easy to read.
In this post I’ll walk you through my approach to reversing a slice in place using start and end indices, without any extra memory. Think of it as a pattern you can reuse in many other algorithms—not just a one-off interview trick.
Let’s start by formalizing the problem we’re trying to solve.
Problem Statement
Given an array of integers, a start index, and an end index, write a function that reverses the integers in-place without using extra memory.
More formally:
Input: an array of integers, an integer for the starting index and another integer for the ending index.
Output: the same array modified in place, with the elements start and end reversed.
Now that we know exactly what goes in and what should come out, we can focus on defining a simple, safe algorithm to get there.
Algorithm
Before touching the array, I need to make sure the indices are safe. If start or end are out of range, or if start is not strictly less than end, there is nothing to do and I can just return the original array.
if start >= end || start < 0 || end >= len(arr) {
return arr
}
Once the input is valid, the idea is simple: swap elements from the edges of the range towards the center. I only need to visit half of the elements between start and end. For each position i from the left, I swap it with its counterpart from the right:
left index:
start + iright index:
end - i
That is why the loop runs up to half the length of the range:
for i := 0; i < (end-start+1)/2; i++ {}
The expression (end - start + 1) is the number of elements in the range [start, end]. Dividing it by 2 gives the number of swaps we need to perform.
To see this in action, take the array [1, 2, 3] and reverse the whole range:
First iteration (
i = 0): swaparr[0]andarr[2]→[3, 2, 1].There is no second iteration, because we already reached the middle.
Thanks to Go's multiple assignment, the swap stays readable and we keep the space complexity at O(1). When the loop finishes, the original slice has been modified in place, using the same underlying memory.
arr[start+i], arr[end-i] = arr[end-i], arr[start+i]
When the loop ends, my original array has been modified while keeping the same memory range. To ensure this is true, I’m going to make a simple test.
Being my Own QA
Now that the algorithm is in place, I want to prove that it actually does what it says. I'll start with a simple test case and let Go’s testing package be my QA.
In this example, I reverse the entire array [1, 2, 3, 4] by using start = 0 and end = 3:
func TestReverseArray(t *testing.T) {
arr := []int{1, 2, 3, 4}
start := 0
end := 3
result := ReverseInPlace(arr, start, end)
expected := []int{4, 3, 2, 1}
for i := range expected {
if result[i] != expected[i] {
t.Errorf("Expected %v, but got %v", expected[i], result[i])
break
}
}
if &arr[0] != &result[0] {
t.Error("Expected array to be modified in place, but got a different array reference")
}
}
This test checks two things:
The values are correct:
[1, 2, 3, 4]becomes[4, 3, 2, 1].The memory is the same: comparing
&arr[0]and&result[0]ensures that we did not create a new array behind the scenes, we modified the original one in place.
You can add more test cases to cover edge scenarios, for example:
A subrange in the middle of the array.
A range of length 1.
Invalid ranges, such as
start > endorend out of bounds, where the function should simply return the original array.
Final Thoughts
Reversing an array in place isn’t just a classic interview question. It is a nice way to practice things you use all the time: indexes, boundaries, and how memory works in Go.
The goal is not to memorize this exact solution, but to keep the pattern: move from the ends toward the middle, touch only half of the range, and stay at O(1) space. From there you can play with variations like subranges, rotations, or different data types.
If you want more exercises like this, check out my algorithms repo: github.com/RubenOAlvarado/algorithms
Pick a problem, try your own solution, then compare it with mine. That kind of practice is what really adds up over time.
Happy coding and I’ll see you in the next one!
Top comments (0)