Amazon Interview Question
Software Engineer / Developerswhat is wrong with this..this was the solution I too came up with...either offer constructive criticism or none at all...Always remember, if you have nothing good to say, don't say at all
slight optimization :)
int mul(int x, int y)
{
if(y>x)
{
swap(x,y);
}
if(y == 0)
return 0;
else
return x+mul(x,y-1)
}
def mul1(a, b, c):
if c > b:
return (0, b)
else:
p, b = mul1(a + a, b, c + c)
return (p + a, b - c) if b >= c else (p, b)
def mul(a, b):
return mul1(a, b, 1)[0]
can use recursive addition here. can get the result in log(n) steps of the multiplier
int mul(int m,int n)
{
int v=0;
if(n==1) return m;
v = mul(m,n/2);
v = v+v;
if(n%2!=0) v+= m;
return v;
}
Ok it seems there exists a solution which performs in O(log(min(m,n)). To the guy who raised the question, are you sure bitwise is also restricted.
Yes, that running time is achievable using only addition, subtraction, comparisons, and recursion.
a more tedious/clumsy answer:
int mult(int x, int y)
{
if (0 == x) return 0;
if (0 == y) return 0;
if ((x < 0) && (0 < y))
{
x= 0-x;
return 0 - (x+ mult(x, y-1));
}
if ((y < 0) && (0 < x))
{
y= 0-y;
return 0 - (x+ mult(x, y-1));
}
if ((y < 0) && (x < 0)) // both are negative
{
x= 0-x;
y= 0-y;
return (x+ mult(x, y-1));
}
// if we get here, both are positive
return (x+ mult(x, y-1));
}
from all above+ -ve check
int mul2(int m, int n)
{
if(n==1)return m;
int v=0;
v=mul2(m,n/2);
v=v+v;
(n%2==1)?v=v+m:0;
return v;
}
int mul(int m, int n)
{
int flag=0;
if(m==0||n==0) return 0;
if(n>m) swap(m,n);
if(m<0){m=0-m;flag++;}
if(n<0){n=0-n;flag++;}
return (flag%2==0)?mul2(m,n):(0-mul2(m,n));
}
I came up with this version, take care of -ve as well
int main()
{
int a = -4, b = 3;
int result = a;
mul(&result, a, b);
cout << result << endl;
}
void mul (int *result, int num, int times){
if( times == 1)
return;
*result += num;
mul(result,num, times-1);
}
Hi,
Just thought to summarize the possible motivation behind this question.
- Check to handle negative number.
- Check for minimum iteration
- Check for recursion concepts.
- Check for bitwise operators
- Check for O(Log N) solution by n/2 recursive solution
- Check for basic ideology of multiply, which is addition.
- Check for overflow condition handling. ( For C#, One can also read about checked keyword)
There are multiple variants of this question depending on the exact problem statement from interviewer. One can easily fit subset of above check's into the problem and come up with a robust source code.
Please feel free to add more analytic to the motivation behind the question.
Thanks
Ankush
public static int multiplicationByAddition(int a, int b)
{
return (b < 0) ? -multiplycationRecursion(0, a, -b) : multiplycationRecursion(0, a, b);
}
private static int multiplycationRecursion(int sum, int a, int b)
{
if(b <= 0)
{
return sum;
}
else
{
return multiplycationRecursion(sum+=a, a, --b);
}
}
Hi,
- ankushbindlish May 02, 2010Just thought to summarize the possible motivation behind this question.
- Check to handle negative number.
- Check for minimum iteration
- Check for recursion concepts.
- Check for bitwise operators
- Check for O(Log N) solution by n/2 recursive solution
- Check for basic ideology of multiply, which is addition.
- Check for overflow condition handling. ( For C#, One can also read about checked keyword)
There are multiple variants of this question depending on the exact problem statement from interviewer. One can easily fit subset of above check's into the problem and come up with a robust source code.
Please feel free to add more analytic to the motivation behind the question.
Thanks
Ankush