Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

int main() {
int n = 0;
for(int i = 0; i < array.size()-1; i++) {
if(array[i+1] - array[i] > 0)
n++;
else
break;
}
cout << ++n%5 << endl;
return 0;
}

- tryhard March 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you probably can find tons of answers everywhere. They key is to use binary search to find the index of that smallest element and also make sure to ask the elements are unique.

using System;
					
public class Program
{
	public static void Main()
	{
		int[] testArray = {7, 9, 11, 12, 15};
		Console.WriteLine(BinarySearch(testArray , 0 , testArray.Length-1));
	}
	
	public static int BinarySearch (int[] rotatedArray , int min , int max)
	{
		if (min > max)
		{
			return 0;
		}
		else if (max == min)
		{
			return min;
		}
		else
		{
			int mid = (min+max)/2;
			
			if (mid-1 > 0)
			{
				if (rotatedArray[mid-1] > rotatedArray[mid])
				{
					
					return mid;
				}
			}
			if (mid+1 < max)
			{
				if (rotatedArray[mid] > rotatedArray[mid+1] )
				{
					
					return mid+1;
				}
			}
			if (rotatedArray[mid]  < rotatedArray[max])
				
				return  BinarySearch (rotatedArray , min ,  mid-1);
			else
				return  BinarySearch (rotatedArray , mid+1 ,  max);
				
			
		}
		
	}
}

- mohamadreza.shakouri April 04, 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