Microsoft Interview Questions and Answers 2
In my last post I tried to answer some questions from a list of Microsoft Interview Questions. As before, this is really just a fun exercise. No googling is allowed.
The Questions
One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?
Interestingly enough we’re not given the distance between LA and New York – maybe we’re supposed to just know it? Anyways, the key is to figure out how long the trains will take to collide and then calculate how far the bird flies in that time. The 2 trains are travelling at 15mph and 20mph respectively so they will cover the distance between the 2 cities at 35mph. So if the distance between the 2 cities is x, the 2 trains will cover the distance in x/35 hours. The bird will cover (x/35) * 25 = (5/7)x miles in this time.
You have two jars, 50 red marbles and 50 blue marbles. A jar will be picked at random, and then a marble will be picked from the jar. Placing all of the marbles in the jars, how can you maximize the chances of a red marble being picked? What are the exact odds of getting a red marble using your scheme?
Very cool question. Most people will try to use statistics to solve it (and fail), but the answer is actually very simple. Put one red marble in the one jar and put all the other marbles (49 red and 50 blue) in the other jar. So calculating the odds of a red marble being picked would be..
0.5 * p(picking red from first jar) + 0.5 * p(picking red from second jar) = 0.5 + 0.5 (49/99)
So it’s almost 75%. Awesome question. Next time you get asked in an interview ‘tell us about a time you had a difficult problem to solve’ you can say ‘well, this one time I had 2 jars of marbles…’
Imagine you are standing in front of a mirror, facing it. Raise your left hand. Raise your right hand. Look at your reflection. When you raise your left hand your reflection raises what appears to be his right hand. But when you tilt your head up, your reflection does too, and does not appear to tilt his/her head down. Why is it that the mirror appears to reverse left and right, but not up and down?
This one messes with your head a little. The trick is that left and right are relative to your body, where up and down are relative to the earth. To understand what I’m saying look at what’s on your left and then turn around. Now what’s on your left is actually the complete opposite. But if you do the same for what’s above you it doesn’t change. So the answer here is – it’s due to the definition of left and right and up and down.
You have 5 jars of pills. Each pill weighs 10 gram, except for contaminated pills contained in one jar, where each pill weighs 9 gm. Given a scale, how could you tell which jar had the contaminated pills in just one measurement?
Another very cool question, with strong links to programming. To make the explanation a little easier let’s name the 5 jars from A to E. Now we need to measure 5 pills from A, 4 from B, 3 from C, 2 from D and one from E. Now the weights will tell us exactly which jar contains the contaminated pills. For example, if the weight is 145 grams that means there were 5 contaminated pills so the contaminated jar is A. 146 Grams means it’s B, 147 means it’s C, 148 means it’s D and 149 means it’s E.
If you had an infinite supply of water and a 5 quart and 3 quart pail, how would you measure exactly 4 quarts?
I think this is actually the same puzzle presented to Bruce Willis in the movie Die Hard. Fill the 3 quart pail and pour it into the 5 quart pail. Fill the 3 quart pail and use it to fill the 5 quart pail (which contains 3 quarts at this stage). There will be 1 quart left in the 3 quart pail. Empty the 5 quart pail and pour the 1 quart left in the 3 quart pail into the 5 quart pail. Fill the 3 quart pail and pour it into the 5 quart pail (which contains 1 quart at this stage). The 5 quart pail will now contain 4 quarts.
You have a bucket of jelly beans. Some are red, some are blue, and some green. With your eyes closed, pick out 2 of a like color. How many do you have to grab to be sure you have 2 of the same?
I couldn’t get this one at all and then I explained it to a friend who managed to figure it out. As with any good riddle the answer is ridiculously easy when you have it. You have to pick 4 jelly beans. If you pick 3 you could possibly pick 3 different colors, but once you have 4 you must have at least 2 of the same color. The wording makes it tricky because it seems to imply that you must be holding exactly 2 beans of the same color in your hand, but actually you need to have at least 2 of the same color.
Bonus Question
A colleague sent me this one (which he was once asked in an interview) – it’s probably my favourite.
*A pirate ship captures a treasure of 1000 golden coins. The treasure has to be split among the 5 pirates: 1, 2, 3, 4, and 5 in order of rank. The pirates have the following important characteristics:
Infinitely smart. Bloodthirsty. Greedy.
Starting with pirate 5 they can make a proposal how to split up the treasure. This proposal can either be accepted or the pirate is thrown overboard. A proposal is accepted if and only if a majority of the pirates agrees on it.
The Question: What proposal should pirate 5 make?
As with many puzzles you can’t really solve it unless you work backwards. Let’s imagine the first 3 proposals have been rejected – pirates 5, 4 and 3 have been thrown overboard and only 1 and 2 are left. Pirate 2 now has to make a proposal. No matter what proposal he makes, pirate 1 will reject it, pirate 2 will be thrown overboard and pirate 1 will get all the money. Even if pirate 2 proposes that pirate 1 gets all the money he will still reject it because he’s bloodthirsty – he would prefer killing 2 and getting all the money to simply getting all the money.
Right, now let’s back up one step. Pirates 1, 2 and 3 are left and pirate 3 has to make a proposal. He knows that if he gets thrown overboard pirate 2 is screwed, so he should be really easy to convince. He will propose pirate 3 gets 999 coins, pirate 2 gets 1 coin and pirate 1 gets nothing. Pirate 2 will accept this since it’s better than him rejecting it and getting nothing – pirate 3 has his majority vote.
Let’s back up another step. Pirates 1, 2, 3 and 4 are left and pirate 4 has to make a proposal. He knows that if he gets thrown overboard pirate 1 gets nothing, pirate 2 gets 1 coin and pirate 3 gets 999 coins. He needs 3 votes for a majority so he will propose pirate 4 gets 997 coins, pirate 3 gets nothing, pirate 2 gets 2 coins and pirate 1 gets 1 coin. (If he proposes that pirate 2 gets only 1 coin pirate 2 will reject it since he’s bloodthirsty and he can get the same amount of coins in the next round)
Let’s back up another step – we’re back to the initial scenario. Knowing what will happen in the next round, pirate 5 will propose: pirate 5 gets 997 coins, pirate 4 gets nothing, pirate 3 gets 1 coin, pirate 2 gets nothing and pirate 1 gets 2 coins.
Awesome question. Happy coding.