Brain Teasers 1 10
Brain Teasers 1 10
Brain Teasers 1 10
These riddles and puzzles are developed for testing your problem solving skills in algorithmic thinking and programming.
Coin Toss
You are given a weighted coin that shows head more likely than tail. How can you achieve a fair coin toss using just the given coin? Solution In order to achieve a fair coin toss, you must have two possible outcomes with equal-probability. The easiest way to achieve this with an arbitrary weighted coin is to repeat the throw 2 times: if the outcome is Head-Tail player 1 wins, if the outcome is Tail-Head player 2 wins whereas if the outcome is Head-Head or Tail-Tail you can just repeat the throw. Some more.. In a similar fashion, with n coin throws it is possible to simulate an m faces dish toss, where m can assume all the possible values of the binomial coecient n with b [0, n], that is all the possible congurations b with b Heads and (n b) Tails in n throws. For example, with 4 coin throws it is possible to simulate: a 4-faces dish toss: a 6-faces dish toss: thth - tthh
4 1 4 2
=4 =6
considering httt - thtt - ttht - ttth considering hhtt - htht - htth - thht -
I challange you to a game: we are sitting in front of a round table and we are both given the same huge amount of 1p coins. We take turns putting pennies down without overlapping and without moving other coins. Moreover, every coin must rest on the surface in order to be valid. The rst that is unable to put a coin on the table loses, thats all. You are really clever and you try to convince me to let you start, because you know that if you start you can not lose... what is your plan? Solution You just put your rst coin in the centre of the table. Then, wherever i put my penny on the table, you put yours exactly in the opposite position. In this way, the symmetry of the table is always conserved, and you will always be able to put your coin down, whatever I do... But i know this, and i will not let you start. '$
sc s ss s s s s cs &%
3 The locksmith
The locksmith
You are a locksmith, and 11 friends come to your shop with a strange request: they have a secret treasure hidden in a chest, and they want to lock it. They need to be able to open the chest only if the majority of them is present, i.e. 6 or more people. In order to do this, you can put as many locks as you want on the safe. The chest can be opened if and only if all the locks are open and every lock can be opened by one or more keys, but every key can open only one lock. You can give to every person one or more keys, that they will bring with them. How many locks do you need in order to satisfy the strange request? Solution There is a huge number of combinations of locks and keys we can test, so lets just try to exploit some simple consideration in order to simplify our problem. We know that every group of 6 people need to be able to open all the locks, but a group of 5 dont. This means that for every possible choice of 5 present people there must be at least 1 lock that can not be opened. Moreover, all the 6 remaining people need to have the key for that lock. As this holds true for every possible choice of 6 absent people over 11, we can put one lock on the safe for every subgroup of 6 people and we can give the key of the safe to all the 6 members of the selected subgroup. In this way, the chest can be opened if and only if at least 6 arbitrary friends are present. Thus, every lock will have 6 keys opening it, and there will be one lock for every possible selection of 6 people over 11. number of locks = 11 6 = 11! = 462 6!5!
Moreover, every person will have one key for every possible choice of 5 other people over the 10 remaining if excluding himself, that is the number of subgroups he can belong to. number of keys per person = 10 5 = 10! = 252 5!5!
Lets just double check our result: 462 locks means 462 6 = 2772 keys, that divided equally between 11 people gives 2772/11 = 252 keys per person. Fair enough! Some more.. In order to better understand the given answer we can think about a small group of just 3 friends, A B and C. We will put a lock for every subgroup AB AC BC giving the respective keys to the members of the subgroup. In this way, every time only one person is present there will be one close lock, e.g. if A is present the lock belonging to BC will secure the
3 The locksmith
chest. In the same way, every time 2 or more people will be present all the locks will be opened, e.g if A and B are present, A can open lock AB and AC while B can open lock BC. number of locks = 3 2 = 3! 32 = =3 2!1! 2 2 1 = 2! =2 1!1!
4 The chessboard
The chessboard
You are given an 8x8 grid where two corner squares have been removed and you are being asked to cover all the 62 remaining spaces with 31 dominoes, where a domino can cover two neighbour squares. How can you manage to do this? If you cant, why?
Solution The rst thing we can try to do is to put the dominoes on the chessboard randomly and see if we are able to nd a solution. Unfortunately, every disposition we use is proven to be wrong (its not a real surprise: we usually know that if the question says If you cant, why? it is likely that the it is not possible. So just try to use some general reasoning and some simple considerations. We can picture our grid as a coloured board made of black and white squares, as it is the case of every chessboard. We can see that the removed squares are of the same colour, say black. Thus, we are dealing with a board of 32 white squares and 30 black ones. But we know that every domino will cover both a black and a white square. Thus, it is impossible to cover 32 white squares using 31 dominoes, and the problem has no solution.
5 Noodles
Noodles
I give you a pot containing 10000 noodles, i.e. 20000 noodle ends. I ask you to randomly grab noodle ends pairwise and to merge them until you are left with only noodle loops and no more ends. What is the average number of loops left in the pot? Solution 10000 is quite a huge number, and the easiest way to solve this problem seems to be using inductive reasoning. Lets assume that we are given with N noodles and L loops. If we randomly match 2 noodle ends, we will be left with N-1 noodles and L loops with probability 2N 2 2N 1 , i.e. if the noodle ends belong to dierent noodles, or with N-1 noodles and L loops with probability 1 2N 1 , i.e. if the noodle ends belong to the same noodle. Thus, if we repeat the process iteratively starting from 10000 noodles, we get that the average number of loops is
10000
Loops =
n=1
1 = 5.5869 2n 1
(1)
It is interesting to note that this series get the mean contribution from low n terms. Therefore, if we double the number of starting noodles to 20000, the average number of ending loops becomes 5.9335 and doesnt change signicantly. Despite this, the series would not converge for an innite number of loops. Below you can nd the plot of the number of loops as a function of the number of starting noodles.
6 SWN
SWN
I am on the earth, and if I walk 10 Km South, 10 Km West and 10 Km North I come back to the same position I started from. If we assume that the earth is a perfect sphere and we forget about the dierences between magnetic, geographic and rotational North, can you tell me exactly where I am? Solution If i walk 10Km South, 10Km West and 10 Km North, it looks like i will always end in dierent positions, unless i start from the North Pole. In this case, if i go South and than we move West, i will till be 10 Km South of the North pole, and thus walking North i will end again in my starting point. So i can be in the North Pole. Is this the end of the story? Unfortunately no! If fact, you can easily gure a 10 km circumference circle around the South Pole. If i am on this circle and i walk 10 Km West, i will end in my initial position. Thus, if i start 10 Km North of this circle and i walk South, West and nally North, i will end again at my starting point. And the same thing happens if we consider a 5 km circumference circle, or a 2.5 Km ones, and so on. Now we deal with innite possible positions and no, you have denitively no idea about where i am.
7 3 hats
3 hats
A clever king want to test the logic ability of his servants. So he chose the 3 smartest subjects in his court and he arrange them in a line. Then, he put 3 hats over their heads, and he tell them that the hats have been randomly chosen from an ensemble of 3 white and 2 black hats. The rst servant can see the hats of the other two servants, but not his own. The second servant can see only the hat of the last one subject while the remaining servant doesnt see anything. The king orders that the rst servant who understands the color of his hat has to tell it loudly. After a long long time, a servant tell his colour. Which servant is it, and what is the colour? Solution The hats can not be all black, as the ensemble contains 3 white and 2 black hats. Thus, if the second and the third servants have a black hat, the rst servant would understand that his hat is white, and tell it instantaneously. This is not the case, as he doesnt speak. So at least one of the remaining servants is white. The second servant knows this, and he can see the third servants hat. If it is black, he can deduce that his hat is white, and tell it in a short time. But, again, this is not the case and, after a long long time, the third servant understands that his hat is denitely white, and tells it to the king.
8 1000 Bottles
1000 Bottles
A king owns a cellar containing 1000 bottles of precious wine. One day, an enemy of the king sends a servant with the order to poison him, but the smart king catches the servant in his cellar and manages to make him confess. He discovers that the some poison as been added to only one of his precious wine bottles. Even one drop of the poisoned wine, if drunk, will kill a man in exactly one day. Unfortunately the servant didt remember which bottle contains the poisoned wine. Therefore, the king decides to make the wine tested by some of his servants, in order to tell the poisoned wine from the good one. What is the minimum number of servants that the king must be disposed to kill in order to be sure to tell the poisoned bottle? Solution There are various strategy that can be used in order to solve this problem. However, the information about the poisoned bottle has to come through the servants survival or death outcome. Therefore, every servants can bring no more than one bit of information, and the minimum number of bits necessary to encode a natural number between 1 and 1000 is 10. Therefore, the number of servants that will test the wine will also be 10. A possible way to solve the problem is to tag every bottle with a dierent natural number, from 1 to 1000. We can then write every number using a binary code of 10 bins, where every bin represent a dierent servant. For every bottle, a drop of wine will be drunk by a servant if the bin associated to the servant is 1 in the bottle binary tag. After this, we just have to wait 1 day, and see which servants die. If we set every bin to1 for a dead servant and 0 for a survived one, the nal binary number will give us the number of the poisoned bottle. Is therefore 10 the number of servants that the king must be dispose to kill in order to be sure to nd the poisoned bottle? Not really. In fact, we know that 210 = 1024 > 1000. Therefore, we can decide that the number (1111111111) representing the worst scenario of 10 dead servants will not be associated to none of the bottles. We are now left with 10 possible numbers encoding a scenario of 9 dead servants, and again we can remove them from the set of tag numbers, as 1024 1 10 = 1013 > 1000. Unfortunately, we can not do this again, as there are 109 = 45 numbers encoding a scenario of 2 8 dead servants, too many to be removed from the original set if we want to have a dierent tag for every bottle. Therefore, given an opportune choice of the tags for the bottles, there will always be at least 2 survived servants, and the answer to the original question is 8.
9 Dice Game
10
Dice Game
I oer you to play a game. You will throw a regular 6 faces dice, and every time you get 2, 3, 4, 5 or 6 I will pay you the equivalent amount in pounds. The game will stop only when you achieve 1. Moreover, you will have to pay 15 pounds to me in order to play the game. Do you accept to play? Solution We should ask how much is the value of being involved in such a game, i.e. how much is the expected gain we will achieve if we are playing? Let say we accept to play and we expect to win ; if in the rst turn we get 1 our gain will be 0, while if we get x > 1 our expected gain will be x + , as we will be involved in another turn of the game. Therefore, we can write = 1 0+ 6
6 x=2
1 (x + ) 6
and, rewriting the sum as the sum of the average values of the addends, we can write 1 5 0 + (4 + ) 6 6 Solving the equation, we get = = 20 Therefore yes, you should accept. A faster but maybe less intuitive way to solve this problem is the following: You can think about which price would be fair to oer you at every turn as a price for stopping playing. If you refuse to stop, then if you get x > 1 (this happening with a probability of 5 ) you will win an average of 4 6 pound and you will till be involved in the game, whereas if you get 1 you will be out of the game, losing the oered stopping price (this happening with a probability of 1 ). Therefore, if the stopping price is fair, stopping 6 or playing will be equivalent, and 1 5 = 4 6 6 i.e. = 20 Again yes, you should denitely play the game.
10 Mislabeled Boxes
11
10
Mislabeled Boxes
You are given with 3 boxes containing each 100 candies. One box contains lemon candies, one mango candies and the last a mix of the two (this not necessarily means 50 lemon and 50 mango, it can be whatever, also 99 lemon and 1 mango or the opposite, as long as both candy avours are present). There is a label on each box, but you know that the labels have been modied and, therefore, all the boxes are mislabeled. How many candies do you need to taste in order to be sure to set all the labels correctly? Solution What do you know about the labels? You have three labels and three boxes, this meaning 3 2 = 6 possible combinations, and every candy you taste will give you a single bit of information, i.e. lemon or mango. Six combinations can be encoded in not less than 3 bits, but you also have a huge uncertainty coming from the box containing a mixture of candies. You can taste 98 lemon candies and till not be sure if the right label is lemon or mix. Is this really all what you know about the labels? No: you also know that each label will not belong to the box it is actually xed, as every box is known to be mislabeled. Therefore you just need to taste one single candy from the box labeled mix in order to x the label on that box. In fact the box mislabelled mix can contain only lemon or only mango candies. Lets say you taste a lemon candy, you will also be able to tell that the box containing mango candies is one of the remaining two, and for the same reason as before it can not be the one which is actually labeled mango and there will be only one possible candidate left. The box containing a mix of the candies will nally be the remaining one. Therefore you need to taste only one candy in order to x all the labels: a lot of information from a single candy, isnt it?