· Manikandan · problem solving · 2 min read
Reverse an array with out using existing funtionalities
logic for reversing an array without relying on built-in functions or predefined methods

Problem statement
During my interview, the interviewer presented me with a challenge to develop a logic for reversing an array without relying on built-in functions or predefined methods. The objective was to implement the solution purely through fundamental programming constructs, focusing on algorithmic efficiency and logical reasoning rather than leveraging existing utilities provided by the language.
Code
1: INPUT ==> [1,2,4,5,7] OUTPUT==> [7,5,4,2,1]
Solutions 1:
first Solution I write this,
public int[] WithForSol1()
{
int[] result = new int[array.Length];
int j=0;
for(int i=array.Length-1;i>=0;i--)
{
result[j]=array[i];
j++;
}
return result;
}
Solution 2:
and he want me to change this one without using additional Array . So I did like below
public int[] WithForSol2()
{
for(var i=0;i<=(array.Length)/2;i++)
{
var temp=array[i];
var position=(array.Length-1)-i;
array[i]=array[position];
array[position]=temp;
}
return array;
}
Solution 3:
and agin He forced me to change this one to optimized way, I said instead for we can use while
public int[] ReverseWithWhile()
{
int i = 0, position = array.Length - 1;
while (i < position)
{
(array[i], array[position]) = (array[position], array[i]);
i++;
position--;
}
return array;
}
Solution 3:
The interviewer further mentioned that there is an alternative approach to optimize this solution. However, I was not familiar with it at the time. Based on my understanding, I explored a possible solution using tuple swapping. While this method seemed efficient, I was uncertain whether it qualifies as an optimized approach, as tuple swapping is inherently a predefined feature in C# and is only supported in the latest versions of the language. I would appreciate further clarification on other potential optimization techniques that could enhance performance while staying within fundamental programming principles.
public int[] ReverseWithWhile()
{
int i = 0, position = array.Length - 1;
while (i < position)
{
(array[i], array[position]) = (array[position], array[i]);
i++;
position--;
}
return array;
}
My thoughts:
Given the allocated time of 10 minutes, I was able to provide two solutions along with one optimization suggestion. However, the interviewer was still looking for a more optimized approach. I hope this indicates that I am progressing to the next stage of the process. If you have any recommendations or insights on further optimizations, please feel free to connect with me.
Benchmark Results
The metrix in below shows the results of our performance benchmarks.
Method | Mean | Error | StdDev | Min | Max | Gen0 | Allocated |
---|---|---|---|---|---|---|---|
WithForSol1 | 4.586 ns | 0.0731 ns | 0.0648 ns | 4.533 ns | 4.718 ns | 0.0067 | 56 B |
WithForSol2 | 1.886 ns | 0.0659 ns | 0.0857 ns | 1.795 ns | 2.132 ns | - | - |
WithForSol3 | 1.349 ns | 0.0572 ns | 0.0821 ns | 1.215 ns | 1.528 ns | - | - |
ReverseWithWhile | 1.353 ns | 0.0575 ns | 0.0878 ns | 1.248 ns | 1.598 ns | - | - |