1. You have a birthday cake and have exactly 3 cuts to cut it into 8 equal pieces. How do you do it?
The answer: stack the pieces
The “correct” answer is to cut the cake in quarters (4 pieces) using 2 of the cuts, then stack all 4 of the pieces and then split all four of the stacked pieces with the third cut.
2. Microsoft interview Puzzle: Three Ants on Triangle
Three ants are sitting at the three corners of an equilateral triangle. Each ant starts randomly picks a direction and starts to move along the edge of the triangle. What is the probability that none of the ants collide?
This puzzle somewhat mathematical oriented. This interview puzzle is famously known as “Three Ants on Triangle” puzzle. People who know the concepts of probability can easily solve this puzzle.
Answer:
The
ants can only avoid a collision if they all decide to move in the same
direction (either clockwise or anti-clockwise). If the ants do not pick
the same direction, there will definitely be a collision. Each ant has
the option to either move clockwise or anti-clockwise. There is a one in
two chance that an ant decides to pick a particular direction. Using
simple probability calculations, we can determine the probability of no
collision.
P(No collision) = P(All ants go in a clockwise direction) + P( All ants go in an anti-clockwise direction) = 0.5 * 0.5 * 0.5 + 0.5 * 0.5 * 0.5 = 0.25
3. Given the numbers 1 to 1000, what is the minimum number [of] guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ for each guess you make?
Finding the maximum number of guesses
But, what if we wanted to find the maximum number of guesses?
Well, think about this one. What if the number that you have to guess is ‘1’ and you start guessing from 1,000? Then, if the person who knows the number keeps saying lower, then you would guess 999,998,997…6,5,4,3,2, and finally until you get to 1.
The maximum number of guesses
This means that the maximum number of guesses is 999.
But, you must be saying, that’s a stupid answer – because no one would take that approach to guessing unless they were really foolish.
Using Binary Search
The approach most programmers would take is dividing the set of numbers in half and then working accordingly – basically a binary search as it is known in computer science.
So, let’s say the number you were trying to guess is a ‘1’. Then, you would start from the middle of 1,000 – which is 500. The person giving you hints would keep saying lower – and you would end up with something like this sequence of numbers:
500, 250, 125, 63, 32, 16, 8, 4, 2, 1
Counting the number of guesses above would give you 10. Another way of arriving at this estimate would be to take the log base 2 of 1,000 – which would also give you approximately 10. Since you can’t possibly have a fraction of a guess, the result of log base 2 of 1000 should be rounded up to a whole number. Log base 2 of 1000 actually gives 9.965, but we round that up to 10 to get an integer.
4. You are in a room with three switches which control three light bulbs in a room way on the other side of the building. You can go into the room with the light bulbs only once. How do you figure out which light switch goes with which light bulb?
Answer:
turn on switch 1 and 2 and wait then turn of 1 then go in the other room the light on is switch 2 the cold light is 3 and the warm is 1
5. You are standing in a school hallway lined with 100 closed lockers. You then open all 100 lockers. After this, you then close every 2nd locker (so the 2nd, 4th, 6th…98th and 100th are all closed). Then, you go to every third locker and open it if it is closed or close it if it is open (let’s call this toggling the locker for our discussion). You proceed to toggle every nth locker on pass number n. So, for example, on pass number 16 – you will toggle every 16th locker. After your hundredth pass of the hallway, in which you toggle only locker number 100, how many lockers are now open? In a hall with x lockers, how many lockers remain open after pass number x?
Answer:
Start out small with this problem
This isn’t the type of problem that gets solved just by staring at it and hoping an answer just comes to you. So, a good idea is to try some different numbers (much smaller than 100, of course), and see if that helps you notice some patterns.
Let’s just start by choosing a random locker, and let’s determine whether it will end up open or closed. Let’s choose locker # 6.
Let’s go through each pass: Pass # 1: all lockers are opened, including locker # 6 Pass # 2: all even numbered lockers are closed, including locker # 6 Pass # 3: every 3rd locker is toggled…so 3, 6, 9, ….96, 99. includes locker # 6. Pass # 4: 4, 8, 12, etc. are all toggled. Excludes #6. Pass # 5: 5, 10, 15 are all toggled. Excludes # 6 again. Pass # 6: 6, 12, 18, etc. are all toggled. Includes # 6.
Passes greater than 6: Locker #6 will not be toggled again, since those will all start farther down the hall.
So, one thing you may notice after running through this example is that locker #6 is only toggled when the number of the pass (also called “x”) that you are on is a factor of the # 6 – you can see that 1,2, 3, and 6 are all factors of 6. And those are all the passes on which the locker 6 is toggled – the sequence is open, close, open and then close. So, locker # 6 ends up closed.
Now that sounds like promising information. Since we are dealing with factors here, why not try a prime number – since a prime number only has 2 factors – itself and ‘1’. Let’s try the number 13 as our prime number. The factors are 1 and 13 – which means that the operations are open and then close for any pass greater or equal to 13. So, it ends up being closed.
So, we know one thing for sure: that the lockers are only toggled on passes that are factors of the individual locker number.
Do the math – It’s all about the factors
What else do we know? Well, we know that lockers are closed every 2nd, 4th, 6th, etc. times they are toggled – so if a locker is toggled an even number of times it ends up closed, otherwise it ends up open. Combining this knowledge with our other knowledge, we can say that a locker will end up closed if it has an even number of factors, because the number of times a locker is toggled equals the number of factors. If a locker has an odd # of factors, the locker will end up being open.
So, this knowledge makes a big difference – we’ve really simplied the problem – now all we need to do is figure out how many numbers between 1 and 100 have an odd # of factors, and we will have our answer!
First, let’s jump into the math to find our answer. What exactly does it mean when we say that c is a factor of d, for some #’s c and d?
Well, it means that c multiplied by some other number b is equal to d. This also means that b is also a factor of d since multiplication is commutative (c*b = b*c). So, the number of factors is usually even since factors tend to come in pairs. And an even number of factors means that the locker will end up being closed.
But, what if the factors are the same numbers – if b is equal to c? Then, the number d would be a perfect square, since b*b would equal d, which would be a perfect square. This would also mean that we would have an odd number of factors – and an odd number of factors would mean that particular locker will remain open.
Since there aren’t very many perfect squares between 1 and 100, you can list them out – here they are: 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100 – so, exactly 10 lockers will remain open.
Generalizing the solution to this brain teaser
Now, let’s generalize the solution so that we have an answer to the original question:
If there are x lockers, the number of open lockers will be the number of perfect squares between 1 and x (inclusive). To count them, all you need to do is find the square root of the largest perfect square less than or equal to x. So, the general solution would be: floor(sqrt(x))
Here’s an example to help illustrate this: If x = 200, then sqrt(200) = 14.142 ..
And because floor(sqrt(200)) = 14, … there will be 14 open lockers.
In an interview situation you may not have been able to easily get to the answer here. But, the most important thing is that you give it your best shot, and you approach it intelligently!
Here, you should also see the value of breaking the problem down into a more manageable size, finding a pattern, and then coming up with a solution.
6. You are given 8 pennies, 7 of which weigh exactly the same, but one penny weighs less than the other 7. You also have a judge scale. Find the one penny that weighs the least in less than 3 steps.
Answer:
First, you split the 8 pennies into 3 groups of pennies – 2 groups with 3 pennies each and 1 group with 2 pennies. Then, you compare the weight of the first two groups of 3 pennies each by putting them on the scale.
Scenario #1: The 2 groups weigh the same. This means the lightest coin is in the group of 2. So, take those 2 pennies and compare them to each other and find the lightest coin.
Scenario #2: The 1st group weighs more than the 2nd group. Take group #2 (3 pennies) and pick any 2 pennies out of that group of 3. If they weight the same, then the third penny is lighter. If they don’t weigh the same then the lighter one is obviously the lightest penny.
7. How would you weigh an elephant without using a scale?
Answer: Put the elephant in a full tank of water
8. Suppose you have a 3 liter jug and a 5 liter jug. The jugs have no measurement lines on them either. How could you measure exactly 4 liter using only those jugs and as much extra water as you need?
Answer:
- Step 1 : First, fill the 5 liter jug and then pour it into the 3 liter jug. The 5 liter jug now has only 2 liters left.
- Step 2 : Next, empty out the 3 liter jug. Then, pour the 2 liters from the 5 liter jug to the 3 liter jug. So, now the 3 liter jug has 2 liters.
- Step 3 : Fill the 5 liter jug again, and pour 1 liter into the 3 liter jug. Now, what’s left in the 5 liter jug? Well, exactly 4 liters! There’s your answer.
9. Let’s say that you have 25 horses, and you want to pick the fastest 3 horses out of those 25. In each race, only 5 horses can run at the same time because there are only 5 tracks. What is the minimum number of races required to find the 3 fastest horses without using a stopwatch?
Answer: 7次:
1)将25匹马分为5个组,分别比赛后排名为:(第1-5场)
A1,A2,A3,A4,A5;
B1,B2,B3,B4,B5;
C1,C2,C3,C4,C5;
D1,D2,D3,D4,D5;
E1,E2,E3,E4,E5;
假定每组的第一名比赛结果为:A1,B1,C1,D1,E1;
2)接下来要找冠军也很容易,每一组的冠军在一起赛一场就行了,这样可淘汰最后两名及其所在组的全部马匹(共10匹),同时可淘汰第三名所在组排名靠后的其余4匹马,以及第二名所在组的排名最后的3匹,再加上第一名所在小组的最后2名,共计淘汰19名,同时产生25匹中的冠军(即本轮的第一名),
假定A1是第一名(第6场)
3)按照第6场比赛中得到的名次依次把它们在前5场比赛中所在的组命名为A、B、C、D、E。即:A组的冠军是第6场的第1名,B组的冠军是第6场的第2名……每一组的5匹马按照他们已经赛出的成绩从快到慢编号:
A组:1,2,3,4,5
B组:1,2,3,4,5
C组:1,2,3,4,5
D组:1,2,3,4,5
E组:1,2,3,4,5
从现在所得到的信息,我们可以知道哪些马已经被排除在3名以外。只要已经能确定有3匹或 3匹以上的马比这匹马快,那么它就已经被淘汰了。可以看到,只有上表中粗体的那5匹马是有可能为2、3名的。即:A组的2、3名;B组的1、2名,C组的 第1名。
取这5匹马进行第7场比赛,第7场比赛的前两名就是25匹马中的2、3名。故一共最少要赛7场。
10. Escaping the train: Suppose 2 boys are walking in the woods and they decide to take a shortcut through a railroad tunnel. They had walked 2/3 of the way through the tunnel, but then something horrible happened: a train was coming in the opposite direction towards the 2 boys, and it was coming close to the other entrance of the tunnel. Each boy ran in a different direction to get out of the tunnel and avoid the incoming train. Each boy ran at the same exact speed of 10 miles per hour, and each boy managed to escape the train at the exact instant in which the train would have hit and killed him. If the train was moving at a constant speed and each boy was capable of instantaneous acceleration, then how fast was the train going?
Answer: since B travels at a speed of 10 miles per hour, the train moves at 30 miles per hour (3 * 10). So, we have our answer!
11. 囚犯和帽子的问题
一个监狱里关押着100名犯人。一天,监狱长对他们说:我给你们一个释放的机会,但是你们必须要做对我要求的事!一会我将从你们当中随机的抽出若干人,然 后给这些人戴上帽子,帽子分三种:红、绿、蓝。每个人都无法看到自己的帽子,但是可以看到其他人的帽子颜色。然后这些人走到操场上,帽子相同颜色的人分别 站成一排,如果有人站错了排,全体囚犯将被重新关押,失去释放的机会。如果全部站对了,那么将全体获释!等帽子戴上后,任何人都不能交流,也不能用任何肢 体语言将信息传递给别人,否则全体处死!给你们5分钟的时间商量。5分钟后开始选人戴帽子!
如果您是这些囚犯之一,你能想到一个方案以获得释放吗?
Answer:
预先商议好四排的位置,选定一人,统计其他99人(假设100人都参加了)的帽子颜色的奇偶。仅红帽为奇数时,站到第一排,仅蓝色帽子为奇数时站到第二排,仅绿色帽子为奇数时站第三排,三种颜色都为奇数时站第四排。
其他人就可以根据第一人提供的信息以及所看到的另外98顶帽子的颜色的奇偶判断出自己的颜色,依次站到相应的队伍中(前面有相同颜色的就与其站一排,没有相同颜色的就自己站一排),注意,依次,一定要一个一个的来。
12. Suppose there are 4 prisoners named W, X, Y, and Z. Prisoner W is standing on one side of a wall, and prisoners X Y and Z are standing on the other side of the wall. Prisoners X, Y, and Z are all standing in a straight line facing right – so X can see prisoner Y and Z, and Y can see prisoner Z. This is what their arrangement looks like:
W || X Y Z
Where the “||” represents a wall. The wall has no mirrors. So, prisoner W can see the wall and nothing else.
There are 2 white hats and 2 black hats and each prisoner has a hat on his head. Each prisoner can not see the color of his own hat, and can not remove the hat from his own head. But the prisoners do know that there are 2 white hats and 2 black hats amongst themselves.
The prison guard says that if one of the prisoners can correctly guess the color of his hat then the prisoners will be set free and released. The puzzle for you is to figure out which prisoner would know the color of his own hat?
Note that the prisoners are not allowed to signal to each other, nor speak to each other to give each other hints. But, they can all hear each other if one of them tries to answer the question. Also, you can assume that every prisoner thinks logically and knows that the other prisoners think logically as well.
Answer:
Clearly prisoners W and Z can not immediately know anything since neither of those prisoners can see any of the other prisoners. So, let’s instead focus on prisoners X and Y.
Suppose we have the following scenario with the arrangement of different hat colors:
W || X Y Z Black || Black White White
W || X Y Z White || White Black Black
W || X Y Z Black || White Black White
In the example above, prisoner X will see that Y and Z have black and white hats. This means that prisoner X will reason that he can not conclusively say whether or not he has a white hat or a black hat – because he knows that that there are 2 black hats and 2 white hats and he sees 1 black and 1 white, so he himself could be wearing either a white or a black hat.
13. 国王和三个囚犯说:每人戴一顶帽子,帽子不是黑色就是白色。
囚犯们互相隔着玻璃,能看到其它人的帽子颜色,但是看不到自己的帽子颜色。自然也不能说话。
国王实际上给他们都戴的是黑帽。并定了以下规则:
1.囚犯们谁看到其它两个人都是白帽子,能被释放。
2.知道自己的是黑帽子,能被释放。
过了一段时间,A囚犯认定自已就是黑帽子,结果被放,A是怎么推理出来的?
Answer:
假设有>=2个白帽子,肯定有一个会被释放,所以不成立
假设有1个白帽子,A看到其他两个都是黑帽子,A假设自己是白帽子,那其他两个人都知道自己黑帽子了(因为不可能有大于2个白帽子,上面解释了),所以A断定自己一个是黑帽子
14。 Suppose that there is a building with 100 floors. You are given 2 identical eggs. The most interesting property of the eggs is that every egg has it’s own “threshold” floor. Let’s call that floor N. What this means is that the egg will not break when dropped from any floor below floor N, but the egg will definitely break from any floor above floor N, including floor N itself.
For example, if the property of the eggs is that N equals 15, those eggs will always break on any floor higher than or equal to the 15th floor, but those eggs will never break on any floor below floor 15. The same holds true for the other egg since they are identical.
These are very strong eggs, because they can be dropped multiple times without breaking as long as they are dropped from floors below their “threshold” floor, floor N. But once an egg is dropped from a floor above it’s threshold floor
Here is the puzzle: What strategy should be taken in order to minimize the number of egg drops used to find floor N (the threshold floor) for the egg? Also, what is the minimum number of drops for the worst case using this strategy?
Remember that you are given 2 identical eggs which both have the same exact threshold floor.
你拿着两个鸡蛋站在100层的大楼上。鸡蛋或许结实到从楼顶掉下也不会摔破,或许很易碎,在一楼摔下就破碎。最少试验多少次可以找出鸡蛋不会被摔碎的最高楼层?
Answer: 14次。
从14楼丢下第一颗鸡蛋,如果破碎了就往下逐层(1,2,3,……,13)试验,共需14次。如果没有破碎,往上走13层,在27楼第二次丢下第 一颗鸡蛋;如果碎了,换第二颗鸡蛋在(15,16,17,……,26)逐层实验;若仍没碎,往上走12层试验第一颗鸡蛋;以此类推,直到走到第99层。如 果鸡蛋要到100层高度落下才会破碎,总共需要14次尝试。
题目默认了两个条件:
1, 两个鸡蛋很神奇,而且抗碎能力一模一样。
2, 一层就是底层,没有架空层。
把答案列出来更直观:
第1次:14
第2次:+13=27
第3次:+12=39
第4次:+11=50
第5次:+10=60
第6次:+9=69
第7次:+8=77
第8次:+7=84
第9次:+6=90
第10次:+5=95
第11次:+4=99
第12次:+3=102
第13次:+2=104
第14次:+1=105
实际上到倒数第三步就可以了,不过还是需要14次。
Use the linear search method
if the egg doesn’t break, in our original algorithm we would go up x floors to find the next floor to test from. Why not just go up x-1 floors instead of x floors? This would save us 1 drop if we have to do a linear search with the 2nd egg whenever the first egg breaks – because we would be doing the linear search from floors x+1 to floor ( (x+1) + (x-1)) instead of floors x+1 to floor (x+1) + x. So, that is 1 less egg drop. This means that the next floor that should be attempted to drop from is x + (x-1) if the egg does not break from floor x. And by the same reasoning the floor after that would be x + (x-1) + (x-2) if the egg does not break on floor x + (x-1).
This would go on to form a series that looks like this:
x + (x-1) + (x-2) + (x-3) + ... + 1
The series above is what’s called a triangular series which is equal to x(x+1)/2. Because there are 100 floors in the building, we set the sum equal to 100 to solve for x:
x(x+1)/2 = 100
When the sum of the series above equals 100, we get x = 13.651, which rounds up to 14. This means that we should start from floor 14 (which is our x) and then move up x-1 (13) floors to floor 27 if the egg doesn’t break and then move up x-2 (12) floors to floor 39 and so on if the egg still does not break. This is the number of drops required as we move up the floors in the building
这是一个很典型的动态规划问题。用确定minNum[n]表示鸡蛋从高n层的楼摔下不碎需要的最小次数。则有
转移方程:
minNum[n ] = min(1 + max(i – 1, minNum[n-1])) for 1<=i <= n
边界条件:
minNum[0] = 0; minNum[1] = 1
假设i是第一次扔鸡蛋的楼层,如果破了,则为了确定下面楼层中的安全位置,需要从第一层挨着试,需要i-1次,不碎的话上面还有n-i层,还剩两个鸡蛋,需要minNum[n-i]次。
假设需要扔n次,则有
n + (n-1) + (n-2) + … + 1 >= 100
n (n +1) >= 200
min(n) = 14
15. AB运药: A、B两人分别在两座岛上。B生病了,A有B所需要的药。C有一艘小船和一个可以上锁的箱子。C愿意在A和B之间运东西,但东西只能放在箱子里。只要箱子 没被上锁,C都会偷走箱子里的东西,不管箱子里有什么。如果A和B各自有一把锁和只能开自己那把锁的钥匙,A应该如何把东西安全递交给B?
Answer:
A把药放进箱子,用自己的锁把箱子锁上。B拿到箱子后,再在箱子上加一把自己的锁。箱子运回A后,A取下自己的锁。箱子再运到B手中时,B取下自己的锁,获得药物。
16. your are given two hourglasses. One measures 4 minutes an one mesures 7 minutes. How could you measure exactly 9 minutes?
Answer:
step time 4 minute timer 7 minute timer
1 0 start start
2 4 flip 3 minute left
3 7 1 minute left flip
4 8 stop flip(1 minute left)
5 9 stop
17: How could you cut a rectangular cake into two equal pieces when a rectangular piece has already been cut out of it? the cut piece can be of any size and orientation. you are only alllowed to make one straight cut.
Answer:
solution 1: cut horizontally mid-height
solution 2: cut on a line that passes throught the center of both rectangles
18 If you had an infinite supply of water and a 5 liter and 3 liter pails, how would you measure exactly 4 quatts?
Answer:
fill 3, transfer to 5, fill 3, transfer to 5, empty 5(the 3 liter has 1 liter in it now), transfer to 5, fill 3, transfer to 5
references:
- http://www.techinterviewpuzzles.com
- http://www.programmerinterview.com/index.php
- http://www.mytechinterviews.com
- http://puzzlersworld.com