You are on page 1of 4

Math and Logic Puzzles

Sometimes known as brainteasers, these types of problems occasionally occur in programming interviews and generally test your logical thinking and mathematical analysis abilities. Microsoft and Google are especially renowned for using brainteasers as part of their interview process, and many other companies have since followed suit. However, some organizations believe that since these brainteasers do not measure programming ability, they have no relevance to the job and should not be part of the interview. Regardless, since they occur in industry, well go over them to ensure your preparedness in the interview process. Many problems have different stories but the same principle solution, so general experience with brainteasers should prove helpful for the occasions when they do occur. Unlike most programming problems which are usually straightforward, brainteasers almost always have an obvious (and incorrect) answer in addition to the correct response. As a result, the immediate naive solution you come up with is most likely wrong. The reason is often a bad assumption, but making assumptions is essential to solving virtually every problem. Therefore, try to identify the solutions you are making and verify their correctness with some thorough analysis. Make a list of assumptions you come up with and go through the problem modifying those assumptions to think outside the box.

Practice Question: Estimate the number of gas stations in the United States.
Well, the number of gas stations around should probably be a function of the number of cars out there, so lets start off by estimating that. The number of cars in the US is also probably a function of the number of people in the country, and its reasonably common knowledge that the population of the United States is around 300 million. Not every person owns a car though a car per every two people seems more reasonable, so well guess that the number of cars in the country is 150 million. How often does a driver have to get gas? On average, it takes around a week, assuming a 3 mile average commute per day and a 22 gallon tank. A week is 24 * 7 hours, but not all stations are open 24/7. Well guess that a station is open for a mean length of 100 hours per week. It takes about 6 minutes for a customer to fill er up, so a single pump can handle 10 people per hour. There are busy stations in metropolitan areas with lots of pumps as well as tiny stations in the middle of nowhere that dont really server customers, so well say they balance each other out and each station on average serves 10 people an hour. Therefore, a station handles around 1000 cars a week. Based on these estimates, we get 150,000,000 cars / 1000 = 150,000 gas stations in the United States. As a comparison with the real numbers, the Journal of Petroleum Marketing states there were 187,097 retail sites selling motor fuel in the United States in June 2008. The goal of these questions are to see if you can make good assumptions and perform some quick back of the envelope calculations to see if the numbers make sense. Its a very useful skill for a developer to have when trying to estimate bandwidth costs, running time of programs,

etc. Similar questions include estimating the number of piano tuners in the country, the amount of water in the Mississippi flowing past New Orleans per hour, etc.

Practice Question: How would you locate a specific book in a big library? There's no cataloging system and no librarian to help you.
If theres no cataloging system, the easiest method that guarantees the correct answer is exhaustive search. Unfortunately, that takes a lot of time - O(n) to be exact. You can suggest slightly faster algorithms that may not work some of the time, such as trying to determine a pattern in the way books are shelved. You could look at the first 5 books of every 10th row to see if there is some system in place for organization, and if so, try to figure out the pattern. If not, try increasing or decreasing the granularity of the search to see if that makes a difference. Parallelization if possible would also be extremely helpful in reducing running time; just assign your friends equal parts of the library to search. The goal of this question is to identify whether you are able to make good decisions given an open ended, basically intractable problem.

Practice Question: You have 26 constants, labeled A through Z. Let A equal 1. The other constants have values equal to the letter's position in the alphabet, raised to the power of the previous constant. That means that B (the second letter) = 2^A = 2^1 = 2. C = 3^B = 3^2 = 9, and so on. Find the exact numerical value for this expression: (X-A) * (X-B) * (X-C) * ... * (X-Y) * (X-Z)
At first glance, this problem seems intractable. If C = 9, then D = 4^9 = 262144 and E = 5 ^ 262144 which is no longer a reasonable calculation. So, there must be an easier way. This looks slightly polynomial, but that would not be a good tactic the problem also states that these letters are constants, so expansion is not a constructive idea. The key here is to realize the identity rule of multiplication: anything multiplied by 0 is 0. One of the terms in the expression is (X-X). Regardless of how big X is, X-X = 0, so the entire expression must evaluate to 0.

Practice Question: Mike and Todd have $21 between them. Mike has $20 more than Todd. How much does each have? You can't use fractions in the answer.
From the problem, we can set up a system of equations: M + T = $21 M = 20 + T By substitution, (20 + T) + T = 21 Solving, we get 20 + 2T = 21

2T = 21 20 = 1 2T = 1 T= At first glance, not being able to use fractions seems like this problem is impossible. However, since money is usually represented in dollars and cents, the technicality lies in that there are not fractions of dollars; instead, there is 50 cents. This question is designed to make sure you stick with your guns when you get a correct answer that is somehow against the rules. I dont agree with this principle, but it doesnt hurt to be prepared.

Practice Question: You have a 3-quart bucket, a 5-quart bucket, and an infinite "supply of water. How can you measure out exactly 4 quarts?
Theres only so much you can do with a 3 quart and a 5 quart container. Lets list them: Obtain 3 quarts. Obtain 5 quarts. Obtain 2 quarts by pouring the 5 quart into the 3 quart and leaving the remaining 2 quarts in the 5 quart container. Obtain 4 quarts by then the 2 quarts from the 5 quart container to the 3 quart container, refilling the 5 quart container, and pouring 1 quart from the 5 quart into the 3 quart container, filling it up completely. Whoa weve got our desired 4 quarts!

Practice Question: Which way should the key turn in a car door to unlock it?
This question seems dumb theres no right answer, and thats the point. Tradition holds that since it is easier for right handed people to turn a key to the right, and right handed people make up the majority of the population, turning a key right unlocks the door. As a result, this is usually the expected answer, but if you have a reasonable conviction otherwise, it shouldnt matter. The purpose of the question is to make sure youre able to make a quick decision on something that really isnt terribly important. The company is looking to weed out people who waste a lot of time on little details that dont matter in the end.

Practice Question: How many times a day do a clock's hands overlap?


Since there are 24 hours in a day, the answer cant be more than 24, and should be somewhere near that number. We need to figure out exactly what that number is. Because both the minute and the hour hands move at a constant rate, the time interval between each overlap must also be constant. Knowing that, well start from the simplest example

midnight. The hands overlap at 12:00am, and the next time they overlap is near 1:05am, but this isnt exact enough. We now know that there cant be more than one overlap each hour, since it takes around 1:05 to get the hands aligned again. Therefore, there are 11 overlaps in a 12 hour period, which comes to 12/11 = 65.45 minutes. If there are 11 overlaps in 12 hours, there must be 22 overlaps in 24 hours, so 22 is the answer.

You might also like