Amazon Interview Question for Software Engineer / Developers






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

IF array is sorted in increasing order
complexity - O(n)
In worst case all the elements might be equal to index value.
In this case we have to check for all i=(0 to n-1)

If array is sorted in decreasing order
complexity - O(logn)
Indexes are in increasing order and values are in decreasing order.
So, they can meet only at a single point. we can find this in O(logn)
using binary search

- var May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

this can be solved like this

1>crate another array Y=a[i]-i

problem now becomes finding aal occurences of 0 in Y

2>apply BinarySearch twice
a>one for finding first index of 0
b>second for finding last index of 0

3>print all elements from a[first] to a[last]

works for unique .. increasingly sorted number array

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

ya this one works :)

- cool frog May 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if there is another condition that all of elements are integer?

- wannuc May 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

perform a customized binary search since the array is sorted and logn solution is needed.
Hit the Middle of the Array and check if the Value of element is Greater than Index ? if so all elements to the right of it would follow the same trend so we can check at the elements to the left of it. If the value of the element is less than Index then check the right Half. If its equal to index, then check the element to the left and the right of it and so forth.
This gets a little tricky if the array has duplicate elements. In addition we may have to find whats the next unique and preceding unique element in the array.

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

the array should have all the element unique, otherwise log(n) not possible.Try to apply pigeonhole principle when deciding left/right side in binary search.

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

the array should have all the element unique, otherwise log(n) not possible.Try to apply pigeonhole principle when deciding left/right side in binary search.

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

public void printElement(int[] a)
{
   while(i<a.length)
   {
       if(a[i]==i)
          System.out.println(i);
       else if(a[i]>i)
         i=a[i];  //we can skip if a[i] >i
        else 
          i++;
   }
}

- MaYank May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't this O(n)?

- abc May 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes abc but I think there is no way ... I have to check each potential member at least I am skipping some element
else if(a[i]>i)
i=a[i]; //we can skip if a[i] >i

if the {0,1,2,3,4,5,6}
we have all of them as answer so it will be O(n)
if {0,1,5,6,7,7,7,7}
then {0,1,7}
at 5 I can skip next 4 i.e. 2+5

- MaYank May 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if there are negative values. There is nothing given about the array containing only numbers from 1.

- Mu May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int func(int *A, int begin, int end)
{
    if(begin>=end) return NOT_FOUND;
    int ptr = (end-begin)/2;
    if(A[ptr]==ptr) return ptr;
    if(A[ptr]>ptr) return func(A,begin,ptr);
    if(A[ptr]<ptr) return func(A,ptr,end);
}

Simple modification to the binary search :)
O(log n)

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

does your code work for {0,1,2,3,4,5,6,7}

- freemail165 May 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is small problem with this ,it won't catch the extremes because if base condition having <= equality.here is the other way (rather modified version of your code)

static void BinSearch(int low,int high,int[] arr,int key){
	if (low>high)
		return;
	else
	{
	  int mid=(low+high)/2;

	  if(arr[mid]==mid)
		  System.out.println("Key Found "+arr[mid]);
	  if(arr[mid]>=mid)
		  BinSearch(low,mid-1,arr,key);
	  if(arr[mid]<=mid)
		  BinSearch(mid+1,high,arr,key);
	  
	}
}

- Priyank May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumptions:
1) all positive integers.
2) no duplicates
3) input array sorted in increasing order
This means any elem in array is either greater than the index or is equal to index.

/* returns the index till which all elems in array are equal to index of array */
int search(int a[], int n)
{
   int lower = 0, upper = n-1;
   int mid;
   while(lower < upper) {
      mid = (lower+upper)/2;
      if (a[mid] == mid) {
         //good so far, search in right half.
         lower = mid+1;
      } else {
         upper = mid-1;
      }
   }
   if (a[lower] == lower)
     return lower;
   else 
     return (lower-1);
}

- cubic May 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think O(logn) soln is possible only when the elements r positive and distinct.......

- KK August 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah write..

- KK July 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this can be solved like this

1>crate another array Y=a[i]-i

problem now becomes finding aal occurences of 0 in Y

2>apply BinarySearch twice
a>one for finding first index of 0
b>second for finding last index of 0

3>print all elements from a[first] to a[last]

works for unique .. increasingly sorted number array

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

One of the best solutions to the above problem is to use the binary search in the array we will check for each of the array elements in the sorted array and we will check if the index at which the element is present must be equal to the array element of the sorted array (sorted in increasing order)

- swapnilkant11 July 27, 2019 | 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