Intel Corporation

  www.intel.com
  www.intel.com

Interview Question

Software Engineer Entry Level Interview Santa Clara, CA

Array reverse without using iteration. Code in C.

Answer

Interview Answer

2 Answers

0

Recursin

Interview Candidate on May 1, 2012
0

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

Add Answers or Comments

To comment on this, Sign In or Sign Up.