Monday, July 23, 2007

Bridge

This one is a classic that many of you have probably already heard, but all the more reason why it should definitely be included here. four people are on this side of the bridge. the bridge will be destroyed by a bomb in 17 minutes. everyone has to get across before that. problem is that it's dark and so you can't cross the bridge without a flashlight, and they only have one flashlight. plus the bridge is only big enough for two people to cross at once. the four people walk at different speeds: one fella is so fast it only takes him 1 minute to cross the bridge, another 2 minutes, a third 5 minutes, the last it takes 10 minutes to cross the bridge. when two people cross the bridge together (sharing the flashlight), they both walk at the slower person's pace. can they all get across before the bridge blows up?
person A: 1 minute
person B: 2 minutes
person C: 5 minutes
person D:10 minutes


Solution below...




of course its possible, otherwise it wouldn't be a very interesting question. the only trick is in realizing that you want to get the two slowest people across together, because otherwise you are wasting too much time. but then once you get them across, how do you not make one of them walk back with the flashlight? well, you just have one of the fast people already there waiting to sprint the flashlight back across.
1. A & B cross. total time: 2 minutes.

C |==========================| A
D | | B
|==========================| flashlight

2. B comes back. total time: 4 minutes.

C |==========================| A
D | |
B |==========================|
flashlight

3. C & D cross. total time: 14 minutes.

B |==========================| A
| | C
|==========================| D
flashlight

4. A comes back. total time: 15 minutes.

A |==========================| C
B | | D
|==========================|
flashlight

5. A & B cross. total time: 17 minutes.

|========= =============| A
| KABOOM! | B
|========= =============| C D
flashlight

another solution which is valid is to have A bring the flashlight back in step 2. it only changes the solution slightly. this is supposed to be a "classic" microsoft interview question but it seems absurdly easy to be a good interview question (especially coupled with the fact that everyone has probably heard it already).

Puzzle: Pirates

five pirates have 100 gold coins. they have to divide up the loot. in order of seniority (suppose pirate 5 is most senior, pirate 1 is least senior), the most senior pirate proposes a distribution of the loot. they vote and if at least 50% accept the proposal, the loot is divided as proposed. otherwise the most senior pirate is executed, and they start over again with the next senior pirate. what solution does the most senior pirate propose? assume they are very intelligent and extremely greedy (and that they would prefer not to die).


Solution below...








(to be clear on what 50% means, 3 pirates must vote for the proposal when there are 5 for it to pass. 2 if there are 4. 2 if there are 3. etc... )
solution: most of the time i get people who give answers like "the most senior pirate takes half and divides the rest up among the least senior pirates." um, you missed the whole point to begin with. sorry.
any answer without a specific logic behind it is invalid. if i ask you why pirate 5 gave x coins to pirate 1, please don't say "because he's nice".
now for the real solution. pirate 5 being the most senior knows that he needs to get 2 other people to vote for his solution in order for him not to be executed. so who can he get to vote for him, and why would they choose to vote for him? if you start thinking that pirate 4 will never vote for him, because he would rather have 5 die and then be in charge and take it all for himself, you are on the right track. but it gets more complicated.
lets consider if there were only 1 pirate. obviously he would take it all for himself and no one would complain.
if there were 2 pirates, pirate 2 being the most senior, he would just vote for himself and that would be 50% of the vote, so he's obviously going to keep all the money for himself.
if there were 3 pirates, pirate 3 has to convince at least one other person to join in his plan. so who can he convince and how? here is the leap that needs to be made to solve this problem. pirate 3 realizes that if his plan is not adopted he will be executed and they will be left with 2 pirates. he already knows what happens when there are 2 pirates as we just figured out. pirate 2 takes all the money himself and gives nothing to pirate 1. so pirate 3 proposes that he will take 99 gold coins and give 1 coin to pirate 1. pirate 1 says, well, 1 is better than none, and since i know if i don't vote for pirate 3, i get nothing, i should vote for this plan.
now we know what happens when there are 3 pirates. so what happens with 4? well pirate 4 has to convince 1 other person to join in his plan. he knows if he walks the plank then pirate 3 will get 99 coins and pirate 1 will get 1 coin. pirate 4 could propose giving pirate 1 two coins, and surely pirate 1 would vote for him, since 2 is better than 1. but as greedy as he is, pirate 4 would rather not part with 2 whole coins. he realizes that if he gets executed, then pirate 3's scenario happens and pirate 2 gets the shaft in that scenario (he gets zero coins). so pirate 4 proposes that he will give 1 coin to pirate 2, and pirate 2 seeing that 1 is better than 0 will obviously vote for this plan.
a common objection is that pirate 2 is not guaranteed to vote for this plan since he might hope for the case when there are only 2 pirates and then he gets all the booty. but that is why i said that the pirates are extremely intelligent. pirate 2 realizes that pirate 3 is smart enough to make the optimal proposal, so he realizes that there will never be 2 pirates left, because 3 doesn't want to die and we just showed that 3 has a winning proposal.
so lets sum up at this point
Pirate 1 2 3 4 5
5. ? ? ? ? ?
4. 0 1 0 99 -
3. 1 0 99 - -
2. 0 100 - - -
1.100
once you see the pattern it becomes very clear. you have to realize that when a pirate's plan does not succeed then that means you are in the same situation with one less pirate.
1. pirate 1 needs 0 other people to vote for him. so he votes for himself and takes all the money. 2. pirate 2 needs 0 other people to vote for him. so he votes for himself and takes all the money. pirate 1 gets 0. 3. pirate 3 needs 1 other person to vote for him. he gives 1 coin to pirate 1 for his vote - if we are reduced to 2 pirates, pirate 1 gets 0 so pirate 1 knows 1 is better than none. pirate 3 takes 99. pirate 2 gets 0. 4. pirate 4 needs 1 other person to vote for him. he gives 1 coin to pirate 2 - if we reduce to 3 pirates, pirate 2 gets 0 so pirate 2 knows 1 is better than none. pirate 4 takes 99. pirate 3 gets 0. pirate 1 gets 0. 5. pirate 5 needs 2 other people to vote for him. its clear now that the 2 people he needs to convince are the 2 who get shafted in the 4 pirate scenario - pirate 3 and pirate 1. so he can give them each 1 coin (which is better than 0 - what they would get otherwise) and keep 98 for himself.
Pirate 1 2 3 4 5
5. 1 0 1 0 98
what happens if there are 15 pirates? pirate 15 needs 7 other people to vote for him, so he recruits pirates 13,11,9,7,5,3, and 1 with 1 coin each and keeps 93 coins himself. those pirates will all vote for him because they know that they get 0 coins if he dies and pirate 14 is in charge.
hope you enjoyed this one. its my favorite interview question of all. it really allows the candidate to ask a lot of interesting questions and its really amazing when they reach the solution all by themselves (as all fogcreek employees have done so far).

Friday, July 20, 2007

Gunshot : Puzzle

Lets play a game of Russian roulette. You are tied to a chair and can,t get up.Here is the gun , six chambers all empty. Now I put two bullets in the gun and I put these bullets in the adjecent chambers. I close the barrel and spin it. I put the gun to your head and pull the trigger. Clik and the slot was empty. Now before we start the interview I want to pull the trigger one more time , which one do you prefer , that I spon the barrel first or that I just pull the trigger ?


Solution below...






shoot without spinning. if the barrel is spun, the chance of being shot is 2/6 = 1/3. if it is not spun, there is only one position out of 4 that would lead to being shot, so that chance in this case is 1/4.

since 1/4

Monday, July 16, 2007

100 doors in a row

you have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), etc, until you only visit the 100th door.

question: what state are the doors in after the last pass? which are open which are closed?

Solution below...





for example, after the first pass every door is open. on the second pass you only visit the even doors (2,4,6,8...) so now the even doors are closed and the odd ones are opened. the third time through you will close door 3 (opened from the first pass), open door 6 (closed from the second pass), etc..
you can figure out that for any given door, say door #42, you will visit it for every divisor it has. so 42 has 1 & 42, 2 & 21, 3 & 14, 6 & 7. so on pass 1 i will open the door, pass 2 i will close it, pass 3 open, pass 6 close, pass 7 open, pass 14 close, pass 21 open, pass 42 close. for every pair of divisors the door will just end up back in its initial state. so you might think that every door will end up closed? well what about door #9. 9 has the divisors 1 & 9, 3 & 3. but 3 is repeated because 9 is a perfect square, so you will only visit door #9, on pass 1, 3, and 9... leaving it open at the end. only perfect square doors will be open at the end.

Wednesday, July 11, 2007

Puzzle : Red and Blue Marbles

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?

Solution below..











chance ! chance is easy if you know how to do the formula. we know that we have two choices to make. first we'll pick a jar, and each jar will have a 1/2 chance of being picked. then we'll pick a marble, and depending how we stack the marbles, we'll have a (# of red marbles in jar)/(# of total marbles in jar) chance of getting a red one.
for example, say we put all the red marbles into jar A and all the blue ones into jar B. then our chances for picking a red one are:
1/2 chance we pick jar A * 50/50 chance we pick a red marble
1/2 chance we pick jar B * 0/50 chance we pick a red marble
do the math and you get 1/2 chance for a red marble from jar A and a 0/2 chance for a red marble from jar B. add 'em up and you get the result = 1/2 chance for picking a red marble.
think about it for awhile and see if you can figure out the right combination. we had a 50/50 (guaranteed) chance in picking a red marble from jar A, but we didn't have to have 50 red marbles in there to guarantee those fantastic odds, did we? we could've just left 1 red marble in there and the odds are still 1/1. then we can take all those other marbles and throw them in jar B to help the odds out there.
let's look at those chances:
1/2 we pick jar A * 1/1 we pick a red marble
1/2 we pick jar B * 49/99 we pick a red marble
do the math and add them up to get 1/2 + 49/198 = 148/198, which is almost 3/4.
we can prove these are the best odds in a somewhat non-formal way as follows. our goal is to maximize the odds of picking a red marble. therefore we can subdivide this goal into maximizing the odds of picking a red marble in jar A and maximizing the odds of picking a red marble in jar B. if we do that, then we will have achieved our goal. it is true that by placing more red marbles into a jar we will increase the chances of picking a red marble. it is also true that by reducing the number of blue marbles in a jar we will increase the odds also. we've maximized the odds in jar A since 1/1 is the maximum odds by reducing the number of blue marbles to 0 (the minimum). we've also maximized the number of red marbles in jar B. if we added any more red marbles to jar B we would have to take them out of jar A which reduce the odds there to 0 (very bad). if we took any more blue ones out of jar B we would have to put them in jar A which reduce the odds there by 50% (very bad).

Tuesday, July 10, 2007

PUZZLE : THE WARDEN

The warden meets with 23 new prisoners when they arrive. He tells them, "You may meet today and plan a strategy. But after today, you will be in isolated cells and will have no communication with one another.

"In the prison is a switch room, which contains two light switches labeled 1 and 2, each of which can be in either up or the down position. I am not telling you their present positions. The switches are not connected to anything.

"After today, from time to time whenever I feel so inclined, I will select one prisoner at random and escort him to the switch room. This prisoner will select one of the two switches and reverse its position. He must flip one switch when he visits the switch room, and may only flip one of the switches. Then he'll be led back to his cell.

"No one else will be allowed to alter the switches until I lead the next prisoner into the switch room. I'm going to choose prisoners at random. I may choose the same guy three times in a row, or I may jump around and come back. I will not touch the switches, if I wanted you dead you would already be dead.

"Given enough time, everyone will eventually visit the switch room the same number of times as everyone else. At any time, anyone may declare to me, 'We have all visited the switch room.'

"If it is true, then you will all be set free. If it is false, and somebody has not yet visited the switch room, you will all die horribly. You will be carefully monitored, and any attempt to break any of these rules will result in instant death to all of you"

What is the strategy they come up with so that they can be free?





;

;

;







;

;

;





;

;

;





;

;

;





;

;

;

HINT:

Take a long-term perspective. Solve the puzzler for three prisoners.


Solution below....












Answer: The team nominates a leader. The group agrees upon the following rules:

The leader is the only person who will announce that everyone has visited the switch room. All the prisoners (except for the leader) will flip the first switch up at their very first opportunity, and again on the second opportunity. If the first switch is already up, or they have already flipped the first switch up two times, they will then flip the second switch. Only the leader may flip the first switch down, if the first switch is already down, then the leader will flip the second switch. The leader remembers how many times he has flipped the first switch down. Once the leader has flipped the first switch down 44 times, he announces that all have visited the room.

It does not matter how many times a prisoner has visited the room, in which order the prisoners were sent or even if the first switch was initially up. Once the leader has flipped the switch down 44 times then the leader knows everyone has visited the room. If the switch was initially down, then all 22 prisoners will flip the switch up twice. If the switch was initially up, then there will be one prisoner who only flips the switch up once and the rest will flip it up twice.

The prisoners can not be certain that all have visited the room after the leader flips the switch down 23 times, as the first 12 prisoners plus the leader might be taken to the room 24 times before anyone else is allowed into the room. Because the initial state of the switch might be up, the prisoners must flip the first switch up twice. If they decide to flip it up only once, the leader will not konw if he should count to 22 or 23.

In the example of three prisoners, the leader must flip the first switch down three times to be sure all prisoners have visited the room, twice for the two other prisoners and once more in case the switch was initially up.

Monday, July 9, 2007

Puzzle : The Stark Raving Mad King


A stark raving mad king tells his 100 wisest men he is about to line them up and place either a red or blue hat on each head. Once lined up, they must not communicate amongst themselves nor attempt to look behind them nor remove their own hat.

The king tells the wise men that they will be able to see all the hats in front of them. They will not be able to see the color of their own hat or the hats behind them, although they will be able to hear the answers from all those behind them.

The king will then start with the wise man in the back and ask "what color is your hat?" The wise man will only be allowed to answer "red" or "blue," nothing more. If the answer is incorrect then the wise man will be silently killed. If the answer is correct then the wise man may live but must remain absolutely silent.

The king will then move on to the next wise man and repeat the question.

Before they are lined up, the king makes it clear that if anyone breaks the rules then all the wise men will die. The king listens in while the wise men consult each other to make sure they don't devise a plan to communicate anything more than their guess of red or blue.

What is the maximum number of men they can be guaranteed to save?

.
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
HINT :
To solve this problem, you need to presume that each wise man can count the total number of red hats in front of them without error, that all the wise men have great attention to detail and that all the wise men care about the greater good. The first wise man will give an answer which may result in his death, but will allow each subsequent wise man to give a correct answer.

source : http://www.folj.com/puzzles/very-difficult-analytical-puzzles.htm



Solution below..


















Answer: 99.

Solution:

You can save about 50% by having everyone guess.

You can save 50% or more if every even person agrees to call out the color of the hat in front of them. That way the person in front knows what color their hat is, and if the person behind also has the same colored hat then both will survive.

So how can 99 people be saved? The first wise man counts all the red hats he can see (all but his own) and then answers "blue" if the number is odd or "red" if the number is even. Each subsequent wise man should be able to then guess correctly the color of their hat, based on the number of red hats in front of them, and all the answers from all the wise men behind them.

For example if the first wise man answers "blue" he indicates to everyone that he sees an odd number of red hats. If the second wise man then sees an odd number of red hats, he can deduce that he has a blue hat on. He will then state "blue" so that everyone knows he still saw an odd number of hats. If the third wise man then sees an even number of red hats then he must be wearing a red hat. He will then state "red" so that everyone knows he saw an even number of hats. Etc..

This does not presume an even number of red and blue hats. There could be 94 blue hats and then 6 red ones. All the hats could be red.


Emporer and Wine

You are the ruler of a medieval empire and you are about to have a celebration tomorrow. The celebration is the most important party you have ever hosted. You've got 1000 bottles of wine you were planning to open for the celebration, but you find out that one of them is poisoned.

The poison exhibits no symptoms until death. Death occurs within ten to twenty hours after consuming even the minutest amount of poison.

You have thousands of prisoners at your disposal and just under 24 hours to determine which single bottle is poisoned.

What is the smallest number of prisoners you must have to drink from the bottles to be absolutely sure to find the poisoned bottle within 24 hours?




.....












Solution :-

The answer is 10.

Even if time is a constraint, the solution is 10. The explanation is given below. The trick is 10 bits are enough to represent 1024 numbers. so label the wine bottles as 1...1000. and number the prisoners also as 1...10 so that

bit 0 ---corresponds to prisoner 1

bit 1 --- corresponds to prisoner 2.

;

;

bit 9 --- corresponds to prisoner 10

Number the people 1 through 10. The first bottle of wine is assigned to no one. The first bottle will be drunk by the first person. The second person will drink the second bottle. The third bottle will be drunk by both the first and the second person. The fourth bottle will be drunk by the third person. The fifth bottle will be drunk by the third person and the first person. The sixth bottle will be drunk by the third person and the second person. The seventh bottle will be drunk by all three people, etc... That way if only the first and third people die, then you know it was the fifth bottle. With ten people there are 1024 unique combinations so you could test up to 1024 bottles of wine.

Each of the ten prisoners will taste from about 500 bottles and will have at least a fifty percent chance of living.

No, they wont die of alcohol poisoning. As long as each prisoner is administered about a millilitre from each bottle, they will only consume the equivalent of about one bottle of wine each.

It helps to understand binary numbers. Google it. Another way to picture this is to use a tree diagram, where each level of branching represents another person drinking a unique set of half of the bottles.

Welcome

Welcome to this blog !!! Here I would be posting interesting puzzles that I come across.