Cadence Inc Interview Question
Software Engineer / Developersint product (int num1, int num2 )
{
if(num2 == 0 || num1 == 0)
return 0;
if (num2 == 1)
return num1;
if (num2 == -1)
return -num1;
if (num1 > 0 && num2 > 0)
return product(num1+num1,num2-1);
if (num1 > 0 && num2 < 0)
return product(num1+num1,num2+1);
}
This code will obviously have integer overflow problem..
A little correction..
int product (int num1, int num2 )
{
if(num2 == 0 || num1 == 0)
return 0;
if (num2 == 1)
return num1;
if (num2 == -1)
return -num1;
if (num2 > 0)
return product(num1+num1,num2-1);
if (num2 < 0)
return product(num1+num1,num2+1);
}
This code will obviously have integer overflow problem..
Nitin your logic is correct but with 1 problem it won't give the correct result. Try it your self. I have the same logic but with little difference:
int Product( int a, int c){
static int b = 0;
if(c ==0) {
int z =b;
b=0;
return z;
}else if(c<0){
b-=a;
return Product(a,++c);
}else{
b+=a;
return Product(a,--c);
}
}
Patron, can you please give me that test case where my code fails... plus try running ur code on product(-5,-3).. i guess it will fail....
Dude, I checked my code and its giving me correct result.
the problem in your code is the statement
Product(num1+num1, num2-1) or Product(num1+num1, num2+1)
For example, If you want Product(-5,-3)
your code will say "return Product(-10,-2) " which will again say "return Product(-20,-1)...are you getting what is the problem??
Ohh.... Missed out on this..... thanks for pointing this out.. :) Your strategy is the best way i could think of to solve this problem unless i am willing to add another argument original num1 in the function.
And let me return you the favor. If you test the similar thing product(-5,-3) on ur code. In the first iteration b=-5 and it return product(-5,-2). In the second iteration b=0 and it returns product(-5,-1). The culprit is b-=a. Check it and let me know if i am not right..
public static void main(String[] args) {
int a = -2;
int b = -4;
if(a < 0 && b < 0) {
a = Math.abs(a);
b = Math.abs(b);
} else if (b < 0) {
int temp = a;
a = b;
b = temp;
}
int result = prod(a, b);
System.out.println(result);
}
public static int prod(int a, int b) {
if (b == 0) {
return 0;
}
if (b == 1) {
return a;
}
return a + prod(a, b - 1);
}
public static int product(int num1, int num2) {
int sign = 1;
if(num1 == 0 || num2 == 0)
return 0;
if(num1 < 0 && num2 > 0 || num1 > 0 && num2 < 0)
sign = -1;
return sign * productRec(Math.abs(num1), Math.abs(num2));
}
private static int productRec(int num1, int num2) {
if(num2 == 1)
return num1;
return num1 + productRec(num1, num2 - 1);
}
public static int product(int num1, int num2) {
int sign = 1;
if(num1 == 0 || num2 == 0)
return 0;
if(num1 < 0 && num2 > 0 || num1 > 0 && num2 < 0)
sign = -1;
return sign * productRec(Math.abs(num1), Math.abs(num2));
}
private static int productRec(int num1, int num2) {
if(num2 == 1)
return num1;
return num1 + productRec(num1, num2 - 1);
}
/* Multiply 2 numbers without using loop or mul operator*/
2 #include <stdio.h>
3 int rec_mul(int p,int q)
4 {
5 int a;
6 if(q <=0)
7 return 0;
8 p += rec_mul(p,q-1);
9 return p;
10 }
11 main()
12 {
13 int p,q,r;
14 p= 4;
15 q= 4;
16 int positive;
17 positive = 1;
18 /*check sign*/
19 if (p < 0 && q > 0)
20 positive = 0;
21 r = rec_mul(p,q);
22 if (!positive)
23 r = -r;
24 printf("%d\n",r);
25 }
/* Multiply 2 numbers without using loop or mul operator*/
2 #include <stdio.h>
3 int rec_mul(int p,int q)
4 {
5 int a;
6 if(q <=0)
7 return 0;
8 p += rec_mul(p,q-1);
9 return p;
10 }
11 main()
12 {
13 int p,q,r;
14 p= 4;
15 q= 4;
16 int positive;
17 positive = 1;
18 /*check sign*/
19 if (p < 0 && q > 0)
20 positive = 0;
21 r = rec_mul(p,q);
22 if (!positive)
23 r = -r;
24 printf("%d\n",r);
25 }
use log.
- Mridul Malpani October 03, 2010let m=x*y;
log(m)=log(x)+log(y);
m=pow(10,m);
correct me, if i m wrng.