· 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

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.

MethodMeanErrorStdDevMinMaxGen0Allocated
WithForSol14.586 ns0.0731 ns0.0648 ns4.533 ns4.718 ns0.006756 B
WithForSol21.886 ns0.0659 ns0.0857 ns1.795 ns2.132 ns--
WithForSol31.349 ns0.0572 ns0.0821 ns1.215 ns1.528 ns--
ReverseWithWhile1.353 ns0.0575 ns0.0878 ns1.248 ns1.598 ns--
Back to Blog