Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

We can do this using the binary search approach as following:

int rotatedSorted(int[] arr, int target){
  int left = 0;
  int right = arr.length-1;
  while(left<=right){
    int mid = left+(right-left)/2;
    if(arr[mid]==target)
      return arr[mid]
    if(arr[left]<=arr[mid]){
      if(arr[left]<=target && target<arr[mid]){
        right=mid-1;
      }else{
        left=mid+1;
      }
    }else{
      if(arr[mid]<target && target <= arr[right]){
        left=mid+1;
      }else{
        right=mid-1;
      }
    }
  }
  return -1;
}

- Joe April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find the smallest elements in the array using binary search. We need to find the element which is smaller than previous element.

If the required element is greater than last element, then we need to do binary search in arr[0, min-1] else binary search in arr[min+1, n-1]

- B_K_J April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This could be sollution whith O(logn) efficency

public static int SearchRotatedSortedArray(int l, int d, int[] array, int toFind) {
		if (l >= d) {
			return -1;
		}
		if (array[l] == toFind) {
			return l;
		}
		if (array[d] == toFind) {
			return d;
		}

		int mid = (l + d) / 2;
		if (array[mid] == toFind) {
			return mid;
		}

		if ((array[mid] > toFind && toFind > array[l]) || (array[mid] < toFind && array[d] < toFind)) {
			return SearchRotatedSortedArray(l, mid - 1, array, toFind);
		} else {
			return SearchRotatedSortedArray(mid + 1, d, array, toFind);
		}

	}

- Igor Skiljevic April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RotatedSortedArray {

static int findMin(int arr[], int low, int high) {


if (high == low) return low;
if (high < low) return low;

int mid = low + (high - low) / 2;

if (arr[mid + 1] < arr[mid] && high > mid) {
return mid + 1;
}
if (arr[mid - 1] > arr[mid] && mid > low) {
return mid;
}
if (arr[high] > arr[mid]) {
return findMin(arr, low, mid);
} else {
return findMin(arr, mid, high);
}
}

static int findPosRotatedArray(int arr[], int low, int high, int num) {
int pos = findMin(arr, low, high);
if (arr[pos] < num && num > arr[high]) {
return binarySearch(arr, num, pos - 1, low);
} else {
return binarySearch(arr, num, high, pos);
}
}

static int binarySearch(int arr[], int num, int high, int low) {
if (high < low) {
return -1;
}
int mid = low + (high - low) / 2;

if (num == arr[mid]) {
return mid;
} else if (num > arr[mid]) {
return binarySearch(arr, num, high, mid + 1);
} else {
return binarySearch(arr, num, mid - 1, low);
}

}

public static void main(String[] args) {
int arr1[] = {5, 6, -2, -1, 1, 2, 3, 4};
System.out.println(findMin(arr1, 0, arr1.length - 1));
System.out.println(findPosRotatedArray(arr1, 0, arr1.length - 1, 2));


}


}

- mail.smritiraj May 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithms.sorting;

import java.util.Arrays;

public class P2 {

    public static void main(String[] args) {
        Integer[] words = new Integer[]{
                5, 6, -2, -1, 1, 2, 3, 4
        };

        int foundIndex = findElement(words, -2);

        if(foundIndex == -1) {
            System.out.println("Not Found");
        }
        else {
            System.out.println("Found at : " + String.valueOf(foundIndex) + " : Word : " + words[foundIndex]);
        }

    }

    private static int findElement(Integer[] words, Integer element) {
        int mid = words.length / 2;
        if(words[mid].equals(element)) {
            return mid;
        }
        if(words.length == 1) {
            return -1;
        }
        if(words[mid] > element && (words[mid] < words[0] || element > words[0])) {
            return findElement(Arrays.copyOfRange(words, 0, mid), element);
        }
        else {
            return findElement(Arrays.copyOfRange(words, mid + 1, words.length), element);
        }
    }
}

- vaibhavsinh December 05, 2017 | 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