Amazon Interview Question for SDE1s


Country: India
Interview Type: Phone Interview




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

If the question is really just to find the min and max in a[25],...,a[75], I don't see the point of specifying the start and end locations instead of just finding the min and max in the whole array. I think we need some clarification.

Anyway, if it is really just to find the min and max, here is a slightly better way to do it:

public void findMinMax(int[] array, int start, int end) {
	if (start < 0 || end >= array.length || start > end) 
		System.out.println("Invalid inputs");
	else {
		int tmpMin, tmpMax;
		int min = end;
		int max = end;
		// for loop checks even number of entries starting from array[start], 
		// up to array[end] or array[end-1], depending if (end-start+1)%2==0 or not
		for (int i = start; i < end; i+=2) {
			if (array[i] <= array[i+1]) {
				tmpMin = i;
				tmpMax = i+1;
			}
			else { // array[i] > array[i+1]
				tmpMin = i+1;
				tmpMax = i;
			}
			min = ( array[min] <= array[tmpMin] ) ? min : tmpMin;
			max = ( array[max] >= array[tmpMax] ) ? max : tmpMax;
		}
		System.out.println("Min = " + Integer.toString(array[min]) + " at index " + Integer.toString(min));
		System.out.println("Max = " + Integer.toString(array[max]) + " at index " + Integer.toString(max));
	}
}

It takes 3 comparisons for every 2 array entries, while the naive solution takes 2 comparisons for every entry.

- chriscow January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

It looks to me that you are doing 4 comparisons for 2 indices. i==end will be false for all indexes except last so you have to do the next comparison regardless and then the other two comparisons. I may be missing something...
But anyway, readability seems to be compromised.

- Anup January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anup: you are right. I've modified the code a little bit. Now it should be correct.

- chriscow January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@chriscow: Shouldn't the following two lines:

int min = array[end];
int max = array[end];

be

int min = end;
int max = end;

Please correct me if I am wrong.

- Nishant Kelkar January 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nishant Kelkar: you are right. corrected

- chriscow January 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with you. My mistake I meant

min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;

sorry about that.

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't see the need to use any sorting technique.... We could just start the index from 25 and scan up to 75 and simultaneously find max and min and this is 0(n)....one might argue tree is better but there's an overhead constructing the tree itself so simple scan will suffice...

- Veena January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this was the basic starting question the interviewer start with which might be converted to basic segmented trees.. i dont think such a simple question makes any sense otherwise

- kkr.ashish January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

rawIswar and kkr.ashish you guyz are right but needs to use only array..it was just a telephonic round..looking for approach and seems more like a screening round..

- Ruhan January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What about a merge sort in O (n log n) from index 25 to 75. so that Min = a[25] and Max = a[75].

mergeSort(a,25,75);
	int min = a[25];
	int max = a[75];

In case we cannot modify the array, we can iterate from a[25] to a[75] and keep track of min and max

Complexity O (n) where n = 75 - 25 ;

int min = Integer.MIN_VALUE;
 	int max = Integer.MAX_VALUE;
 	for(int i = 25; i <= 75; i++){
		if( array[i] < min) min = array[i];
		if(array[i] > max) max = array[i];
	}

Another option will be:
A min heap and a max heap out of the range of the array. in O (n/2) each

There are many solutions to the problem. I wonder why they were asked an specific range, 100 elements is still a small size for that range.

Any more ideas?

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your 2nd solution will only output min = Integer.MIN_VALUE and max = Integer.MAX_VALUE.......

Can any array[i] be < min or > max?? OMG!

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, I meant the following:

min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Segment Trees if the queries is too high.. or just do a min,max

- kkr.ashish January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

#include "stdio.h"

void printMinMaxInRange(int start, int end, int array[])
{
	int i = 0;
	int min = end;
	int max = start;
	while (i <= ((end - start) / 2))
	{
		if (array[i+start] > array[max])
			max = i + start;
		if (array[end - i] > array[max])
			max = end - i;
		if (array[i+start] < array[min])
			min = i + start;
		if (array[end - i] < array[min])
			min = end - i;
		i++;
	}
	printf("max %d at index %d\nmin %d at index %d\n",
		array[max],max,array[min],min);
}


int main(int argc, char *argv[])
{
	srand(time(NULL));
	int a[101];
	int i = 1;
	while (i < 101)
	{
		a[i] = rand() % 1000;
		i++;
	}
	printMinMaxInRange(25,75,a);
}

- ZSGoldberg January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain the reason to do the scan in both directions?

How is it different from simply scanning from start to end?

for (int i = start; i <= end; i++) {
	if ( array[i] > array[max] ) max = i;
	if ( array[i] < array[min] ) min = i;
}

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fewer times through the loop, I considered the two to be equivalent. Looking at it now I think going through the extra loops is worthwhile for cleaner looking code

- ZSGoldberg January 25, 2014 | Flag


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