Amazon Interview Question for Interns


Country: India
Interview Type: In-Person




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

This is working code.
Please check it and give any comment if you have.

int binarySearchOnCircularArray(int arr[], int arrSize, int key)
{
    int mid, start=0, end1=arrSize-1;
    int index=-1;
    if(arr[start] == key)
        index = start;
    else if(arr[end1] == key)
        index = end1;

    while(index == -1 && start != end1 -1)
    {
        mid = (start+end1)/2;
        if(arr[mid] == key)     //If found the element
            index = mid;
        else if (arr[mid] < arr[start]) //Right part of mid is sorted array...
        {
            if(key > arr[mid] && key < arr[end1])
                start = mid;
            else
                end1 = mid;
        }
        else    //Left part of mid is sorted array....
        {
            if(key > arr[start] && key < arr[mid])
                end1 = mid;
            else
                start = mid;
        }
    }

    return index;
}

- Rahul October 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

This one is often asked / talked about:

blogs.msdn.com/b/bali_msft/archive/2009/02/03/search-for-a-number-in-shifted-sort-array-within-o-log-n-time.aspx

- Sri September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Post +1

- Anonymous September 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def searchB(arr, start, end, key):
length = len(arr)-1
if start>end:
end = length+end
while(start<end):
mid = (start+end)/2
if key == arr[start%length]:
return start%length
elif key == arr[end%length]:
return end%length
elif key == arr[mid%length]:
return mid%length
elif key>arr[mid%length]:
start = mid+1
else:
end = mid
return -1

- anon October 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public int searchB(arr, start, end, key)
{
length = arr.length-1;
if (start>end)
{
end = length+end;
}
while(start<end):
{
mid = (start+end)/2
if(key == arr[start%length])
{
return start%length;
}
else if (key == arr[end%length])
{
return end%length;
}
else if (key == arr[mid%length])
{
return mid%length;
}
else if (key>arr[mid%length])
{
start = mid+1;
}
else
{
end = mid;
}
}
return -1
}

- anon October 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CircularBinSearch {
	
	
	private int[] arr;
	private int offset;


	public CircularBinSearch(int[] arr, int offset){
		this.arr = arr;
		this.offset = offset;
	}
	
	private int getValue(int index){
		return this.arr[getWithOffset(index)];
	}

	private int getWithOffset(int index) {
		return (index+offset)%this.arr.length;
	}
	
	
	public int search(int value){
		int l = 0;
		int r = arr.length;
		int result = -1;
		while(l<r){
			int center = (l+r)/2;
			int currValue = getValue(center);
			if(currValue == value){
				l = center;
				break;
			}
			if(currValue<value){
				l = center+1;
			}else{
				r = center - 1;
			}
		}
		
		if(value == getValue(l)){
			result = l;
		}
		return getWithOffset(result);
	}
	
}

- Pawel December 09, 2013 | 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