Amazon Interview Question for Software Engineer / Developers






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

- iatbst January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Binary search!!!

- Anonymous July 20, 2011 | Flag Reply
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;

}

- Sonal July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anon October 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anon October 22, 2011 | Flag
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;
}
}
}

- rahul July 21, 2011 | Flag Reply
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;
}

}



}

- geeks July 22, 2011 | Flag Reply
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;

- geeks July 22, 2011 | Flag Reply
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

 }

- raja roy September 27, 2011 | Flag Reply
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;
	}

}

- Abhiraj December 29, 2011 | Flag Reply
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.

- ss July 07, 2012 | Flag Reply
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.

- miandfhyu July 25, 2013 | 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