Meta interview question

Multiply two big ints.

Interview Answers

Anonymous

Apr 30, 2015

The above code is neat; unfortunately, it does not account for *big* integers being multiplied. So, suppose we have two numbers that are close to the integer limit. What would happen when we try to multiple them? In addition, what happens when one of the integers is negative?

3

Anonymous

Apr 24, 2015

public static int bitwiseMultiply(int a, int b) { if (a == 0 || b == 0) { return 0; } if (a == 1) { return b; } else if (b == 1) { return a; } int result = 0; while (b >= 1) { if ((b & 1) == 1) { result = result + a; } a >= 1; } return result; }

Anonymous

Apr 30, 2015

Java Provides multiply() method for BigInteger datatype. It can be used directly.

1