Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Here is one good solution using modified binary search,

puddleofriddles.blogspot.com/2011/12/given-array-of-unsigned-integers-which.html

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

will do a linear scan and return i as index for max number such that the following condition is satisfied.

a[i-1]<=a[i]>=a[i+1]

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

ur solution gives O(n), it can be solved in log(n) using modified binary search

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

yes, on every middle element , we will check the above condition is true or not.

int BinarySearch(int a[], int l, int h){
int m=(l+h)/2;

if(m-1 >=l && m+1<=h){
if(a[m-1]<a[m]<a[m+1])
return a[m];

else if(a[m-1]>a[m]>a[m+1])
return BinarySearch(a,l,m);

else
return BinarySearch(a,m,h);
}
}

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

You mean:

if(a[m-1]<a[m]) && (a[m]>a[m+1]))
return a[m];

on that first condition

- eugene.yarovoi December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ashish

Your algorithm will fail in case of repeated integers.

For Example: 1 2 2 3 3 4 4 4 3 3 2 1 1

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

I would think "increasing and then decreasing" means "strictly increasing and then decreasing". So there wouldn't be any repeated integers.

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

@VArora.mnnit: how was the interview? Did you get the offer?

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

we can modify the binary search

int find(int a[],int n)
{
 int low=0,up=n;
 int mid;
 while(low<=up)
 {
   mid=(up+low)/2;
   if(a[mid]>a[mid-1]&&a[mid]>a[mid+1])
      return a[mid];
   else if(a[mid]>a[mid-1]&&a[mid]<a[mid+1])
   low=mid+1;
   else if(a[mid]<a[mid-1]&&a[mid]>a[mid+1])
   up=mid-1;

 }
 
}

- getjar.com/todotasklist my android app December 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

else if(a[mid]<a[mid-1]&&a[mid]>a[mid+1])
this condition cannot arise check it

- Anonymous January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static  int MaxValue(int[] arr, int L, int H)
        {
            if (L == H)
                return arr[L];

            int M = (L + H) / 2;
            if (M + 1 < arr.Length)
            {
                if (arr[M] > arr[M + 1])
                {
                  return MaxValue(arr, L, M);
                }
                else
                {
                  return  MaxValue(arr, M + 1, H);
                }
            }
            else
            {
               return MaxValue(arr, L, M);
            }

        }

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

can be solved in log n using modified binary search..

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

we can have repetitions in the array since it is increasing and decreasing not monotonically increasing and decreasing

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

Is the array increasing till half of the array or it could be till n-1? How it is? From the question it is not clear..

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

any segment (increasing or decreasing or both ) can be null

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

This problem can be solved with ternary search.
let l = 0, r = n-1;
delta = (r - l) / 3;
e1 = l + delta, e2 = e1 + delta;
if (a[e1] < a[e2]) l = e1;
else r = e2;

- Alex Fetisov December 13, 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