Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

1. pick the middle and check

a. A[mid-1]<A[mid]<A[mid+1] if true, this means the transition point is between mid and the right
b. A[mid-1]>A[mid]>A[mid+1] if true, this means the transition point is between mid and the left

also check if

c. A[mid-1]<A[mid] && A[mid]> A[mid+1] this means the mid is the transition point.

recursively pick the side that contains the transition point.

public static int findMax(int[] A, int left, int right){
	    if(right-left==1){
	        if(A[left]>A[right])
	            return left;
	        else
	            return right;
	    }
	    int mid = (left+right)/2;
	    if(A[mid]>A[mid-1] && A[mid]>=A[mid+1]){
	        return mid;
	    }
        if(A[mid]>A[mid-1] && A[mid]<=A[mid+1]){
            return findMax(A, mid+1, right);
        }
        if(A[mid]<A[mid-1] && A[mid]>=A[mid+1]){
            return findMax(A, left, mid-1);
        }	    
	    return -1;
	}

- GeeBee September 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Make sure to check that mid-1 and mid+1 are inside the array.

- Anonymous October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Its like increasing slope and then decreasing slope. We have to find the highest point.

That highest point cud be found out by using binary search, and check if

a[mid-1] a[mid] a[mid+1] is in increasing or decreasing order.

if increasing , move to right half
if decreasing, move to left half

Stopping criteria: a[mid-1] < a[mid] > a[mid+1]

- bebo September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good sol. I would also make sure to check for cases when K = 0 or K = array.length-1

- Corrector September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary search?

- Anonymous September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary search works on sorted array

- Amar September 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

improved binary search dear

- getjar.com/todotasklist my android app September 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

I presume k is not known, and that is an important piece of info which you ought to provide.

- Anonymous September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

k+1 is the maximum element, so if they give k, then finding the maximum element is trivial.

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

k could also be the max. But yes, still trivial.

- JeffD December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a variation of - If a sorted array has been rotated (circularly) then find the maximum element.
(1 2 3 4 5) rotated to (4 5 1 2 3)

- Sandy September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

After rotation are the element in decending order?

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

int FindMax(int A[], int size)
{
  return FindMaxRec(A, 0, size - 1);
}

int FindMaxRec(int A[], int start, int end)
{
  if (start == end)
  {
    return A[start];
  }

  int mid = (start + end) / 2;

  if (A[mid - 1] < A[mid] && A[mid + 1] < A[mid]) return A[mid];
  
  if (A[mid - 1] < A[mid]) return FindMaxRec(A, mid + 1, end);

  return FindMaxRec(A, start, mid - 1);
}

- Anonymous September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

heyyyy........i think we have to compare the Kth and k+1 th element to get the max element.......let me know its right or not.

- abhrajuit September 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u mean to say he will tell value of k and u need to find that number at kth position
:P

- Lol @abhrajuit September 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

perform bitonic sort

- Abhinav September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

find first case while scanning array where you find an element is > next element. i.e. go on comparing the adjacent array elements. the first case where you find element (i) > element (i+1)stop here. ith element is the maximum. comments welcome!

- anucjs September 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so what is the use of shorted array still the time complexity will O(n)

Use modified binary search... compare the middle element lay in which side of array..

- sanjay.2812 September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If till K array is in ascending order then Kth element is highest element (0, k) and then if array is in descending then K+1st element in highest among rest (K+1, nth)
So, compare highest elements in each partition i.e k and k+1st element
return max(a[k], a[k+1]).

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

int method(int a[],int n)
{
	int m,l=0,r=n;
	
	while(l<r)
	{
		m=(l+r)/2;
		if(m>0 && m<n)
			{
			if(a[m-1] <= a[m] )
			{
				if( a[m] <= a[m+1])
					l=m+1;
				else
					break;
			}
			else
				r=m-1;
			}
		else
			break;
	}
	
	cout<<a[m];
return 0;
}

- Anonymous October 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max(int[] num)
{
int i = 0;
while (i < num.Length)
{
while (num[i] < num[i + 1])
i++;
break;
}
return num[i];
}

- Anonymous November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Acc. to this code we don't need the value of k, since it is sorted in ascending order till k and descending order (k+1) onwards, we can get the answer with the following code.
Am I missing anything ?

int max(int[] num)
{
int i = 0;
while (i < num.Length)
{
while (num[i] < num[i + 1])
i++;
break;
}
return num[i];
}

- rnishkam November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the array would be like 1,2,3,4,5,6,10,9,8,7 OR 1,23,4,5,6,12,11,10,9. So, we need to arrive at the kth value and then, we need to identify if kth value is f=greater than the k+1th value. I think you missed that.

How do you think?

Thanks,
-Pavan.

- Pavan April 22, 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