Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Should the hopping happen only in 1 direction or it can happen in both directions?
Should a position which is already hopped to can be hopped again, otherwise there can be infinite number of hops?

- Naveen Reddy Mandadi April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Sr - hope you do well in your interview.
Could you pls explain this question more clearly? I don't think i understood the problem well. Is this ALL that was given? Why would the array being sorted make a difference?

- prashant April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The way I understand this we are given an array. The positions at which there is no bunny will be set to 0. The position where there is a bunny will be set to 1. For example in the array {0,0,1,1,0,0,0,1,0,1,1,0,0} there are 5 bunnies at positions 2,3,7,9,10.
So given a position of a bunny, we need to know how many times the bunny can hop.
The first question to ask is when can the bunny stop hopping? This should happen when the bunny reaches either end of the array(0 or Length-1).
Another case when the bunny has to stop hopping is when the next position it can hop to is already occupied.
We can write a few functions to accomplish this

//MinPos = 0 initially, CurrentPos is the position of the bunny we are interested in. EndPos is L-1 where L is the length of the array.
int ValidHops(int arr[],int MinPos,int CurrentPos,int EndPos)
{
return LeftHops(arr,MinPos,CurrentPos) + RightHops(arr,CurrentPos,EndPos);
}

//find the position of the next bunny
//calculate position after jumping over it.
//terminate when the final position is either>=high or its occupied.

int RightHops(int arr[],int StartPos,int MaxPos)
{
	//we know that StartPos is occupied
	if (StartPos>=MaxPos) return 0;

	//find the next bunny
	for(int i=StartPos+1; ((i<=MaxPos)&&(arr[i]!=1));i++);
            if (i==MaxPos) return 0; //no more valid hops if reached the end

	int nextPos = 2*i-StartPos;
	if (nextPos>MaxPos) return 0; //reached the right end of the array
            if (arr[nextPos]==1) return 0; //the next position is occupied therefore cannot proceed further

	return 1+RightHops(arr,nextPos,MaxPos);
}

int LeftHops(int arr[],int StartPos,int MinPos)
{
	//we know that StartPos is occupied
	if (StartPos<=MinPos) return 0;

	//find the next bunny
	for(int i=StartPos-1; ((i>=MinPos)&&(arr[i]!=1));i--);
            if (i==MinPos) return 0; //no more valid hops if reached the end

	int nextPos = 2*i-StartPos;
	if (nextPos<MinPos) return 0; //reached the right end of the array

            if (arr[nextPos]==1) return 0; //the next position is occupied therefore cannot proceed further

	return 1+ LeftHops(arr,nextPos,MinPos);
}

- retardedgenius April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't the condition be a[i] ==1 , as we are checking that where is the next bunny. Given that in an array we assume that if there is a bunny its position is marked as 1.

- scofield April 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have missed one condition.
a bunny can only hop over one bunny.

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

I think here bunnies are only numbers like if a bunny is at the position 2 and the other is at 4 then the array will be 2,5bunny at 2 can hop to the position 8 as 2*5-2=8 so the new array after the hop will be 5,8.

- V.dineshkumar July 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