## Interview Question

Country: United States

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

``````public static double[] findNumbers(int sum, int product){
/**
* Given the sum of two numbers & their product, return the two numbers.
* a * b = 20
* a + b = 10
* b = 10 - a
*
* 10a - a^2 = 20
* a^2 -10a + 20, now solve with quadratic formula
*/
double[] result = new double[2];
double tmp = Math.sqrt(Math.pow(sum , 2) - 4*(product));
result[0] = (sum + tmp)/2;
result[1] = (sum - tmp)/2;
return result;
}``````

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

Hello,

let the 2 nos be a and b so the given is

a*b = (say)8 -- equation1
a+b=6. -- equation 2

You can keep 8 and 6 and x and y too.

so a = 6-b => substitute this in equation 1

=> (6-b)*b = 8
=>6b-b^2 = 8
=> b^2 - 6b + 8 = 0

Now this becomes a quadratic equation and will always be in such problems. solve get the value of b and by substituting that into either of equations above of ur choice will get the value of a.

So in this case
(b-4)(b-2)=0 so the o/p is 4 and 2

Edit:::::
In short, in such problems try to reduce the number of variables to one and try to represent others in term of only one variable. Then that variable will help you to find out rest of the values.

Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi
This is the code:

``````package CareerCup;

/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {

public static void main(String[] args) {
int sum=6,product=8;

int a,b;

a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}``````

Thanks.

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

I think all you need to do is get the second number and find it's factors and also add them to check if there sum is the first number.

For eg:8,12

1*12=12
1+12!=8

Continue...

2*6=12
2+6=8
Break...

The solution can be modified to include negative integers as well...

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

``````package CareerCup;

/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {

public static void main(String[] args) {
int sum=6,product=8;

int a,b;

a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}``````

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

``````package CareerCup;

/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {

public static void main(String[] args) {
int sum=6,product=8;

int a,b;

a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}``````

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

Simplify the equations:
a+b=x --> a=x-b
a*b=y --> a=y/b

Substituting (x-b) for 'a' in the equation and solving the quadratic equation yields:
(x-b)*b=y --> b^2 -x*b + y =0 --> b=(x+-(x^2-4*y)^(1/2))/2

a, b == (x-(x^2-4*y)^(1/2))/2, (x+(x^2-4*y)^(1/2))/2

Validating with the numbers from the example
a=(6-(36 - 32)^(1/2))/2 --> a=(6-2)2 -->a=2
b=(6+(36 - 32)^(1/2))/2 --> b=(6+2)2 -->b=4

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

Solve for 'a' in the equations:
a+b=x --> a=x-b
a*b=y --> a=y/b

Substituting (x-b) for 'a' in the equation and solving the quadrtic equation yeilds:
(x-b)*b=y --> b^2 -xb + y =0 --> b=(x+-(x^2-4*y)^(1/2))/2
a, b == (x-(x^2-4*y)^(1/2))/2, (x+(x^2-4y)^(1/2))/2

Validating with the numbers from the example
a=(6-(36 - 32)^(1/2))/2 --> a=(6-2)2 --> a=2
b=(6+(36 - 32)^(1/2))/2 --> b=(6+-2)2 --> b=4

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

Hi
Here's the code

``````package CareerCup;

/**
* Created by Deven on 17/08/15.
*/
public class SumProduct {

public static void main(String[] args) {
int sum=1037,product=29232;

int a,b;

a=(sum +(int)Math.sqrt((sum*sum)-(4*product)))/2;
b=(sum -(int)Math.sqrt((sum*sum)-(4*product)))/2;
System.out.println("Answers are:" + a + " "+ b);
}
}``````

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

Compute the quadratic result for values:

``````public static void printComponents(int sum, int product){
int sqrRt = (int)Math.sqrt((sum * sum) - (4 * product));
int posA = (-sum + sqrRt) / 2;
int posB = product / posA;
if(posA + posB != sum ){
posA = (-sum - sqrRt) / 2;
posB = product / posA;
}
java.lang.System.out.println(""+posA+", "+posB);
}``````

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