Amazon Interview Question for Software Engineer / Developers


Team: Kindle
Country: India
Interview Type: Written Test




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

int modifiedBinarySearch(int arr[], int left, int right)
{

while(left <=  right and (right - left >= 0) )
{
          int mid = (right + left)/2;
          if (arr[mid] == 0)
                     left = mid + 1;
          else 
               //Check if the element left to 1 is 0, then mid is the starting position
               if (mid > 0 && arr[mid - 1] == 0) 
                     return mid;
               // Binary search towards the left
               else if (mid > 0 && arr[mid - 1] == 1)
                     right = mid - 1;    
}
// Return - 1 when the arr has all zeros or no elements
return -1;
}

int main()
{
int arr[5] = {0, 0, 0, 1, 1};
cout<<modifiedBinarySearch(arr, 0, 4);
return 0;
}

- Dragon March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apply binary search with extra concern that check the right and left element.

int Bin(int a[],int i,int j)
{
if(i>j )
return -1;

mid=i+ (j-i)/2;

if(mid==0 && a[mid]==1)
return 0; //first element
if(mid>0)
if(a[mid]==1 && a[mid-1]==0 )
return mid;

if(a[mid]==0)
return bin(a,mid+1,j);
if(a[mid]==1)
return bin(a,i,mid-1);
}

- NaiveCoder February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

code fails for:{0,0,0,0,0}

- skbuta February 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks for pointing out

in the end of function you can add the statement
return -1;

- NaiveCoder February 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[10] = {0,0,0,1,1,1,1,1,1,1};
for(int i=0; i<a.length;i=i+2){

if (a[i] + a[i+1] = =1)
{s.o.p("Count = "+(i+1));
break;
}


if(a[i]+a[i+1]==2)
{s.o.p("Count = "+(i));
break;
}

}// for loop end

- Abhishek February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Binary search to find index for first occurrence and last occurrence of 1's .. so no. of occurences = last_index - first_index + 1

- viks February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

add the elements starting from index 0 until you get a value 1, that index would be the answer.

worst case =O(n)
int s=0;
for(int i=0; <n; i++)
{
s+= a[i];
if(s)
{
printf("found first one at an Index %d", i);
break;
}
}

- raja roy February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why you are adding, just if(a[i]) will work

- rosh February 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) solution
int a[10]={0,0,0,0,0,1,1};
for(int i=0;i<10;i++)
{
if(a[i]>0)
return i;
}

- udhaya10 February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary search strategy :

int first1(int * a , int low , int high ) {

if ( low > high )
return -1;
else if( low == high ) {
if(a[low] == 1)
return low;
else return -1;
}
else {
int mid = (low +high )/2;
if( a[mid] == a[mid+1] ){
if(a[mid] == 1)
return first1(a,low,mid);
else
return first1(a,mid+2,high);
}

else return (mid+1) ;
}

}
}

- mohangupta13 February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary search strategy :

int first1(int * a , int low , int high ) {

if ( low > high )
return -1;
else if( low == high ) {
if(a[low] == 1)
return low;
else return -1;
}
else {
int mid = (low +high )/2;
if( a[mid] == a[mid+1] ){
if(a[mid] == 1)
return first1(a,low,mid);
else
return first1(a,mid+2,high);
}

else return (mid+1) ;
}

}
}

- mohangupta13 February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary search strategy :

int first1(int * a , int low , int high ) {

if ( low > high )
return -1;
else if( low == high ) {
if(a[low] == 1)
return low;
else return -1;
}
else {
int mid = (low +high )/2;
if( a[mid] == a[mid+1] ){
if(a[mid] == 1)
return first1(a,low,mid);
else
return first1(a,mid+2,high);
}

else return (mid+1) ;
}

}
}

- mohangupta13 February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is the value for high?
its not working for 0000011 this test case

- manidavid0 February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary search:

int findOne(vector<int> a)
{
	int start = 0;
	int end = a.size() - 1;
	int middle;

	while(start >= 0 && start <= end)
	{
		middle = (start + end) / 2;
		if(a.at(middle) == 1)
		{
			if(middle == 0 || a.at(middle - 1) == 0)
				return middle;
			else
			{
				end = middle - 1;
				continue;
			}
		}
		else
			start = middle + 1;
	}
	return -1;
}

- StartFromNeg1 February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My question is: Why will someone want to ask this ? What is the use ?

- Anonymous February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

anything involving bit encoding / decoding. subnet masking, network protocol management aka for bit operations in general.

- Rishi March 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My question is: Why will someone want to ask this ? What is the use ?

- Anonymous February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find(int a[],int low, int high)
{
   if(high<low)
     return low;
   int mid = (low+high)/2;
   if(a[mid] == 0)
     return find(a,mid+1,high);
   else
     return find(a,low,mid-1);
 
}

int main()
{
   int ip[] = {0,0,0,0,0};
   printf("The op indx is :%d",find(ip,0,4));
   return 0;
}

- Anonymous February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary search technique, time complexity is O(log n)
int low=0,hi=n-1;
while(low<=hi)
{
int mid=(low+hi)/2;

if(A[mid-1]==1)
hi=mid-1;
else if(A[mid]==1)
return mid+1;
else if(A[mid+1]==1)
low=mid+1;
else
low=mid+1;
}
return 0;

any change pls reply

- manidavid0 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary search strategy and with out recursive function:

int low=0,hi=n-1;
     while(low<=hi)
     {
          int mid=(low+hi)/2;
          
          if(A[mid-1]==1)
              hi=mid-1;
          else if(A[mid]==1)
              return mid+1;
          else if(A[mid+1]==1)
              low=mid+1;
          else
              low=mid+1;
              }
     return 0;
can any one tell me the time complixity for this algo

- manidavid0 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using linear search strategy:
for(i=0;i<n;i++)
        if(A[i]!=0)
            return i+1;
    return 0;

- manidavid0 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to opimize the solution.
we dont need to compare the no.
just do addition and subtraction.

sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
index=n-sum;
return index+1;

i have a doubt , which is more expensive comparison or addition ?

- Anonymous March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void modifiedBinarySearch(int arr[], int left, int right, int &pos)
{

if (left<right)
{
int mid = (right + left)/2 +1;
if (arr[mid] == 0)
{
modifiedBinarySearch(arr, left, mid-1, pos);
modifiedBinarySearch(arr, mid+1, right, pos);
}
else
{
pos = (pos > mid) ? mid : pos;
modifiedBinarySearch(arr, left, mid-1, pos);
}
}
else
return;
}



int main()
{
int arr[6] = {0, 1, 1, 0, 1, 1};
int position=6;
modifiedBinarySearch(arr, 0, 5, position);
cout<<position<<endl;
return 0;
}

- L ^ V YY April 14, 2012 | 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