Cadence Inc Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

use log.

let m=x*y;
log(m)=log(x)+log(y);
m=pow(10,m);

correct me, if i m wrng.

- Mridul Malpani October 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int 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..

- Nitin October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

see the correct code in my next post :)

- Nitin October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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....

- Nitin October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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??

- Patron October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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..

- bajanitin October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, Its not I have declared b as static int. So in second iteration " b = -10 and return Product (-5,-1)

- Patron October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the divide operator x/(1/y)

- cupnoodles October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice hehe

- Ajay October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cupnoodles...theoretically you are correct but sorry to say not programmatically ..when you say 1/y it will give you quotient...

- Anonymous October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int product(int n1,int n2){
    if(n2==0)return 0;
    else if(n2>0)return n1+product(n1,n2-1);
    else return -n1-product(n1,-n2-1);
}

- itachi October 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int result=n2;
int m=n1;
while (m=m/2 >= 1)
result << 1;
if (m%2==1)
result += n2;

- Anonymous October 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int multiplier(int num1, int num2)
{
if (num2 < 0)
{
num1 *= -1;
num2 = Math.Abs(num2);
}

if (num2== 1)
return num1;
return num1 + multiplier(num1, --num2);
}

- Biruk Solomon November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int recursiveMultiply(int M, int N) {
int result = 0;

if (N == 1) {
result = M;
} else {
result = M + recursiveMultiply(M, N - 1);
}

return result;
}

- Pedro November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Patron your reply is the best

- Anonymous February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<math.h>

void main()
{
float a=1,b=-2,c;
if(a==0 || b==0)
{ printf("0");
}
else
{
printf("check");
c=a/(1/b);
printf("check1");

}

if(((a<0)&&(b>0))||((a>0)&&(b<0)))
printf("%f",c);
else
printf("%f",c);

return ;
}

- Sunny December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	}

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

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);
	}

- Anonymous April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	}

- Dado April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* 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 }

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

/* 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 }

- Vishwas S July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int recMultiplication(int p,int q)
{
int signValue = true;

if(p ==0 || q == 0)
return 0;
else if(p == 1)
return q;
else if(q == 1)
return p;

/*check sign*/
if ((p < 0 && q > 0) || (p > 0 && q < 0))
signValue = false;

p += rec_mul(p,q-1);

if(!signValue)
{
p=-p;
}
return p;
}

- Anonymous June 26, 2018 | 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