Thursday, September 8, 2011

Solution for calculating the distance

Question:  http://good-interview-puzzles.blogspot.com/2011/09/calculate-distance.html

It is given that the platoon and the last person moved with uniform speed. Also,they both moved for the identical amount of time. Hence, the ratio of the distancethey covered - while person moving forward and backword - are equal.Let's assume that when the last person reached the first person, the platoonmoved X meters forward.Thus, while moving forward the last person moved (50+X) meters whereas theplatoon moved X meters
Similarly, while moving back the last person moved [50-(50-X)] X meterswhereas the platoon moved (50-X) meters.
 Now, as the ratios are equal,(50+X)/X = X/(50-X)(50+X)*(50-X) = X*XSolving, X=35.355 metersThus, total distance covered by the last person= (50+X) + X= 2*X + 50= 2*(35.355) + 50= 120.71 metersNote that at first glance, one might think that the total distance covered by thelast person is 100 meters, as he ran the total lenght of the platoon (50 meters)twice. TRUE, but that's the relative distance covered by the last person i.e.assuming that the platoon is stationary 

calculate the distance


There is a 50m long army platoon marching ahead. The last person in the platoonwants to give a letter to the first person leading the platoon. So while the platoon ismarching he runs ahead, reaches the first person and hands over the letter to him andwithout stopping he runs and comes back to his original position.In the mean time the whole platoon has moved ahead by 50m.The question is how much distance did the last person cover in that time. Assuming that  
he ran the whole distance with uniform speed.

Solution is   http://good-interview-puzzles.blogspot.com/2011/09/solution-for-calculating-distance-it-is.html

Monday, August 1, 2011

Solution: Puzzle : Prisoners and Five - hats.

Question: http://good-interview-puzzles.blogspot.com/2011/08/puzzle-prisoners-and-five-hats.html


The solution

Assuming that A wears a black hat.

• If B wears a black hat as well, C can immediately tell that he is wearing a white hat after looking at the two black hats in front of him.

• If B does not wear a black hat, C will be unable to tell the colour of his hat (since there is a black and a white). Hence, B can deduce from A's black hat and C's response that he (B) is not wearing a black hat (otherwise the above situation will happen) and is therefore wearing a white hat.

This therefore proves that A must not be wearing a black hat.

Puzzle : Prisoners and Five - hats.


Another different condition. only three prisoners and five hats (supposedly two black and three white) are involved. The three prisoners are ordered to stand in a straight line facing the front, with A in front and C at the back. They are told that there will be two black hats and three white hats. One hat is then put on each prisoner's head; each prisoner can only see the hats of the people in front of him and not on his own's. The first prisoner that is able to announce the colour of his hat correctly will be released. No communication between the prisoners is allowed. After some time, only A is able to announce (correctly) that his hat is white. Why is that so?

Solution: Puzzle : Prisoners and Four - hats.


Question: http://good-interview-puzzles.blogspot.com/2011/08/puzzle-prisoners-and-four-hats.html

There are two cases:
In first case, one of the three prisoners (in A, B, C) wears the single off-colour hat, thus the other two can easily deduce the colour of theirs. For example if A is single color then B can see that C and A have different color hats. From this he concludes that his color is not single so he shouts his color.

In second case, the three prisoners wear hats of the same colour, while D wears the off-colour hat. After a while, all four prisoners should be able to deduce that, since none of the others was able to state the colour of his own hat, D must wear the off-colour hat.

Puzzle : Prisoners and Four - hats.



Now the same puzzle with different condition. Instead of two red and two blue hats there are 3 hats of one colour and only 1 hat of another, and the 3 prisoners can see each other i.e. A sees B & C, B sees A & C and C sees A & B. D again not to be seen and only there to wear the last hat.

Answer: http://good-interview-puzzles.blogspot.com/2011/08/solution-puzzle-prisoners-and-four-hats.html

solution: puzzle: prisioners and three hats

Question: http://good-interview-puzzles.blogspot.com/2011/08/puzzle-prisoners-and-three-hats.html

Answer:


For the sake of explanation let's label the prisoners in line order A B and C. Thus B can see A (and his hat colour) and C can see A and B.

The prisoners know that there are only two hats of each colour. So if C observes that A and B have hats of the same colour, C would deduce that his own hat is the opposite colour. However, If A and B have hats of different colours, then C can say nothing. The key is that prisoner B, after allowing an appropriate interval, and knowing what C would do, can deduce that if C says nothing the hats on A and B must be different. Being able to see A's hat he can deduce his own hat colour. (The fourth prisoner is irrelevant to the puzzle: his only purpose is to wear the fourth hat).

Puzzle : Prisoners and three hats


Four prisoners are arrested for a crime, but the jail is full and the jailer has nowhere to put them. He eventually comes up with the solution of giving them a puzzle so if they succeed they can go free but if they fail they are executed.

The jailer puts three of the men sitting in a line. The fourth man is put behind a screen (or in a separate room). He gives all four men party hats . The jailer explains that there are two red and two blue hats. The prisoners can see the hats in front of them but not on themselves or behind. The fourth man behind the screen can't see or be seen by any other prisoner. No communication between the men is allowed.

If any prisoner can figure out and say (out loud) to the jailer what colour hat he has on his head all four prisoners go free. The puzzle is to find how the prisoners can escape.

Answer: http://good-interview-puzzles.blogspot.com/2011/08/solution-puzzle-prisioners-and-three.html

Sunday, July 31, 2011

Solution to amazon puzzle: How to find the age of Children

Question: http://good-interview-puzzles.blogspot.com/2011/07/amazon-interview-puzzle-find-age-of.html


Answer :

             First, determine all the ways that three ages can multiply together to get 72:

72 1 1 (quite a feat for the bartender)
36 2 1
24 3 1
18 4 1
18 2 2
12 6 1
12 3 2
9 4 2
9 8 1
8 3 3
6 6 2
6 4 3
As the man says, that's not enough information; there are many possibilities. So the bartender tells him where to find the sum of the ages--the man now knows the sum even though we don't. Yet he still insists that there isn't enough info. This must mean that there are two permutations with the same sum; otherwise the man could have easily deduced the ages.

The only pair of permutations with the same sum are 8 3 3 and 6 6 2, which both add up to 14 (the bar's address). Now the bartender mentions his "youngest"--telling us that there is one child who is younger than the other two. This is impossible with 8 3 3--there are two 3 year olds. Therefore the ages of the children are 6, 6, and 2.

Pedants have objected that the problem is insoluble because there could be a youngest between two three year olds (even twins are not born exactly at the same time). However, the word "age" is frequently used to denote the number of years since birth. For example, I am the same age as my wife, even though technically she is a few months older than I am. And using the word "youngest" to mean "of lesser age" is also in keeping with common parlance. So I think the solution is fine as stated.

In the sum-13 variant, the possibilities are:
11 1 1
10 2 1
9 3 1
9 2 2
8 4 1
8 3 2
7 5 1
7 4 2
7 3 3
6 6 1
6 5 2
6 4 3

The two that remain are 9 2 2 and 6 6 1 (both products equal 36). The final bit of info (oldest child) indicates that there is only one child with the highest age. This cancels out the 6 6 1 combination, leaving the childern with ages of 9, 2, and 2.


amazon interview puzzle: Find age of children


  A man walks into a bar, orders a drink, and starts chatting with the bartender. After a while, he learns that the bartender has three children. "How old are your children?" he asks. "Well," replies the bartender, "the product of their ages is 72." The man thinks for a moment and then says, "that's not enough information." "All right," continues the bartender, "if you go outside and look at the building number posted over the door to the bar, you'll see the sum of the ages." The man steps outside, and after a few moments he reenters and declares, "Still not enough!" The bartender smiles and says, "My youngest just loves strawberry ice cream." How old are the children?
 A variant of the problem is for the sum of the ages to be 13 and the product of the ages to be the number posted over the door. In this case, it is the oldest that loves ice cream. Then how old are they?

Answer: http://good-interview-puzzles.blogspot.com/2011/07/solution-to-amazon-puzzle-how-to-find.html

Solution for gold for 7 days puzzle

Question: http://good-interview-puzzles.blogspot.com/2011/07/microsoft-puzzle-gold-for-7-days-of.html


The trick is not to try and how to cut in such a way to make 7 equal pieces but rather to make transactions with the worker. Make two cuts on the gold bar such that you have the following sizes of bars.


1/7, 2/7 and 4/7. For convenience sake, I would just refer to the bars as 1, 2 and 4.

At the end of Day 1: Give Bar 1 (You- 2 and 4, Worker- 1)

At the end of Day 2: Give Bar 2, Take back Bar 1 (You- 1 and 4, Worker- 2)

At the end of Day 3: Give Bar 1 (You- 4, Worker- 1 and 2)

At the end of Day 4: Give Bar 4, Take back Bar 1 and Bar 2 (You- 1 and 2, Worker- 4)

At the end of Day 5: Give Bar 1 (You- 2, Worker- 1 and 4)

At the end of Day 6: Give Bar 2, Take back Bar 1 (You- 1, Worker- 2 and 4)

At the end of Day 7: Give Bar 1 (You- Empty, Worker- 1, 2 and 4)

That should take care of everything.


Microsoft Puzzle: Gold for 7 days of Work


Lets us consider someone working for you seven days. You have a Gold bar to pay him. You must pay the worker for their work at the end of every day. If you are only allowed to make two breaks in the gold bar, how do you pay your worker? (Assuming equal amount of work is done during each day thus requiring equal amount of pay for each day).

Solution to 4 quarts of Water puzzle

Question: http://good-interview-puzzles.blogspot.com/2011/07/4-quarts-of-water.html


This question is very simple actually. Since we can’t hold 4 quarts in the 3 quart pail, we have to look to filling up the 5 quart pail with exactly 4 quarts. Lets count the steps as we move along

1. Fill 3 quart pail ( 5p – 0, 3p – 3)
2. Transfer to 5 quart pail (5p – 3, 3p – 0)
3. Fill 3 quart pail ( 5p – 3, 3p – 3)
4. Transfer to 5 quart pail (5p – 5, 3p – 1)
5. Empty 5 quart pail (5p – 0, 3p – 1)
6. Transfer to 5 quart pail (5p – 1, 3p – 0)
7. Fill 3 quart pail ( 5p – 1, 3p – 3)
8. Transfer to 5 quart pail (5p – 4, 3p – 0) We are done!!!

That was easy right. Now for those who are mathematical and need everything solved in terms of a formula, here comes a little more mathematical solution.

Now the general steps are to fill up the 3 quart pail and keep transferring to the 5 quart pail (empty if full) until we hit 4 quarts. Therefore, the total amount of water we filled in the 3 quart pail must be equal to 4 more than a multiple of 5 (since we discard 5 quarts of water at a time). From this, we can derive this formula.
5n + 4 = 3m, where n and m are arbitrary positive integers
n represents the number of times we had to empty the 5 quart pail and m represents the number of times we had to fill up the 3 quart pail.

Now all we have to do is solve for the lowest set of positive integer solutions for {n,m}. {1, 3} is the lowest set. Some of the other solutions are {4, 8}, {7, 13}, {10, 18} and so on.

4 quarts of Water

  If you had an infinite supply of water and a 5 quart and 3 quart pails, how would you measure exactly 4 quarts? and What is the least number of steps you need?

Solution: http://good-interview-puzzles.blogspot.com/2011/07/solution-to-4-quarts-of-water-puzzle.html

Solution to Google Puzzle: Two Eggs Puzzle


Question: http://good-interview-puzzles.blogspot.com/2011/07/google-puzzle-two-2-egg-puzzle.html
Answer :

Let x be the answer we want, the number of drops required.

So if the first egg breaks maximum we can have x-1 drops and so we must always put the first egg from height x. So we have determined that for a given x we must drop the first ball from x height. And now if the first drop of the first egg doesn’t breaks we can have x-2 drops for the second egg if the first egg breaks in the second drop.

Taking an example, lets say 16 is my answer. That I need 16 drops to find out the answer. Lets see whether we can find out the height in 16 drops. First we drop from height 16,and if it breaks we try all floors from 1 to 15.If the egg don’t break then we have left 15 drops, so we will drop it from 16+15+1 =32nd floor. The reason being if it breaks at 32nd floor we can try all the floors from 17 to 31 in 14 drops (total of 16 drops). Now if it did not break then we have left 13 drops. and we can figure out whether we can find out whether we can figure out the floor in 16 drops.

Lets take the case with 16 as the answer

1 + 15 16 if breaks at 16 checks from 1 to 15 in 15 drops
1 + 14 31 if breaks at 31 checks from 17 to 30 in 14 drops
1 + 13 45 .....
1 + 12 58
1 + 11 70
1 + 10 81
1 + 9 91
1 + 8 100 We can easily do in the end as we have enough drops to accomplish the task


Now finding out the optimal one we can see that we could have done it in either 15 or 14 drops only but how can we find the optimal one. From the above table we can see that the optimal one will be needing 0 linear trials in the last step.

So we could write it as

(1+p) + (1+(p-1))+ (1+(p-2)) + .........+ (1+0) >= 100.

Let 1+p=q which is the answer we are looking for

q (q+1)/2 >=100

Solving for 100 you get q=14.
So the answer is: 14
Drop first orb from floors 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100... (i.e. move up 14 then 13, then 12 floors, etc) until it breaks (or doesn't at 100).

Google Puzzle: Two (2) Egg Puzzle


This Puzzle is asked by Google while they had an interview to a select student

for their company:


* You are given 2 eggs.
* You have access to a 100-storey building.
* Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100 th floor.Both eggs are identical.
* You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking.
* Now the question is how many drops you need to make. You are allowed to break 2 eggs in the process

Answer: http://good-interview-puzzles.blogspot.com/2011/07/solution-to-google-puzzle-two-eggs.html

Saturday, July 30, 2011

Solution to Weights Puzzle

Here is the question:
 http://good-interview-puzzles.blogspot.com/2011/07/weighing-in-harder-way.html

[1] A = B: C has a heavy or light coin [18 cases]
[2] A < B: A has a light coin or B has a heavy coin [18 cases]
[3] A > B: A has a heavy coin or B has a light coin [18 cases]

[1] Split C into groups of three coins: C1, C2, C3 and weigh C1 vs. C2.

C1 = C2: C3 has a heavy or light coin [6 cases]
Procedure 1:
 Name the C3 coins c3a, c3b, c3c.
c3a = c3b: if c3a </> c3c, then c3c is the heavy / light coin.
c3a < c3b: if c3a </= c3c, then c3a is light / c3b is heavy.
c3a > c3b: if c3a =/> c3c, then c3b is light / c3a is heavy.

C1 < C2: C1 has a light coin or C2 has a heavy coin [6 cases]
C1 = C3: [C2>C1=C3] C2 has a heavy coin.
Apply Procedure 1 to determine the odd one out.
C1 < C3: [C2>C1<C3] C1 has a light coin. Apply Procedure 1 to determine the light coin.
C1 > C3: [C3>C1>C2] Impossible.

C1 > C2: C1 has a heavy coin or C2 has a light coin [6 cases]
C1 = C3: [C1=C3>C2] C2 has a light coin. Apply Procedure 1 to determine the light coin.
C1 < C3: [C2<C1<C3] C1 Impossible.
C1 > C3: [C2<C1>C3] C1 has a heavy coin. Apply Procedure 1 to determine the heavy coin.

[2] Weigh A vs. C

A = C: [A=C<B] B has a heavy coin [9 cases]
Procedure 2:
Split the B coins into groups of three: B1, B2, B3
B1 = B2: B3 has a heavy coin. Apply procedure 1 to determine which.
B1 < B2: B2 has a heavy coin. Apply procedure 1 to determine which.
B1 > B2: B1 has a heavy coin. Apply procedure 1 to determine which.
A < C: [B>A<C] A has a light coin [9 cases] Apply Procedure 2 to determine which.
A > C: [B>A>C] Impossible

[3] Weigh A vs. C

A = C: [A=C>B] B has a heavy coin [9 cases] Apply Procedure 2 to determine which.
A < C: [B<A<C] Impossible
A > C: [B<A>C] A has a heavy coin [9 cases] Apply Procedure 2 to determine which.

Weighing in a Harder Way


You've got 27 coin, each of them is 10 g, except for 1. The 1 different coin is 9 g or 11 g (heavier, or lighter by 1 g). You should use balance scale that compares what's in the two pans. You can get the answer by just comparing groups of coins.
What is the minimum number weighings that can always guarantee to determine the different coin.

Answer: http://good-interview-puzzles.blogspot.com/2011/07/solution-to-weights-puzzle.html