Amazon Interview Question for Software Engineer / Developers






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

Lets n = Size(array) - m;
So check for A[n] >X depending on that you know in which part of array it will be....

- tito March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int SearchCircularArray(vector<int>& v ,int m, int num){

	if(num > v.at(v.size() - 1)){
		return binary_search(v.begin(),(v.end() - m),num);	
			
	}else
	{
		return binary_search(v.end()- m,v.end() -1 ,num);	
	}
}


int main( )
{	
	int a [] = {3,4,5,6,1,2};

	vector<int> v(a,a+6);
	//Here m = 2 , meaning array is circularly shifted by 2 times
        //Fist parm:vector
        //Second: m
        //Third : Elemnt to be searched
        cout << SearchCircularArray(v,2,10);
	return 0;
}

Please let me know any other better approach than this or if i am missing something

- sachin323 March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if you do not know what the array is shifted by ?

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

if u dont know how much the array is shifted then also u can do it in O(lgn) time
first step then be to search for a pivote point means the rotated point after that it will be a similar question as given
whole algorithm:
int searchInRotated(int a[],int n,int key)
{
int end=n-1;
int start=0;
int s=0;
int e=n-1;
int mid=0;
if(a[0]>a[n-1])
{
while(s<=e)
{
mid=(s+e)/2;
if(a[mid]>a[mid+1])
break;
else
{
if(a[mid]<a[e])
{
e=mid-1;
}
else
{
s=mid+1;
}}}}
if (mid==0)
{
while(start<=end)
{
mid=(start+end)/2;
if(a[mid]==key)
break;
if(a[mid]>key)
end=mid-1;
else
start=mid+1;
}
return mid;
}
else
{
if(key==a[mid])
return mid;
if(key>=a[mid+1] && key <=a[end])
{
start=mid+1;
while(start<=end)
{
mid=(start+end)/2;
if(a[mid]==key)
break;
if(a[mid]>key)
end=mid-1;
else
start=mid+1;
}
return mid;
}
else
{
end=mid-1;
while(start<=end)
{
mid=(start+end)/2;
if(a[mid]==key)
break;
if(a[mid]>key)
end=mid-1;
else
start=mid+1;
}
return mid;
}
}
}

int main(void)
{
int k;
int a[10]={6,7,8,9,10,1,2,3,4,5};
k=searchInRotated(a,10,10);
printf("position of %d is :%d",a[k],k);
return 0;


}

- geeks July 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you don't know the break point, use binary search to find it. Looking at mid-1 and mid+1 should let you know which part of the broken list mid is in.

Then do binary search of the top or bottom part.

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

note that the index of original array elements become to (index-m)%N, where N is the array length. So we just need to modify the index of binary search to (N/2-m)%N.

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

crackprogramming.blogspot.com/2012/10/operations-on-rotated-sorted-array.html

- Anonymous November 02, 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