Intel Interview Question Software Engineer / Developers

  • intel-interview-questions
    0
    of 0 votes
    18
    Answers

    Reverse an array without using iteration.
    Hint: Recursion.

    - KaranGoswamiKenZ on May 02, 2012 in United States for Firmware Report Duplicate | Flag
    Intel Software Engineer / Developer Arrays

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);
}

- Anonymous on May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous on May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- KaranGoswamiKenZ on May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Corrected code:-

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

}

- Nitish on May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no silly question. Only arrogant people.

- Anonymous on May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are no arrogant people. Only sensitive idiots.

- Anonymous on May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

fuck you all
bazinga !!!

- Sheldon Cooper on June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}

- Deepak Sharma on July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Annoymous11 on September 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous on October 08, 2012 | Flag
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*/
}

- Anonymous on May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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*/
}

- Anonymous on May 02, 2012 | Flag
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;
}

- milo on May 02, 2012 | Flag Reply
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);
}

- jsd on May 02, 2012 | Flag Reply
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;
}

- maverick on May 02, 2012 | Flag Reply
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;
}

- maverick on May 02, 2012 | Flag Reply
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);

}

- Anonymous on May 05, 2012 | Flag Reply
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);



}

- aaman.singh85 on October 20, 2012 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More