## Intel Interview Question Software Engineer / Developers

• 0

Reverse an array without using iteration.
Hint: Recursion.

Team: Firmware
Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

Silly question.

``````void reverse(int *a, int n) {
if (n <= 0) return;
swap(a[0], a[n-1]);
reverse(++a, n-1);
}``````

Comment hidden because of low score. Click to expand.
0

This wont work. Probably typo. pass n-2 instead of n-1 for the recursive call?

Comment hidden because of low score. Click to expand.
0

Yea, we have to pass n-2 because ++a moves it by one more

Comment hidden because of low score. Click to expand.
0

Corrected code:-

``````void reverse(int *a, int n) {
if (n <= 0) return;
swap(a[0], a[n-1]);
reverse(++a, n-2);``````

}

Comment hidden because of low score. Click to expand.
0

There is no silly question. Only arrogant people.

Comment hidden because of low score. Click to expand.
0

There are no arrogant people. Only sensitive idiots.

Comment hidden because of low score. Click to expand.
0

fuck you all
bazinga !!!

Comment hidden because of low score. Click to expand.
0

I guess the check needs to be modified like this

void reverse(int *a, int n) {
if (n <= 1) return;
swap(a[0], a[n-1]);
reverse(++a, n-1);
}

Comment hidden because of low score. Click to expand.
0

Considering swap function, don't we need to call swap(&a[0], &a[n-1])?

Comment hidden because of low score. Click to expand.
0

No, arrays are already in memory, so you're directly accessing it.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/*  Move first element to last position, and shift other elements to left */
void Bubble(T *a, int len){
if (len <= 1) return;
T tmp = a[0];
a[0] = a[1];
a[1] = tmp;
Bubble(++a, --len);
}
void Reverse(T * a, int len) {
Bubble(a, len); /* first move first element to last */
Reverse(a, --len); /* now reverse the initial portition of bubbled array*/
}``````

Comment hidden because of low score. Click to expand.
0

Of course, the Reverse needs to to return when length is 1.

So corrected code:

``````/*  Move first element to last position, and shift other elements to left */
void Bubble(T *a, int len){
if (len <= 1) return;
T tmp = a[0];
a[0] = a[1];
a[1] = tmp;
Bubble(++a, --len);
}
void Reverse(T * a, int len) {
if (len <= 1) return;
Bubble(a, len); /* first move first element to last */
Reverse(a, --len); /* now reverse the initial portition of bubbled array*/
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include <iostream>

using namespace std;

void reverse_array(int array[], int index, int len) {
if (index >= len - 1) return;
array[index] = array[index]^array[len - 1];
array[len - 1] = array[index]^array[len - 1];
array[index] = array[index]^array[len - 1];
reverse_array(array, index + 1, len - 1);
}

int main() {
int array[10];
for (int i = 0; i < 10; ++i)
array[i] = i;
reverse_array(array, 0, 10);
for (int i = 0; i < 10; ++i)
cout << array[i] << endl;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include "stdio.h"
#define MAX 10

void printarr(int a[],int n)
{
printf("\nArray elements : ");
int i;
for(i=0;i<n;i++)
printf(" %d",a[i]);
}

void reverse(int a[],int start,int end)
{
int temp;
temp=a[start];
a[start]=a[end];
a[end]=temp;

if(start==end ||start==end-1)
return;
reverse(a,start+1,end-1);
}

int main()
{
int arr[MAX];
int n,i;
printf("Enter size of array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element : ");
scanf("%d",&arr[i]);
}
printarr(arr,n);
reverse(arr,0,n-1);
printarr(arr,n);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int len=sizeof(a)/sizeof(int); //9
for(int i=0;i<len/2;i++) //4
swap(a[i],a[len-1-i]);
for(int j=0;j<len;j++)
cout << a[j]<<"\t";
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int len=sizeof(a)/sizeof(int); //9
for(int i=0;i<len/2;i++) //4
swap(a[i],a[len-1-i]);
for(int j=0;j<len;j++)
cout << a[j]<<"\t";
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

reverserArray(int a[],int initial,int last){

if(initial < last)
reverserArray(a,initial+1,last-1);
swap(a,inital,last);

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

char *arrayrec(char arr[],int N,int i=0)
{
if(i>=N/2)
return arr;
char temp=arr[i];
arr[i]=arr[N-i-1];
arr[N-i-1]=temp;
arrayrec(arr,N,++i);

}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.