Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Hey folks. I think you are all missing a very important hint that the array is sorted and rotated by some K positions. In that case we should use the binary search principle to find the reset point, which will be the minimum element in the array. Below is the complete code to demonstarte this technique that runs in O(Log N) time:

#include <iostream>
using namespace std;
/* Find the reset point, i.e. the minimum element's index in 
   a sorted array that has been rotated (left or right) by some 
   number of positions.
*/
int find_reset_point(int arr[], int sidx, int eidx)
{
    int midx;
    if (eidx - sidx <= 0)
        return -1;
    while (sidx < eidx){
        if (eidx - sidx <= 1){
            if (arr[sidx] > arr[eidx])
                return eidx;
        }
        midx = sidx + (eidx - sidx) / 2;
        if (arr[midx] > arr[sidx])
            sidx = midx;
        else 
            eidx = midx;        
    }
    return -1; // No reset point detected, array is sorted ascending
}
int main(int argc, char *argv[])
{
    //int arr[] = {4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3};
    int arr[] = {10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    //int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    int cnt = sizeof(arr)/sizeof(arr[0]);
    int idx;

    idx = find_reset_point(arr, 0, cnt - 1);
    cout << "IDX: " << idx << endl;

}

- ashot madatyan May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you cann't use binary search on a linked list....

- Anonymous May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can't use binary search on a linked list....

- Anonymous May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The original problem says " Given a sorted list ... "
I presume that the "list" here is just a loose semantics for an array, which is iterable randomly. If the input is nevertheless a LinkedList, then yes, you cannot use BS on it efficiently (though it is implementable).

- ashot madatyan May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the correct implementation. It's sort of a trick question because linear traversal is obvious but since it's semi sorted, binary search can cut the search down to O(long). please use better notation next time, instead of sidx and eidx use left and right... it makes the readability a lot easier.

- rdo May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 5 vote

In case of ascending order, just find the first element which is less than its previous element.

for (i=1; i<n; i++)
{
    if (a[i] < a[i-1])
        return i;
}
return 0;  // in case when there is no rotation.

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

this logic can be extended to work for linked lists...jus check if current element is greater than the next element( if yes: then the next element is the starting point of the sorted list)

- suraj May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the linked list is sorted in ascending order then find min in the array.
if the LL is sorted descending order find the maximum element.

we also have to take care of duplicate elements by using 2 pointers.

O(n) complexity.

- krishna May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this need to be linked list
given a rotated sequence find the min value

- Anonymous May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this need NOT to be linked list
given a rotated sequence find the min value

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

Could someone tell me, what it means by 'rotating a LL'.

- Pavan Dittakavi May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The rotation of a string or list is just pushing the values as though they were in a circular list:

orig: 1 2 3 4 5 6 7
rotate: 5 6 7 1 2 3 4

orig: "water"
rotate: "terwa"

Another helpful hint when dealing with rotations of strings is if you concatenate the string to its self the original string will be included in the new string.

terwa + terwa = terWATERwa which contains the original as a substring.

- Anonymous May 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Anonymous for helping me out.

- Pavan Dittakavi May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution in normal JavaScript (can be quickly written on browser, and executed:-), and following Binary Search:

//returns the starting point of an array-segment
var minList = function(arr) {
  var len = arr.length;
    //checks whether the array-segment is sorted
    if(arr[0] <= arr[len-1]) {
      return arr[0];  //returns the min value in an array-segment
    } else {
      //else continue till you find a sorted array segment
      var arr2 = arr.splice(arr.length/2);
      var min1 = minList(arr);
      var min2 = minList(arr2);
      return Math.min(min1,min2);
    }
};

- devesh chanchlani May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just take the array a

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

the main point of this question is that if we take the mid point,either of the portion is sorted.We just have to pick the unsorted part and recursively find the point across which rotation exists.

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

//This approach gives you answer in n/2 steps. This function returns the index of the point where it is rotated
int func(int arr[])
{
int p1,p2;
for(p1=0, p2=arr.length-1; p1>=0,p2<arr.length; p1++,p2-- )
{
if(arr[p1]>arr[p1+1])
return p1;

if(arr[p2]<arr[p2-1])
return p2;

}
}

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

public static int findStartPoint(int [] array){
int left=0;
int right=array.length-1;

while (left<=right){
int mid=(left+right)/2;

if (array[mid]<array[mid-1]){
return mid;
}

if (array[mid]<array[left]){
right=mid-1;
}else if (array[mid]>array[right]){
left=mid+1;
}else if (array[mid]>array[left]&&array[mid]<array[right]){
return 0;
}

}

return -1;
}

- Scott May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct listNode {
   int item;
   listNode * next;
}

listNode * findFirst(listNode * head) {
   listNode * currentMin = head;
   listNode * current = head;
   while (current != null) {
      if (current -> item < currentMin ->item) {
         currentMin = current;
      }
      current = current -> next;
   }
   return currentMin;
}

- Anonymous June 01, 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