Amazon Interview Question for Software Engineer / Developers






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

This is a repeat of the question 8226041.

- BM March 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int MinDis(int *a,int b1,int b2){
int i=0,ti=0,tdis=0,dis=SIZE;

for(i=0;i<SIZE;i++){

if(a[i]==b1||a[i]==b2){

if(a[i]!=a[ti]&&ti!=0){
tdis=i-ti;
if(tdis<dis)
dis=tdis;
}
ti=i;

}


}

return dis;

}

- Gyanesh March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code will fail in the following case:
2 5 4 3 2 1

2 is the 0th element, 5 is the 1st element. dis will still remain as SIZE due to your if condition checking if ti != 0

int idxb1 = -1, idxb2 = -1;
int dis = SIZE;

for(int i = 0; i < SIZE; i++) {
   if(a[i] == b1) {
       idxb1 = i;
       if(idxb2 != -1)
          if((idxb1 - idxb2) < dis)
             dis = idxb1 - idxb2; 
    }
   else if(a[i] == b2) {
         idxb2 = i;
         if(idxb1 != -1)
            if((idxb2 - idxb1) < dis)
                dis = idxb2 - idxb1;
     }

}

My code is obviously incomplete, but you would have hopefully got the idea.

- Anonymous April 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max=0;
int c=0;
int[] a = new int[]{1,2,5,6,8,2,1,8,2,1,1,1,1,1,1,1,8};
for(int i=0;i<a.length;i++){
if(a[i]==2){
while(a[i++]!=8){
c++;

}
if(c>max){
max=c;
c=0;
}else{
c=0;
}
}

}
System.out.println(max-1);
}

- Neha March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

include <stdio.h>
#include <algorithm>
#include <limits.h>

int minDist(int a[], int n, int b, int c) {

int ret = INT_MAX, cur = -1;


while(n--) {

if(a[n] == b || a[n] == c) {

if(cur != -1 && a[cur] != a[n]) {

ret = std::min(ret, cur - n);
}
cur = n;

}

}

return ret;
}


int main() {

int a[] = {1, 2, 10, 2, 3, 5, 2, 1, 5};


printf("%d\n", minDist(a, sizeof(a)/sizeof(int), 2, 5));
return 0;
}

- Algoseekar March 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>
#include<conio.h>

void find_dist(int arr[],int a,int b,int size)
{
    
    int dist=9999;
    int strt,fin;
    strt=-1;
    fin=-1;
    for(int i=0;i<size;i++)
    {
       if(arr[i]==a)
       {
         strt=i;
       }
       else if(arr[i]==b)
       {
         fin=i;
       }
       if(strt!=-1 && fin!=-1)
       {
        if(strt>fin) 
        {
         if((strt-fin)<dist)
            dist=strt-fin;
        }
        else
        {
          if((fin-strt)<dist)
            dist=fin-strt;    
        }
       }
    }
    cout<<strt<<" "<<fin<<endl;
    if(strt==-1 && fin==-1) cout<<a<<" and "<<b<<" not found\n";    
    else if(strt==-1) cout<<a<<" not found\n"; 
    else if(fin==-1)  
    {
         if(a==b)
          cout<<"the min dist is  "<<0<<endl;
         else 
          cout<<b<<" not found\n";  
    }
    else
      cout<<"the min dist is  "<<dist<<endl;
}

int main()
{
   int arr[]={1,2,10,2,3,5,2,1,5,10};
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<size<<endl;
   find_dist(arr,1,10,size);
   getch();
   return 0;
}

- XYZ March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>
#include<conio.h>

void find_dist(int arr[],int a,int b,int size)
{
    
    int dist=9999;
    int strt,fin;
    strt=-1;
    fin=-1;
    for(int i=0;i<size;i++)
    {
       if(arr[i]==a)
       {
         strt=i;
       }
       else if(arr[i]==b)
       {
         fin=i;
       }
       if(strt!=-1 && fin!=-1)
       {
        if(strt>fin) 
        {
         if((strt-fin)<dist)
            dist=strt-fin;
        }
        else
        {
          if((fin-strt)<dist)
            dist=fin-strt;    
        }
       }
    }
    cout<<strt<<" "<<fin<<endl;
    if(strt==-1 && fin==-1) cout<<a<<" and "<<b<<" not found\n";    
    else if(strt==-1) cout<<a<<" not found\n"; 
    else if(fin==-1)  
    {
         if(a==b)
          cout<<"the min dist is  "<<0<<endl;
         else 
          cout<<b<<" not found\n";  
    }
    else
      cout<<"the min dist is  "<<dist<<endl;
}

int main()
{
   int arr[]={1,2,10,2,3,5,2,1,5,10};
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<size<<endl;
   find_dist(arr,1,10,size);
   getch();
   return 0;
}

- XYZ March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud 1 !

- Anonymous April 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
        int arr[]={2,5,4,3,2,1};

        int p1,p2,minval,n,v1,v2,temp;
        n=6;
        p1=0;
        p2=0;
        v1=2;
        v2=5;

        while(arr[p1]!=v1)
                p1++;
        while(arr[p2]!=v2)
                p2++;

        minval=abs(p1-p2);

        while(p1<n || p2<n)
        {
                if(arr[p1]==v1 && arr[p2]==v2)
                        if(abs(p1-p2)<minval)
                                minval=abs(p1-p2);

                if(p2>p1)
                        p1++;
                else
                        p2++;
        }

        printf("MIN = %d\n",minval-1);
}

- inx August 17, 2011 | 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