Showing posts with label Puzzle. Show all posts
Showing posts with label Puzzle. Show all posts

Wednesday, December 2, 2020

Measuring 6L water from 4L and 9L buckets

Problem: You are given one 4 liter bucket and one 9 liter bucket. The buckets have no measurement lines on them either. How could you measure exactly 6 liter using only these buckets given you have as much extra water as you need.

Solution: Let's say 4L bucket is Bucket_4 and 9L bucket is Bucket_9. Initial value is [Bucket_4: 0, Bucket_9: 0](empty buckets). We can measure 6 liter by following below steps: 

  1. Fill Bucket_9. [Bucket_4: 0, Bucket_9: 9]
  2. Fill Bucket_4 from Bucket_9. [Bucket_4: 4, Bucket_9: 5]
  3. Empty Bucket_4. [Bucket_4: 0, Bucket_9: 5]
  4. Fill Bucket_4 from Bucket_9. [Bucket_4: 4, Bucket_9: 1]
  5. Empty Bucket_4. [Bucket_4: 0, Bucket_9: 1]
  6. Fill Bucket_4 (remaining 1 L water in Bucket_9) from Bucket_9. [Bucket_4: 1, Bucket_9: 0]
  7. Fill Bucket_9. [Bucket_4: 1, Bucket_9: 9]
  8. Fill Bucket_4 from Bucket_9. Bucket_4 can only take 3L more as 1L is already there in Bucket_4. [Bucket_4 : 4, Bucket_9: 6]
Now you can see that at step 8 Bucket_9 will have 6L (9 - 3) water.

Thursday, October 29, 2020

Find the Jar with contaminated pills

Problem: You have 5 jars of pills. Each pill weighs 10 grams, except for contaminated pills contained in one jar, where each pill weighs 9 grams. Given a scale, how could you tell which jar had the contaminated pills in just one measurement?

Solution: Take following number of pills from each jar:

  • Jar 1: 1 pill
  • Jar 2: 2 pills
  • Jar 3: 3 pills
  • Jar 4: 4 pills
  • Jar 5: 5 pills
Take the weight of all these 15 pills. Ideally if all the pills are of same weight i.e. 10 grams then the total weight should have been (10 X 15 =) 150 grams but we know that in one of the jar the pill weight is 9 grams i.e. 1 gram lesser than the usual pills. Let's see how it can impact this calculations:
  1. If Jar 1 has contaminated pills  then the total weight would be 1 X 1 = 1 gram lesser than 150 grams. That means if total weight is (150 - 1 =) 149 grams then we can say Jar 1 has contaminated pills.
  2. If Jar 2 has contaminated pills then the total weight would be 2 X 1 = 2 gram lesser than 150 grams. That means if total weight is (150 - 2 =) 148 grams then we can say Jar 2 has contaminated pills.
  3. With the same logic we can say:
    1. total weight is (150 - 3 =) 147 then answer is Jar 3
    2. total weight is (150 - 4 =) 146 then answer is Jar 4
    3. total weight is (150 - 5 =) 145 then answer is Jar 5
Problem solved 😀.

Wednesday, September 30, 2020

How to measure 45 minutes using two wires

Problem: How do we measure forty-five minutes using two identical wires, each of which takes an hour to burn. We have matchsticks with us. The wires burn non-uniformly. So, for example, the two halves of a wire might burn in 10 minute and 50 minutes respectively.

Solution: We know that the wires are not uniform so we can't say that after 30 minutes half of the wire would have burnt but what we can guarantee is if we burn the wire from both sides then the wire will be burnt in 30 minutes (60 / 2). With this approach we can solve this puzzle easily. Here is how:

  1. Burn the Wire-1 from both side and Wire-2 from single side.
  2. Once the Wire-1 is completely burnt means 30 minutes we burn the Wire-2 from the other side too.
  3. Now the Wire-2 will be completely burnt in next 15 minutes as for 30 minutes it is already burnt from single side and 30 minutes were remaining. As we have started fire from the other end too it will take only 15 (30 / 2) minutes to be fully burnt. 

Tuesday, February 11, 2014

Average salary of n people in the room.

Question:
How can n people know the average of their salaries without disclosing their own salaries to each other?

Solution:
Let's say salaries of n people (P1, P2, P3.....Pn) are S1, S2, S3.....Sn respectively. To know the average of the salary i.e. (S1 + S2 + S3 + ..... + Sn) / n, they will follow the following steps -

  1. P1 adds a random amount, say R1 to his own salary and gives that to P2 (P2 won't be able to know P1's salary as he has added a random amount known to him only). In this case, P2 will receive the figure (S1 + R1) from P1.
  2. P2 does the same and gives the final amount to P3 (without showing that to anyone). Now P3 will get the figure (S1 + R1 + S2 + R2).
  3. Rest of the persons (P3, P4, P5.....Pn) do the same. Pn will give the final amount to P1. Now P1 will have (S1 + R1 + S2 + R2 + S3 + R3 + ...... + Sn + Rn).
  4. Now P1 subtracts his random amount (R1) and gives the final figure to P2 (without showing that to anyone). P2 will now receive the figure (S1 + R1 + S2 + R2 + S3 + R3 + .... Sn  + Rn) - R1 = (S1 + S2 + R2 + S3 + R3 + ...... + Sn + Rn).
  5. P2 subtracts his random amount (R2) and gives the final figure to P3 (without showing it to anyone). P3 will receive the amount (S1 + S2 + S3 + R3 + ..... Sn + Rn).
  6. In the same way every person remaining except (Pn) will subtract their random amount and give it to the next one. Now Pn will have (S1 + S2 + S3 + ..... Sn + Rn).
  7. Now Pn subtracts his random amount (Rn) and then the figure becomes (S1 + S2 + S3 + .... + Sn). Pn will divide this amount by n and get the average i.e. (S1 + S2 + S3 + ..... + Sn) / n and show to every one in the room.
(Puzzle was asked in the Yahoo interview)

Tuesday, December 6, 2011

5 Pirates and 100 Gold Coins

Question:
Five pirates got a chest containing 100 gold coins. Now they need to think about a distribution strategy. The pirates are ranked  (Pirate 1 to Pirate 5, where Pirate 5 is the most powerful). The most powerful pirate gets to propose a plan and then all the pirates vote on it. If at least half of the pirates agree on the plan, the gold is split according to the proposal. If not, then that pirate will be killed and this process continues with the remaining pirates until a proposal is accepted. The first priority of the pirates is to stay alive and second to maximize the gold they get. Pirate 5 devises a plan which he knows will be accepted for sure and will maximize his gold. What is his plan? 

Solution:
Reduce this problem to only 2 pirates. So what happens if there are only 2 pirates. Pirate 2 can easily propose that he gets all the 100 gold coins. Since he constitutes 50% of the pirates, the proposal has to be accepted.

Now in 3 pirates situation, Pirate 3 knows that if his proposal does not get accepted, then pirate 2 will get all the gold and pirate 1 will get nothing. So he decides to give one gold coin to Pirate 1 . Pirate 1 knows that one gold coin is better than nothing so he has to back Pirate 3. Pirate 3 proposes {pirate 1, pirate 2, pirate 3} {1, 0, 99}.
If there are 4 pirates, Pirate 4 needs to get one more pirate to vote for his proposal. Pirate 4 knows that if he dies, pirate 2 will get nothing (according to 3 pirates situation) so he will give one gold coin to pirate 2 to get his vote. So the distribution will be {0, 1, 0, 99}.

Now in 5 pirates situation, Pirate 5 needs 2 votes and he knows that if he dies, pirate 1 and 3 will get nothing. so he can give one gold coin to Pirates 1 and 3 to get their vote. In the end, he proposes {1, 0, 1, 0, 98}.