Math Interview Questions | Glassdoor

# Math Interview Questions

62

interview questions shared by candidates

## Math Interview Questions

Sort: Relevance Popular Date

### Manager at Amazon was asked...

Mar 29, 2010

May 2, 2012

Feb 7, 2011
 Implement a power function to raise a double to an int power, including negative powers. 11 Answers Could be implemented many ways. I got the feeling that the interviewer wanted to see you approach the problem in multiple ways and demonstrate confidence in your math and recursive skills. #include #include #define MAX_ARRAY_LENGTH 256 double power(double, unsigned int); int main(int argc, char** argv) { double a = atof(argv[1]); int b = atoi(argv[2]); double result = power(a, b >> 31 == 0 ? b : -b); if ((unsigned int) b >> 31 == 1) { result = 1 / result; } printf("%f\n", result); return 0; } double power(double a, unsigned int b) { switch (b) { case 0: return 1.0; case 1: return a; default: return (b & 1) == 0 ? power(a * a, b >> 1) : power(a * a, b >> 1) * a; } } c implementation of the above (no recursion): int ipow(int base, int exp){ int result = 1; while(exp){ if(exp & 1) { result *= exp; } exp >>= 1; base *= base; } return result; } Show More Responses int power(double n, int exp) { bool npower = (exp < 0) ? true : false; double result = 1; exp = abs(exp); // get the absolute value for (int i = 0; i < exp; i++) { if (npower) { result = result/n; } else { result = result*n; } } return result; } C# code verified: static double Power(double d, int exp) { if (d == 0 || exp == 0) { if (exp >= 0) { return 1; } else { return double.PositiveInfinity; } } int expAbs = Math.Abs(exp); double res = d; for (int i = 1; i 0) ? (res) : (1 / res); } double power(double x, int y) { if(y == 0) return 1; int sign = 1; if(y < 0) sign = -1; y = abs(y); double d = power(x, y/2); if(y%2 == 0) d = d*d; else d = x*d*d; if(sign == -1) return 1.0/d; else return d; } I am surprised that not a single person here had noticed that the guy asked to raise a DOUBLE to a given power. Men, double are not integers. Their exponent is stored in a part of their binary representation. If you multiply n times a double you will make n times a rounding error and n useless calculations. Just changed the binary part of the double that is related to its exponent, and here it is, your double has been raised to a given power, a you absolutely lost no precision, and you've made 0 calculations. This is basic stuff, every university teaches that to its students... floating numbers representation... I believe interviewer is expecting for this public static double Power(double x, int y) { double result = 1; bool isNegative = y 0) { if ((y & 1) > 0) { result *= x; } y = (y >> 1); x *= x; } if (isNegative) result = 1 / result; return result; } Verified C# static double Pow(double b, double exp) { if (exp == 0) return 1; else if (exp > 0) return b * Pow(b, exp - 1); else return 1 / Pow(b, -exp); } Does it get more compact? TD's answer is interesting, but not very useful. If you actually try it you'll find that since the double's base is 2, any changes to the exponent portion approximately multiply (or divide) numbers by a power of two. I say approximately here, since TD forgot to mention that the number itself isn't stored in float point numbers, only the digits after the implied 1. So yes, it's important to know how floating point numbers work, but modifying the exponent portion of a floating point number is a fundamentally incorrect solution. public double power(double num, int exp) { if(exp == 0) return 1; double res = 1; for(int e=Math.abs(exp);e>0;num*=num,e>>=1) { if( (e&1) == 1) res *= num; } return (exp>0)?res:1.0/res; }

### Summer Intern at Five Rings Capital was asked...

Apr 25, 2012
 • Is 1027 a prime number? • How would you write an algorithm that identifies prime numbers? • 2 blue and 2 red balls, in a box, no replacing. Guess the color of the ball, you receive a dollar if you are correct. What is the dollar amount you would pay to play this game? 6 Answers An algorithm for testing prime numbers is trial testing, test whether whether the number is dividable by an integer from 2 to its square root. For the color guessing game, the expected number of dollars you get is the average identity between a permutation of rrbb and rrbb, which is 2. For the prime number testing, only the number 2 and then odd numbers need to be tested. If it is not divisible by 2, there is no need to test against any other even number. So start with 2, then 3, then increment by 2 after that (3,7,9,...) until you are greater than the square root (then it's prime), or you find a divisible factor (it is not prime). To test for divisibility, we are looking for a remainder of zero - use a MOD function if available. Taking the integer portion of the quotient and subtracting from the actual quotient: if the difference is zero, then the remainder is zero and we have a divisible factor. If the difference is nonzero, then it is not divisible and continue testing. In this case, we find that dividing by 13 gives 79 with no remainder, so it is not prime. For the guessing game, the minimum winnings are \$2 every time with the proper strategy. I'm assuming the rules are you pay to play and you get to guess until there are no more marbles. Say you guess wrong the first attempt. (you guess blue and it was red). So now you know there are 2 blue, 1 red. Your logical choice is to choose blue again, since there are more of them. But say you guess wrong again. Now you know there are 2 blue left, so you will win on both of the last 2 draws. If you were correct on one or both of the first two trials, then you could wind up with an even chance on the third trial, so you would win that some of the time, then you'll always win on the last trial. Show More Responses David, I think we could pay more that \$2 and still come out on top. You logic seems sound, but looking at the probabilities I see: 1/2+1/3*(2)+2/3*(5/2) = 17/6 = ~2.83 Choosing the first ball, we obviously have an expected value of 1/2. Then, WLOG, we are left with RRB. Clearly we then choose R as this gives us a 2/3 shot at picking correctly. If it is R, then we get that \$1, have a 50% shot at the next, and are assured the last, giving us, on average, \$2.5. If it is B, then we know the next two will be R, giving us \$2. As you can see, with an optimal strategy, we should expect to make ~\$2.83 per round. Take the square root fo 1027. You get 32.04. Need only to check if divisible by prime numbers from 1 to 32, which include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31 For algorithm, see Lucas' test on Wikipedia, where there is also pseudocode. 1027 = 1000 + 27 = 10^3 + 3^3 and you know you can factor a^3 + b^3

### Summer Analyst at Goldman Sachs was asked...

May 8, 2011

Dec 28, 2009
 One thousand 1x1x1 cubes in a 10x10x10 cube. Outside spray painted. How many have no paint.\? 5 Answers 1000-8^3=488 Its asking how many have no paint, so its just 512 1000....because they are placed INSIDE the 10x10x10 cube, thus the only cube getting painted is the big one. Show More Responses 1000- the number of cubes on the outside = 1000 - 2 sides - 2 other sides (not counting the cubes that were already counted by the 2 previous sides) - 2 remaining sides (not counting the cubes that were counted by the 4 previous sides) = 1000 - 200 - 160 - 128 = 512 or just count the number of cubes on the inside = 8^3 = 512 http://brainteaserbible.com/interview-brainteaser-cube-dipped-in-paint-10x10x10

### Candidates Day Interviews for Multiple Areas (X-Div/Back Office) at Goldman Sachs was asked...

Mar 18, 2009
 If you have a three gallon jug and a five gallon jug No marks on either one The goal is to fill the five gallon jug with four gallons of water How is this accomplished? 5 Answers Take 5 gal. and pour into the 3 gal. The 5 now has 2 gal. Empty the 3 gal, and pour the remaining 2 gal into the 3 gal. Fill the 5 gallon and pour 1 gal. (which is the remainder of the space in the 3 gal.) This gives you 4 gal. that was from die hard 3. 1) fill up the 3 and pour it into the 5. 2) fill up the 3 and fill up the rest of the 5. that leaves 1 in the three. 3) dump out the 5. 4) pour the 1 that's left in the three into the 5. 5) fill up the 3 again and pour it into the 5. see - easy peasy. might be a bit rougher if solving it is necessary to stop a bomb from blowing up the city. mclain and samuel L got it done. 1) Fill up half of 5 gal (2.5 gal) from 3 gal jug 2) Fill up half of 3gals jug(1.5 gal) and pour into 5gal jug 2.5 + 1.5 = 4gal Show More Responses Die Hard 2 also has the solution to this question. there are actually 4 die hards, which is why you two are confused. 1. the classic big LA building he runs through glass 2. the airport based one 2*. the new york fed gold gets stolen (die hard with vengence) -- THIS is the one with the water puzzle. 3. the newest die hard where the stop lights get taken over etc, live free die hard i think

Aug 12, 2011