Amazon Interview Question
SDE-2sTeam: Fraud prevention
Country: India
Interview Type: In-Person
@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
Yes this works. In fact, Mathematics has a name for the theorem behind it: Rearrangement Inequality.
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.
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;
}
what if both arrays contains negative numbers?
array1: -25,2,3,5
array 2: 23,-3,7,9
then ??
@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.
-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
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....
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
Solution I suggest is this:
- pavi.8081 July 11, 2013sort 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 ?