Interview Question

Interview(Student Candidate) New York, NY

• 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?
math, probability

Interview Answer

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.

Arnie on May 17, 2012

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.

David on Aug 26, 2012

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.

David on Aug 26, 2012

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.

Drew on Oct 18, 2012

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.

Anonymous on Jan 11, 2013

1027 = 1000 + 27 = 10^3 + 3^3 and you know you can factor a^3 + b^3

Anonymous on Sep 19, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.