Intel Corporation Interview Question: Array reverse without using i... | Glassdoor

Interview Question

Software Engineer Entry Level Interview Santa Clara, CA

Array reverse without using iteration. Code in C.

Answer

Interview Answer

3 Answers

2

Recursin

Interview Candidate on May 1, 2012
1

unsigned int A[N] (is global variable)
l - is left index
r - is right index

swap(unsigned int l , unsigned int r) {
 tmp = A [ r ];
 A [ r ] = A [ l ];
 A[ l ] = tmp;
}

void reverse ( unsigned int l , unsigned int r ) {
 if ( l + 1 >= r )
   return;
 reverse( l + 1 , r - 1 );
 swap( l , r );
}

Call it by:
reverse ( 0 , N - 1 );

the Dude on Oct 27, 2012
0

#include

#define ARRAY_SIZE 20

void reverse( int *array, unsigned int l, unsigned int r );

int main(int argc, char** argv) {
  int array[ARRAY_SIZE];
  int i;
  printf("Before: ");
  for( i = 0; i = r ) {
    return;
  }
  int tmp = array[l];
  array[l] = array[r];
  array[r] = tmp;
  reverse( array, l+1, r-1);
}

KL on Jul 6, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.