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