Amazon Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




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

1. Sort the array
2. If minimum integers is greater than 0, product of maximum 3 integers is the answer.
3. If there exist at least 2 integers less than 0, find the product of mimimum 2 integers and 1 max integers. Compare it with the product of 3 maximum integers. Greater of the two is the answer.

Eg:

Given array : 10, 67, 28, 0, -1, -30, -20

sorted form : -30, -20. -1, 0, 10, 28, 67

product of 3 max integers : 10*28*67 = 18760

product of 2 min integers and 1 max int = (-30)*(-20)*67 = 40200

Answer : 40200

- incognitosj July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should include the case when there is 1 negative number.

- Nhung Nguyen July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not necessary to sort the array just to find the largest 3 positive numbers and the largest negative number.

- Anonymous July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No need to sort!
Maintain a minheap of 3 elements based on absolute values. height of this will always be 1

Complexity O(n)

- words&lyrics July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bhupendra : I agree this can be done without sorting. But there will be so many cases that it won't suffice to keep a minheap of height 3.

Eg input : -100, 12, 42, 89, 103

How will your logic work here?

- incognitosj July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I missed one condition!
After forming the heap once, If incoming element is larger than root replace!

In your example! Below is the heap formation and you need to compare only absolute values . -100 is converted to 100

------------
       12
100       42
-----------
       42
100     89
------------
       89
100       103
    
----------

- words&lyrics July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bhupendra : Can you plz explain it. How will you solve it further to get the answer??

- incognitosj July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@incognitosj

I think we need to do some more modification to above approach!

1. We need to maintain number of negative nodes inserted.
2. We need to keep track of greatest positive number from those inserted or deleted from the heap at any point of time.
say max.

In the final heap if number of -ve numbers is odd use the max from above along with other two positive nuumber
else you have the solution.

Note: Original numbers will be inserted in the heap with proper sign just comparison while inserting will be based on absolute values

In the above example:
Number of odd numbers in final heap is odd so we will discard the negative number and calculate product using other two and max (42)

Please comment if any error!

- words&lyrics July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bhupendra: One more case needs to be taken care of, where all the integers are less than 0. Then some more modification is needed.

- incognitosj July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have considered that case!
When the numbers of negative numbers are odd you will discard the number with minimum absolute value and use max instead

- words&lyrics July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually in case the integers are negative, it should use the minimum absolute value.
Eg: -10, -90, -9, -56, -32

answer should be (-9)*(-10)*(-32) and not (-90)*(-56)*(-32)

- incognitosj July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we need not to sort entire array.if we want to find max product of 3 number, so we have to run bubble sort loop three times and product last three numbers.

- Anonymous November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

The solution involves in finding three maximum and two minimum numbers. If the minimum numbers are negatives and if their product is greater than the two maximum number's product, then they have to considered for maximum product.

- Ani July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that case is taken two smallest negative number and one one maxm +ve number

- mahesh lora July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I don't think "the largest 3 numbers" is the right solution, the numbers may be negative, right? two negative numbers' product is still positive. Look at the following example:
6 3 2 -100 -101
The first 3 number is 6 3 2, the product = 36. But the largest product here should be 6 * -100 * -101.

- little-eyes July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are right.
and I think we should first find 6 numbers:the max 3 positive and the max 3 negitive.
the 3 number must in this 6 numbers...

- kurskk141 July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kurskk141, this solution is still not perfect, we still need to figure out the min 3 negative. So the solution must be in max 3 positive, max 3 negative and min 3 negative. Of course the length of the array should be larger than 9.

- little-eyes July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One thing for sure that we have to find the max positive number first. Then we can find the max 2 positive number and max 2 negative number besides it. Compare the two product to get the maximum.

- emma.wang July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if there are no positive numbers. The product in that case shall be the min product.

- Yoda July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This had so many cases i ended up hardcoding a lot. Let me know if this can be improved.
Some code:
Test cases:
1) has less than 3 elements
2) has only 3 elements;
3) has all zeroes
4) has less than 3 positive +negative numbers
5) has atleast 3 positive 2 negative
6) has atleast 2 positive 3 negative
7) has atleast 1 postive and 3 negative

int Find_maxProduct(int A[n])
{
	MinHeap minHeap;//has all Max values
	MaxHeap maxHeap;// has all min values
	if(n<3) return -1;
	if(n==3) return A[0]*A[1]*A[2];
	bool hasZero;
	for(int i=0; i<n-1; i++)
	{
		if(A[i]==0) {hasZero=true; continue;}
		if(i<3)
		{
			minHeap.push(A[i]);
			maxHap.push(A[i]);
			continue;
		}
		if(A[i]>0 && A[i]>minHeap.top())
		{
			minHeap.pop();
			minHeap.push(A[i]);
		}
		if(A[i]<0 && A[i]<maxHeap.top())
		{
			maxHeap.pop();
			maxHeap.push(A[i]);
		}
	}
	int prod_pos=1;
	int prod_neg=1;
	int max;
	if(minHeap.size()+maxHeap.size()<3)
		{
		return (hasZero)?0; -1;
		}
	if(minHeap.size()==3)
		{
		prod_pos*=minHeap.top(); minHeap.pop();
		prod_pos*=minHeap.top(); minHeap.pop();
		max= minHeap.top();
		}
	if(minHeap.size()==2)
		{
		prod_pos=0;
		minHeap.pop();
		max=minHeap.top();
		}
	if(minHeap.size()==1)
		{
		prod_pos=0;
		max=minHeap.top();
		}	
	if(maxHeap.size()==3)
		{
		maxHeap.pop();
		prod_neg*=maxHeap.top(); maxHeap.pop();
		prod_neg*=maxHeap.top(); maxHeap.pop();
		}
	if(maxHeap.size()==2)
		{
		prod_neg*=maxHeap.top(); maxHeap.pop();
		prod_neg*=maxHeap.top(); maxHeap.pop();
		}
	return max* MAX( prod_pos, prod_neg);
}

- Yoda July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ jilong.liao
Thank you,I forget the situation which there is no positive numbers in the array,and in this situation ,we still need to check if there has a "0" in the array

- kurskk141 July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can be done in O(n) time
take 4 var x,y,z,p and w;
x=arr[0],y=arr[1],z=arr[2] and p=xyz;
traverse the array and take elements in w;
check if xyw>p or yzw>p or zxw>p
lets say xyw>p...then p=xyw and z=w;
so on

- season July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

You can get the first 3 maximum numbers in O(n), you can do it with 3 if statements

- Edu July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n log3 = n) time and O(3) space:

use a min heap of length 3. So while traverse the arr check each element if is bigger than the min in the heap and if so remove the min and add the new one. At the end we will have the max elements which you can do the product

- GKalchev July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is a sample Java code:

private final static int NUMS = 3;
 
 public int maxProduct(int[] arr) {
  int result = arr.length > 0 ? 1 : 0;
  PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
  for (int i = 0; i < arr.length && i < NUMS; i++)
   pq.add(arr[i]);
  
  for (int i = NUMS; i < arr.length; i++)
   if (pq.peek() < arr[i]) {
    pq.poll();
    pq.add(arr[i]);
   }
  
  while (pq.size() > 0)
   result *= pq.poll(); 
  
  return result;
 }

- GKalchev July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just saw the "jilong.liao" who has a point.

So if there are negative values we can do max heap of len 3 to keep the min values (just the same logic as the min heap).


Then we remove the first element from the max heap and return Math.Max("maxHeap.poll() * maxHeap.poll() * minHeap.peek()", "minHeap.poll() * minHeap.poll() * minHeap.poll()")

we still keep the Linear time and constant space

- GKalchev July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. sort the array say we get a[n]
2. compare (a[0] * a[1]) > (a[n-2] * a[n-1]) ? a[0] * a[1] * a[n-1] : a [n-1] * a[n-1] * a[n-3]

will it work ?

- rahul July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

get the biggest three (may <3) and the smallest three (may <3). The max product is the max of any of the three numbers taken from these 6 numbers (may <6). There are at most 20 combinations of these (at most) 6 numbers. This avoids convoluted code to handling different cases.

- jiangok2006 July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Creating a max heap using the numbers and if a negative number comes take the mod
Then apply heap sort only for first three elements.
The product of first three element will be the maximum

- ajaypathak July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution involves in finding three maximum and two minimum numbers. If the minimum numbers are negatives and if their product is greater than the two maximum number's product, then they have to considered for maximum product.

- Ani July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can do it using dividing the array into three and finding the max and min of each.so that we'll have three max and three min numbers from which the max product can be easily found.
that would just take log(N) time.

- sidd.scope July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Prod{
        public static void main(String[] arg)
        {
                int[] arr={4,3,6,2,1,8};
                int len_arr=arr.length;

                for(int i=0; i<len_arr; i++)
                {
                        for(int j=1; j<(len_arr-i); j++)
                        {
                                if(arr[j-1]>arr[j])
                                {
                                        int temp=arr[j-1];
                                        arr[j-1]=arr[j];
                                        arr[j]=temp;
                                }
                        }
                }

                int product = arr[arr.length-1]*arr[arr.length-2]*arr[arr.length-3];
                System.out.println(product);

- puja July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if all number are negative then we should take three smallest negative number

- mahesh lora July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I try to write C++ codes for this question;

#include <iostream>
using namespace std;

int main()
{
int array[] = {10, 60, 40, 0, -1, -60, -50, 200};
int n=sizeof(array)/sizeof(int);
cout<<"Before sorted: "<<endl;
for (int i=0; i<n; i++)
cout<< array[i] << " ";
int tmp;
for (int i=0; i<n-1; i++)
for (int j=0; j<n-1-i; j++)
if (array[j]<array[j+1])
{
tmp=array[j];
array[j]=array[j+1];
array[j+1]=tmp;
}
cout<<"\n\nAfter sorted: "<<endl;
for (int i=0; i<n; i++)
cout<< array[i] << " ";
cout<<endl;
int maxproduct1;
int maxproduct2;
maxproduct1=array[0]*array[n-2]*array[n-1];
maxproduct2=array[0]*array[1]*array[2];
cout<< "\nThe maxproduct of 3 numbers\n"<<(maxproduct1>maxproduct2?maxproduct1:maxproduct2)<<endl;
return 0;
}

- bestry August 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question seem simple, but is actually tricky. I think Yoda's answer is the closest here. What you want to do is breakdown the possible scenarios:

1) Positive Numbers >= 3
     a) No Negatives in array: Answer is just the product of the three max numbers.
     b) 2 or more Negatives: Calculate the product of two maximum negative numbers and the maximum positive number, and compare to above to find Answer
2) Positive Numbers == 2
     a) 1 Negative Number in array: Answer is just the product of the two positive numbers and the negative number
     b) 2 or more Negatives:  Answer is the product of two maximum negative numbers and the maximum positive number
3) Positive Numbers == 1
     a) Answer is the product of two maximum negative numbers and the positive number
4) Positive Numbers == 0
     a) Answer is the product of the MINIMUM negative numbers.

So you want to keep track of the 3 Max Positive, 3 Max Negative, and 3 Min Negative numbers to solve this problem. You can utilize 3 arrays to track these, or 3 Max/Min Heaps to track these. If you use an array of size 3 so that the numbers you're tracking are limited, you'll save a little bit of speed and memory compared to tracking all the numbers in a Heap (Heap insertion = O(log n)).

The algorithm is basically traverse through the array and keep track of the above numbers, and then find the max product by doing the multiplications based on the scenario above.

My C code: pastie.org/4410410

- SF August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
We can do it in O(n) by having the first 3 values in 3 variables and then maintaining a pointer to the minimum value. Now, when we iterate from i=3 to n, and find a value greater than that pointed by min ptr, update the variable pointed by min ptr and recalculate the min ptr. int *findmin(int *a, int *b, int *c) {{{ int *min; if (*a < *b && *a < *c) min = a; else if (*b < *a && *b < *c) min = b; else min = c; return min; }}} int maxProduct(int *arr, int len) {{{ if (len < 3) {{{ return -1; }}} int a = arr[0]; int b = arr[1]; int c = arr[2]; int *min = findmin(&a, &b, &c); int i; for (i = 3; i < len; i++) {{{ if (arr[i] > *min) {{{ *min = arr[i]; min = findmin(&a, &b, &c); }}} }}} return (a*b*c); }}} - Dev August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

following cases(assuming atleast 3 elements exist in an array) after sorting in nlog(n) in increasing order, If sign of last three elements are
case1) - - - than answer is product of last three numbers
case 2)if sign is - + + than answer is min{product of last two num; product of first two num}*third max
case 3)if sign is - - + product of first two min num*last num of array
case 4)if sign is + + + ans is last num*max{product of first two num; product of second and third last num}

- cs1rangers September 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxThreeProduct {
	public static void main(String a[]){
		System.out.println("Enter the array");
		int no[] = {-45,-98, -4, -78, -9, -90, 10};
		int largestNo =Integer.MIN_VALUE, largerNo=Integer.MIN_VALUE, largeNo=Integer.MIN_VALUE;
		
		for(int i=0; i<no.length;i++){
			
			if(no[i]>largeNo && no[i]<largerNo){
				
				largeNo=no[i];
				
			}
			else if(no[i]>largerNo && no[i]<largestNo){
	
				largeNo=largerNo;
				largerNo = no[i];
			}
			else if(no[i]>largestNo){
					
					largeNo = largerNo;
					largerNo = largestNo;
					largestNo = no[i];				
					
				}
		}
		
		System.out.println("Largest : "+largestNo);
		System.out.println("Larger : "+largerNo);		
		System.out.println("Large : "+largeNo);
		System.out.println("Max Product is : "+largeNo*largerNo*largestNo);
		}
		
	
	}

- praveen October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;


public class MaxProductOfThreeNumberTest {
		public static void main(String[] args){
			int[] Arr={-25,-1,1,2,3,7,20};


			int MaxProduct=0;
			quickSort(Arr,0,Arr.length-1);
			System.out.println(Arrays.toString(Arr));
			if(Arr.length==3){
				MaxProduct=Arr[0]*Arr[1]*Arr[2];
			}
			else if(Arr.length>3){
				if((Arr[0]<0)&&(Arr[1]<0) ){
					if((Arr[0]*Arr[1])> (Arr[Arr.length-3]*Arr[Arr.length-2])){
						MaxProduct=Arr[0]*Arr[1]*Arr[Arr.length-1];
					}
					else{
						MaxProduct=Arr[Arr.length-3]*Arr[Arr.length-2]*Arr[Arr.length-1];
					}
				}
			}
			if(Arr.length>=3){
				System.out.println("MaxProduct :"+MaxProduct);
			}
			else{
				System.out.println("Array lenght should be equal or greater than 3");
			}
			
			

		}
		
		private static int partition(int[] list, int first, int last) {
		    int pivot = list[first];
		    int low = first + 1;
		    int high = last;

		    while (high > low) {

		        while (low < high && list[low] < pivot) {
		            low++;
		        }


		        while (low < high && list[high] >= pivot) {
		            high--;
		        }


		        if (high > low) {
		            int temp = list[high];
		            list[high] = list[low];
		            list[low] = temp;
		        }
		    }
		    while (high > first && list[high] >= pivot) {
		        high--;
		    }

		    if (pivot > list[high]) {
		        list[first] = list[high];
		        list[high] = pivot;
		        return high;
		    } else {
		        return first;
		    }

		}

		private static void quickSort(int[] list, int first, int last) {
		    if (last > first) {
		        int pivotIndex = partition(list, first, last);
		        quickSort(list, first, pivotIndex - 1);
		        quickSort(list, pivotIndex + 1, last);
		    }
		}
		
}

- Ranjeet April 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

O(n log n) solution:
Sort the array descending order.
find product of first 3 numbers.

- sk July 17, 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