Amazon Interview Question


Country: United States
Interview Type: Phone Interview




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

#include "stdafx.h"
#include <stdio.h>

int main() {
int A[] = {1,2,3,4,5};
int max1 = 0, max2 = 0;
for(int i = 0; i < 5;i++) {
if(A[i] > max1) {
max2 = max1;
max1 = A[i];
}
else if(A[i] > max2 && A[i] < max1) {
max2 = A[i];
}
}
printf("%d\n",max2);
}

- Dinesh March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is a kth order statistics problem can be solved in O(n).
Rand-Select() algorithm works too.
There is a rather special case (since its just second largest) - brute force as below.

namespace QuickPorjects
{
	class Program
	{
    	static void Main(string[] args)
    	{
        	int[] arr = new int[] { 7,1,2,3,4,5,8 };

        	FindTwoLargest(arr);
    	}

    	private static void FindTwoLargest(int[] arr)
    	{
        	int max = int.MinValue;
        	int max_second = int.MinValue;
        	int i;
       	 
        	//max =  arr[0];

        	for (i = 0; i < arr.Length; i++)
        	{
            	if (arr[i] > max)
            	{
                	max_second = max;
                	max = arr[i];
            	}
            	else if (arr[i] > max_second)
            	{
                	max_second = arr[i];
            	}
        	}

        	Console.WriteLine(max + " 	" + max_second);
    	}
	}
}

- whizz.comp March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Selection algorithm ..works in O(n) in avg case.

- yogeshhan March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Revised algorithm:

- Iterate the array from left to right and from right to left to find the largest element in each direction.
- When checking for the largest element in this direction, ignore the largest element from the other direction.
- Find the smallest elment in these two largest elements.

int findSecondLargest(int A[], int N)
{	
	int left = 0;
	int right = N - 1;	

	for (int i = left, j = right; i < N && j >= 0; i++, j--)
	{
		if (A[left] < A[i] && i != right)
			left = i;		
		if (A[right] < A[j] && j != left)			
			right = j;				
	}
	if (A[left] < A[right])
		return A[left];
	else
		return A[right];
}

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

above solution will fail if largest number occurs twice in an array

- shashank March 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

This can be solved in n +logn -2 comparision using tournament method

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

Can you please expand on that?
What betterment are you trying to achieve over this:

int *max, *secondmax;
max = secondmax = NULL;
for( i = 0; i < n; i++ )
{
	if( max == NULL )
		max = new int(a[i]);
	else if( *max < a[i] )
		if( secondmax == NULL )	secondmax = new int;
		*secondmax = *max;
		*max = a[i];
	else if( *secondmax < a[i] )
		*secondmax = a[i];

}

- y2km11 March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess your algo takes 2n comparison....

- Anonymous March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Build max-heap, remove the root and max heapify again. I guess this should take order n times.

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

Build Heap = O(n)
Heapify again = O(logn)
total time = O(n + lgn) ...
Tournament tree also has the same disadvantage for time and Space
it wud be better to use a simple variable to keep max and 2nd max.

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

To get the second largest
we can use bubble sort (2 times outer loop... N-2 is second largest element)
Or selection sort method as below.

#include <iostream>

using namespace std;

int main()
{
int a[] = {0,0,0,1,2,0};
int pos,temp;
int n = 6;

for (int i =0; i<2; i++)
{
pos = i;

for(int j= i+1; j<n; j++)
{
if(a[j] > a[pos])
{
pos = j;
}
}

if(pos != i)
{
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
}

cout << "Second Largest: "<< a[1] << endl;
}

- Mallinath March 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int 2ndLargest(int []a,int n)
{
int i,SL,L;
for(SL=L=a[0],i=1;i<n;i++)
if(a[i]>L)
{
SL=L;
L=a[i];
}
else
if(a[i]>SL)
SL=a[i];
return SL;

}
Complexity is = O(n-1)

- Nishchay March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct if the second largest element is in the same subarray as the largest one.

- LostAtSea March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct if the second largest element is in the same subarray as the largest one.

- LostAtSea March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong algo
5 4 3 1 1 2
it will give 2 as ans

- abc April 16, 2012 | 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