Amazon Interview Question for Software Engineer / Developers

• 0

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

Tested, this should work .

``````int	FindRotationPoint(int *input, int beg, int end )
{
if ( beg == end )
return beg;

//edge case : no rotation
if ( input[beg] < input[end] )
return beg;

int middle = (beg+end)/2;

// lucky, find the one, no need further recursion
if ( middle > 0 && input[middle -1] >input[middle] )
return middle;
else if ( input[middle] >= input[beg] )
return FindRotationPoint(input, middle+1, end );
else
return FindRotationPoint(input, beg, middle-1);
}``````

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

Binary search!!!

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

``````int find(int a[], int s, int e)
{
if((e-s)==1)
return e;
int m;
m=(s+e)/2;
if((a[e]-a[m])>0 && (a[m-1]-a[s])>=0)
return m;
if((a[e]-a[m])>0)
return find(a, s, m-1);
else
return find(a,m,e);
}

int main()
{
int a[7]={13,24,35,46,0,4,12};

cout<< find(a,0,6);
return 0;``````

}

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

It does not work for {5,8,10,1,2,4,5}
according to your solution it returns the index of 8
you need a check if(a[m]<a[m-1]) return m as soon as you calculate m

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

The array will be {6,8,10,1,2,4,5} although the earlier one also gives the same wrong result

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

int find_number(int * a, int x ) {

int low= 0;
int up=strlen(a)-1;
mid= low+up/2;
int prev_low=prev_up=0;

while(low<=up){
if(prev_low >low) {
return prev_low;
}
else if(prev_up<up){
return prev_up;
}
elseif(a[low]< a[mid]){
prev_low=low;
low=mid+1;
}
else{
prev_up=up;
up=mid-1;
}
}
}

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

just do the modified version of the binary search :)
int ModifiedBinarySarch(int arr[],int n)
{
int start=0;
int end=n-1;
int mid;
while(start<end)
{
mid=(start+end)/2;
if(a[mid]<a[mid-1])
break;
if(a[mid]>a[start])
{
start=mid+1;
}
else
{
end=mid-1;
}

}

}

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

ohh just return the mid

one trivial check should be done that first a[0]<a[n-1] if so then no rotation has been done return 0;

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

This checks for all the cases!! ie if mid value falls on one side along with lower bound or upper bound, then we have to adjust the range as well.

``````int
main()
{
int a[]={ 4, 5, 6, 7, 8, -1, 0, 1, 2, 3,};
int n=sizeof a/sizeof(int);

int lb=0, hb=n-1, mid;

while(1)
{
mid=(lb+hb)/2;

if(a[mid-1] > a[mid])
break;

if( a[mid] > a[0])
{
lb=mid;

if(a[hb] > a[0]) //make sure we span whole array
hb=n-1;
}

else
{
if(a[lb] < a[0])
lb=0;

hb=mid;
}
}

//print the mid value now

}``````

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

This will return the correct output

``````int rotatedArraySearch (int[] array) {

if (array.length < 1)
return -1;

if (array.length < 2)
return 0;

int start, end, mid;
start = 0;
end = array.length-1;

while(start < end) {
mid = (start+end)/2;
if (a[mid] < a[mid-1])
return mid;
if (a[mid] > a[start])
start = mid+1;
else
end = mid-1;
}

}``````

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

There is no mention of sorted in ascending order or descending order. Solution will be different for both of them.

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

Just find min ele or max (depending on asc r descending) using binary
search and return index of that.
Correct me if its wrong.

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.

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.