# Quantitative developer Interview Questions

# 375

Quantitative Developer interview questions shared by candidates### How to measure 9 minutes using only a 4 minute and 7 minute hourglass

15 Answers↳

Start both timers together. When the 4 minute timer is done, flip it. 7 minute timer will have 3 minutes left. When the 7 minute timer is done, the 4 minute timer will have 1 minute left. Now you can count to 9 minutes by simply leaving the 4 minute to expire (1 min), flip it and let it expire (4 min), flip it again and let it expire (4 min). 1 + 4 + 4 = 9 Less

↳

The key is understanding that you will have to use the two hourglasses together. Since this problem could be asked in many ways using different values for the hourglasses and the total amount of time, it's more important to understand how you use the tools rather than memorize a specific example. The question is used to determine those who can apply their knowledge to solve problems vs. those who memorize answers "from the book". Start both timers. After four minutes, the four-minute timer will have expired and the seven-minute timer will have three minutes remaining. Flip the four minute timer over. After seven minutes, the seven-minute timer will have expired and the four-minute timer will still have one minute left. Flip the seven-minute timer over. After eight minutes, the four-minute timer will have expired for the second time. The seven-minute timer will have accumulated one minute after it's last flip. Flip over the seven-minute timer and when it expires nine minutes will have elapsed. For extra measure, you can always throw in something like, "assuming the timers can be flipped over nearly instantly..." Less

↳

1st timer 2nd timer time count 4 7...................start both timers 3 6................. 1min 2 5..................2mins 1 4..................3mins 0(flip) 3..................4mins completed 4 3..................4mins(assuming flip takes no time ideally) 3 2..................5mins 2 1..................6mins 1 0(flip)..........7mins 1 7..................7mins(again ideal flip) 0 6..................8mins(flip 2nd timer to count 1min) 0(as it is) 7..................9mins... Less

### The first question he gave me was not hard. 1. You call to someone's house and asked if they have two children. The answer happens to be yes. Then you ask if one of their children is a boy. The answer happens to be yes again. What's the probability that the second child is a boy? 2. (Much harder) You call to someone's house and asked if they have two children. The answer happens to be yes. Then you ask if one of their children's name a William. The answer happens to be yes again.(We assume William is a boy's name, and that it's possible that both children are Williams) What's the probability that the second child is a boy?

12 Answers↳

1. BB, BG, GB, GG 1/4 each, which later reduced to only BB, BG, GB with 1/3 probability each. So the probability of BB is 1/3 2. Let w is the probability of the name William. Probability to have at least one William in the family for BB is 2w-w^2, For BG - w, GB - w, GG - 0. So the probability of BB with at least one William is (2w-w^2)/(2w+2w-w^2) ~ 1/2 Less

↳

The answer by Anonymous poster on Sep 28, 2014 gets closest to the answer. However, I think the calculation P[Y] = 1 - P[C1's name != William AND C2's name != William] should result in 1 - (1- e /2) ( 1- e / 2) = e - (e ^ 2 ) / 4, as opposed to poster's answer 1 - (e^2) / 4, which I think overstates the probability of Y. For e.g. let's assume that e (Probability [X is William | X is boy]) is 0.5, meaning half of all boys are named William. e - (e ^ 2) / 4 results in probability of P(Y) = 7/16; Y = C1 is William or C2 is William 1 - (e ^ 2) / 4 results in probability of P(Y) = 15/16, which is way too high; because there is more than one case possible in which we both C1 and C2 are not Williams, for e.g. if both are girls or both are boys but not named William etc) So in that case the final answer becomes: (3e/2 - (e^2)/2) * 0.5 / (e - (e ^ 2) / 4) = 3e - e^2 / 4e - e^2 = (3 - e) / (4 - e) One reason why I thought this might be incorrect was that setting e = 0, does not result in P(C2 = Boy | Y) as 0 like Anyoymous's poster does. However I think e = 0 is violates the question's assumptions. If e = 0, it means no boy is named William but question also says that William is a Boy's name. So that means there can be no person in the world named William, but then how did question come up with a person named William! Less

↳

I think second child refers the other child (the one not on the phone) In this case answer to first is 1/3 and second is (1-p)/(2-p) where p is total probability of the name William. For sanity check if all boys are named William the answers coincide. Less

### How many numbers between 1 and 1000 contain a 3?

11 Answers↳

I would go with elimination of one character from a base 10 numbering system gives you a base 9 numbering system. 9^3 = 729 permutations of a base 9 numbering system (a system with no number 3) with 3 digits, since 10^3 = 1000; 10^3 - 9^3 = 271 thus 271 numbers have a 3 in them. Less

↳

Thanks Candidate, or more simply For 300 to 399 = 100 numbers For other x00 to x99 = 19 numbers each = 19 x 9 = 171 numbers Total of 271 numbers Less

↳

1000 does not contain a 3. So, count the number of 3 digit numbers without a three. There are 9 choices for the first entry, 9 for the second, and 9 for the third. So, there are 729 numbers without a 3, and 1000-729 = 271 with a 3. Less

### You have a chest of 8 drawers. With probability 1/2, you put a letter in one of the drawers. With probability 1/2, you don't put a letter in any drawer. I open the first 7 drawers, all are empty. What is the probability there is a letter in the 8th drawer?

7 Answers↳

1/9

↳

Why is it not 1/2? since there is only one drawer left, there would be a 50/50 change that it is there or isn't. I don't understand how you get 1/9 Less

↳

Conditional probability gives 1/9, but suppose we conduct the experiment one million times, and each time the opened 7 drawers are empty, what is the expected number of times that one finds the ball in the 8th drawer? Less

### If you toss a coin n times, what's the expected value of n if you get the 2nd head?

6 Answers↳

It is 4.

↳

Assume the expected number of times you have to toss to get 2 heads is E_n If the first time you flip, you get one tail, then the exp time you get two heads is: 1/2 * (1+ E_n) If the first time you flip, you get one head, if you get another head the 2nd time, you're done, if you get a tail for the 2nd flip, you start all over again: 1/2 * 1/2 * 2 + 1/2 * 1/2 * (E_n+2) By conditional probability: E_n = 1/2 * (1+ E_n) + 1/4 *(2 + E_n + 2) -> E_n = 6 So you have to flip coin 6 times on average Less

↳

Assuming his initial q meant below: what is the expected number of flips needed to get two heads in a row. ans) 6 How? By recursive problem construction. Let E = expected #of flips to get HH 1) if we get T => we need to start all over again. need to flip +1 to get H. 2) if we get HT => we need to start all over again. need to flip at least 2 more flips to achieve HH 3) if we get HH => we are done. we used 2 flips to get HH idea: (for having T, HT, HH) E = 0.5 * (E + 1) + (0.5 * 0.5) * (E + 2) + (0.5 * 0.5) * 2 if we solve above recursion, the answer is 6. Less

### You are given an array, and a number x. Determines whether or not there exist two elements in in the input array whose sum is exactly x, efficiently.

4 Answers↳

If the array is initially unsorted: -O(NlogN) runtime, no additional memory: Sort the array. For each element i, perform a binary search on the array for X-i. -O(N) runtime, O(N) additional memory: Stick all of the elements in a HashSet. For each element i, check whether X-i is present in the set. If the array is initially sorted: Create two pointers, one originally to the first entry in the array and the other to the last. At each step, check how the sum of the two entries currently being considered compares to X. If it's too small, increment the first pointer. If it's too big, decrement the second. If it's equal, we are done. This process will locate a pair summing to X, if such a pair exists. Otherwise, we will know that no such pair exists as soon as the pointers coincide. Less

↳

Oops, little error there: change either the check or the insert operation to work on X instead of X-CUR. Less

↳

Create a Set called S For each element CUR in the array check to see if S.contains(X-CUR) if yes then return true else insert (X-CUR) into S. After the loop quits return FALSE Less

### Given a m*n matrix with values -1 or 1, try to flip the values in a given row and a given line efficiently.

3 Answers↳

R: given row, C: given column for i = 0 to m: Matrix(i, C) *= -1 for j = 0 to n: Matrix(R, j) *= -1 Matrix(R, C) *= -1 Less

↳

you can get even more efficiency if you go down to the bit level, since multiplication is expensive. python code follows: let's call the matrix M, and the given row and column r,c respectively for i in range(0,m): M[i][c] = ~M[i][c] + 1 for j in range(0,n): M[i][r] = ~M[i][r] + 1 Less

↳

for(i=0;i

### What is the probability of an integer from 1 to 60,000 not having the digit 6?

3 Answers↳

My guess, number of integers not having the digit 6=6*9*9*9*9-1=39365 So the probability is about 0.6561. Less

↳

The number of integers in [1,60000] that contains digit 6 is 6*(10^4-9^4)+1. The answer is verified by writing a testing program. Less

↳

If we are to find the answer from 0-59999, the probability of not having a 6 will be (9/10)^4. We rescale the probability to make the denominator 60000 and get (9^4*6)/(10^4*6). Now we shift our range to 1-60000 and we have one case, 60000, to delete from our previous result. We delete that case and get (9^4*6-1)/(10^4*6). Less

### Given a char buffer[4096], write a malloc implementation.

3 Answers↳

char * a = (char*) malloc ((4096 + 1)*sizeof(char))

↳

Write the "implementation" of malloc, not malloc'ing a char[]... In general you need to record the size in this "heap", then return a pointer pointing to the block right after the size. Less

↳

char* a = (char*) malloc(4096);

### Rotate a matrix counter-clockwise

2 Answers↳

Rotation is just a transform "Linear Algebra" with special cases at 90, 180,270

↳

Simplest way assuming 90degrees is to transpose the matrix then reverse rows