Intel Interview Question for Software Engineer / Developers


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




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

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

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

}

- Anonymous May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Silly question.

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

- Anonymous 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 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 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 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 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 May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

fuck you all
bazinga !!!

- Sheldon Cooper 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 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 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 October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is wrong.. lets see how..
take the string as "abcd"
in first call you make it "dbca"
then "dcba".. its reversed.. but wait.. n is still far from 0...
you reverse again it to.. "dbca".
then "abcd"..

So my friend.. make the recursion calls till n reaches ceil(n/2) (ceil as you are coming from n to middle; in case of odd characters the middle char remains at its original place.)

- Tarang July 26, 2013 | 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 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 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 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 May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You case is not correct. consider array only has two elements. It's not get reversed.

Solution: change if(start==end ||start==end-1)
to if(start<=end)

- fredju November 03, 2013 | Flag
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 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 May 02, 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 October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void rev_arr(int [],int,int);

int main()
{
int i=0;
int arr[10]={1,2,3,4,5,6,7,8,9,0};
rev_arr(arr,0,9);
for(i=0;i<10;i++)
printf("%d\n",arr[i]);
return 0;
}

void rev_arr(int arr[],int min,int max)
{
if(min>4)
return;
int temp=arr[min];
arr[min]=arr[max];
arr[max]=temp;
rev_arr(arr,min+1,max-1);
}

- uday September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void rev(int *,int *);
void main(){
int arr[]={1,2,3,4,5,6,7,8,9,0};
int i;
printf("\nThe array is:");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);
rev(&arr[0],&arr[9]);
printf("\nThe New array is:");
for(i=0;i<10;i++) {
printf("%d\t",arr[i]);
sleep(1);
}
getch();
}
void rev(int *p, int *q){

int temp;
temp=*p;
*p=*q;
*q=temp;
if(q>p){
rev(p+1,q-1);
}
}

- RK January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void printArray(int n[],int i)
{
cout<<n[i]<<endl;
if(i>0)
{
printArray(n,i-1);
}
}
int main()
{
int n[]={1,2,3,4,5,6,7,8};
printArray(n,7);
return 0;
}

- Jignesh July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void printArray(int n[],int i)
{
    cout<<n[i]<<endl;
    if(i>0)
    {
        printArray(n,i-1);
    }
}
int main()
{
    int n[]={1,2,3,4,5,6,7,8};
    printArray(n,7);
    return 0;
}

- Jignesh July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void printArray(int n[],int i)
{
    cout<<n[i]<<endl;
    if(i>0)
    {
        printArray(n,i-1);
    }
}
int main()
{
    int n[]={1,2,3,4,5,6,7,8};
    printArray(n,7);
    return 0;
}

- Jignesh Patel July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//============================================================================
// Name : reversearray.cpp
// Author : gangadhar
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
cout << "\nProgram to reverse array";
cout << "\nEnter iterations";
int T;
cin >> T;
cout << "\nEnter array size";
int size;
cin >> size;
int arr[size], i, temp, iter;
cout << "\nReversing array";
for(iter=0; iter<T; iter++){
cout << "\nEnter array elements:";
for(i=0; i< size; i++)
cin >> arr[i];
for(i=0; i< size/2; i++){
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
cout << "\nReversed array";
for(i=0; i< size; i++) {
cout << arr[i] << endl;
}
}



return 0;
}

- Runs for T iterations and reverses the arra April 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//============================================================================
// Name : reversearray.cpp
// Author : gangadhar
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
cout << "\nProgram to reverse array";
cout << "\nEnter iterations";
int T;
cin >> T;
cout << "\nEnter array size";
int size;
cin >> size;
int arr[size], i, temp, iter;
cout << "\nReversing array";
for(iter=0; iter<T; iter++){
cout << "\nEnter array elements:";
for(i=0; i< size; i++)
cin >> arr[i];
for(i=0; i< size/2; i++){
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
cout << "\nReversed array";
for(i=0; i< size; i++) {
cout << arr[i] << endl;
}
}



return 0;
}

- ganga.mahadevan5 April 14, 2016 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

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