Amazon Interview Question for SDE-2s


Team: Fraud prevention
Country: India
Interview Type: In-Person




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

Solution I suggest is this:
sort 1st array in increasing order and sort 2nd array in decreasing order.
this arrangement of the 2 arrays will result in min sum of product of corresponding elements, that is:
a1*b1 + a2*b2 + ...+ an*bn = minimum..

your take ?

- pavi.8081 July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes i think it would work

- vgeek July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it should work

- Chaitanya July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@NaMo--both will give exactly same result,as the arrays are of equal size

- undefined July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Namo- both will give the same result as:
arr1- 1234
arr2-8765 will lead to -8*1+7*2+3*6+4*5
and the other way:
arr1-4321
arr2-5678 will lead to- 5*4+6*3+7*2+8*1 note: its just the reverse order

- vgeek July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this works. In fact, Mathematics has a name for the theorem behind it: Rearrangement Inequality.

- Anonymous July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I personally strongly dislike this kind of problem, which tests mathematical ingenuity more than coding ability. Might as well ask candidates to prove that this approach would always produce the minimal result.

- Sunny July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if both arrays contains negative numbers?
array1: -25,2,3,5
array 2: 23,-3,7,9
then ??

- bvarghese July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the implementation of @IM.first idea.

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
 
using namespace std;
 
#define N 5
 
int compare(const void *p1,const void *p2)  {
    
    return *(int *)p1-*(int *)p2;
    
}
 
int compare2(const void *p1,const void *p2) {
    return *(int *)p2-*(int *)p1;
}
 
 
int findMinimum(int *arr1,int *arr2)   {
    qsort(arr1,N,sizeof(int),compare);
    qsort(arr2,N,sizeof(int),compare2);
    int minimum=0;
    for(int i=0;i<N;i++)    {
        minimum+=arr1[i]*arr2[i];
    }
    return minimum;
}
 
int main()  {
    int arr1[]={3,2,7,4,-1};
    int arr2[]={4,12,10,8,9};
    cout<<"The minimum value is:"<<findMinimum(arr1,arr2);
    return 0;
}

- Sibendu Dey July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if both arrays contains negative numbers?
array1: -25,2,3,5
array 2: 23,-3,7,9
then ??

- bvarghese July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bvarghese: Doesn't matter as you have given two arrays.In the first array multiplying the -ve number with the largest number will result in a minimum value.Similarly the largest element from first array get multiplied with the -ve element from second array.

- Sibendu Dey July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

-6789 1 34
-6789 1 5

SORTED AND MULTIPLIED ACCORDING TO ABOVE DISCUSSION: ANSWER= 46090692

BUT MINIMUM VALUE POSSIBLE : -6789*1-6789*1+ 34*5= -13408

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

@Anonymous:-According to what I said..
first array in increasing order:- -6789 1 34
second array in decreasing order :- 5 1 -6789
minimum value = -6789 * 5 + 1*1 + 34*(-6789)

- Sibendu Dey July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

did you get through interview?

- viv July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This was question from my friend's interview. He could not make through because as per him Manager round didn't go well. So probably its the soft skills which let him down rather than technical.
Moreover, there were a couple of design questions asked in earlier rounds, so probably some things might have went wrong there as well, we're not sure.
Please have a look at those questions at:
question?id=21718663
question?id=21219666
Please people, suggest your solutions to these....

- pavi.8081 July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MinInnerProduct {
	public int solve(int[] array1,int[] array2){
		Arrays.sort(array1);
		Arrays.sort(array2);
		int toReturn=0;
		int negative1=negative(array1);
		int negative2=negative(array2);
		int l1=0;
		int r1=array1.length-1;
		int l2=0;
		int r2=array2.length-1;
		while(l1<=negative1 && r2>negative2){
			toReturn+=(array1[l1]*array2[r2]);
			l1++;
			r2--;
		}
		while(l2<=negative2 && r1>negative1){
			toReturn+=(array2[l2]*array1[r1]);
			l2++;
			r1--;
		}
		while(l1<=r1){
			toReturn+=array1[l1]*array2[l2];
			l1++;
		}
		return toReturn;
	}
	private int negative(int[] array){
		if(array[0]>=0){return -1;}
		
		for(int i=0;i<array.length;i++){
			if(array[i]>=0){
				return i-1;
			}
		}
		return array.length-1;
	}
	public static void main(String[] args){
		MinInnerProduct mip=new MinInnerProduct();
		int[] array1={-2,0,2};
		int[] array2={-2,-3,1};
		System.out.println(mip.solve(array1, array2));
	}
}

correct me if i did this wrong

- jicheninsjtu July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class C {

static int arr1[]={1,3,5,7,9};
static int arr2[]={10,8,6,4,2};

public static void main(String[] args) {

int min = 0;

for(int i=0;i<arr1.length;i++){

min += arr1[i] * arr2[i];
}

System.out.println(min);
}
}

- Ankit July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what do you mean by arrange the array? I don't understand the question. You want to calculate the value or what?

- avaln August 01, 2013 | 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