Monday, 17 October 2016

IPv4 & IPv6 “Link-Local Addresses”

In a computer network a link-local address is a network address that is valid only for communications within the network segment (link) or the broadcast domain that the host is connected to.

Link-local addresses are not guaranteed to be unique beyond a single network segment. Routers therefore do not forward packets with link-local addresses. For protocols that have only link-local addresses, such as Ethernet, hardware addresses assigned by manufacturers in networking elements are unique, consisting of a vendor identification and a serial identifier.

IPv4 Link-local Addresses
====================
Link local addresses allow machines to automatically have an IP address on a network if they haven't been manually configured or automatically configured by a special server on the network (DHCP).The host can assign itself an IP address from a range of reserved Link-local addresses. Link local address ranges from 169.254.0.0 -- 169.254.255.255.

Assume a network segment where all systems are configured to acquire IP addresses from a DHCP server connected to the same network segment. If the DHCP server is not available, no host on the segment will be able to communicate to any other. Windows (98 or later), and Mac OS (8.0 or later) supports this functionality of self-configuration of Link-local IP address. In absence of DHCP server, every host machine randomly chooses an IP address from the above mentioned range and then checks to ascertain by means of ARP, if some other host also has not configured itself with the same IP address. Once all hosts are using link local addresses of same range, they can communicate with each other.

These IP addresses cannot help system to communicate when they do not belong to the same physical or logical segment. These IPs are also not routable.

Before an address is chosen from that range, the machine sends out a special message (using ARP which stands for address resolution protocol) to the machines on the network around it (assuming that they also haven't been assigned an address manually or automatically) to find out if 169.254.1.1 is free. If it is, then the machine assigns that address to its network card. If that address is already in use by another machine on the same network, then it tries the next IP 169.254.1.2 and so on, until it finds a free address.
The purpose of these self-assigned link-local addresses is to facilitate communication with other hosts within the subnet even in the absence of external address configuration (via manual input or DHCP). Unlike in IPv6, implementation of IPv4 link-local addresses is recommended only in the absence of a normal, routable address. Hosts pseudo-randomly generate the last two octets of the address to mitigate address conflicts. Because of the broadcast nature of some local networking protocols (for example, Microsoft's NetBIOS), hosts may be able to detect one another even without any preexisting knowledge of the address scheme.

IPv6 Link-Local Unicast Address:
===========================
       A link-local unicast address is an IPv6 unicast address that is automatically configured on an IPv6 node interface by using the link-local prefix FE80::/10 (1111 1110 11) and the interface ID in the EUI-64 format.

        It is used to communicate with other nodes on the same link. The below figure shows two nodes on a single subnet using Link local IP addresses.  Two nodes on a same sub-net communicate using the Link local IP address No need of Routers)
                 
        



        NOTE: Routers will not forward any packets with link-local source or destination addresses to other links.
           
        
How to Use of EUI-64 Format in IPv6 Addresses in link local Address or Global?=================================================
EUI- Extended Universal Identifier
To create the IPv6 interface identifier from the 48-bit (6-byte) Ethernet MAC address:

The hexadecimal digits 0xFF-FE are inserted between the third and fourth bytes of the MAC address.
The Universal/Local bit (the second low-order bit of the first byte of the MAC address) is complemented. If it is a 1, it is set to 0; and if it is a 0, it is set to 1.
For example, for the MAC address of 00-60-08-52-F9-D8:
The hexadecimal digits 0xFF-FE are inserted between 0x08 (the third byte) and 0x52 (the fourth byte) of the MAC address, forming the 64-bit address of 00-60-08-FF-FE-52-F9-D8.
The Universal/Local bit, the second low-order bit of 0x00 (the first byte) of the MAC address, is complemented. The second low-order bit of 0x00 is 0 which, when complemented, becomes 1. The result is that for the first byte, 0x00 becomes 0x02.
As a result, the IPv6 interface identifier that corresponds to the Ethernet MAC address of 00-60-08-52-F9-D8 is 02-60-08-FF-FE-52-F9-D8.

The link-local address of a node is the combination of the prefix FE80::/64 and the 64-bit interface identifier expressed in colon-hexadecimal notation.

As a result, the link-local address of this example node, with the prefix of FE80::/64 and the interface identifier 02-60-08-FF-FE-52-F9-D8, is FE80::260:8FF:FE52:F9D8.

For Example:
=======


Uniqueness mask 000000X0 where X=1 is unique and X=0 in not unique. So if X=1 then the EUI-64 Address is 02 90 27 FF FE 17 FC 0F

NOTE :IMPORTANT
Characteristics:
==========
  •  Mandatory addresses that are used exclusively for communication between  two IPv6 devices on the same link
  •  Automatically assigned by device as soon as IPv6 is enabled
  •  Not routable addresses (Their scope is link-specific only.)
  •  Identified by the first 10 bits (FE80)
  •  Typically created using the EUI-64 format

Addressing:
=======
  •   Link Local Identifier (10 bits): Always begins with FE80::/10 (i.e. 1111 1110 10)
  •  Remainder (54 bits): Could be all zeros or manually configured to another value.
  •   Example: FE80:0000:0000:0000:0987:65FF:FE01:2345 or FE80::987:65FF:FE01:2345 (shorthand format)

Monday, 10 October 2016

Puzzles and Riddles


1. How to cut a piece of cake equally in 8 pieces in 3 shots?
Solution:
Cut #1 – Down the center of the cake (vertically) leaving two equal halves.
Cut #2 – Across the center of the cake (horizontally) leaving four equal slices.
Cut #3 – Through the middle edge of the cake slicing all four of the pieces in equal halves, leaving eight equal slices (four equal tops and four equal bottoms).

2. Adam, Bob, Clair and Dave are out walking: They come to rickety old wooden bridge. The bridge is weak and only able to carry the weight of two of them at a time. Because they are in a rush and the light is fading they must cross in the minimum time possible and must carry a torch (flashlight,) on each crossing. They only have one torch and it can't be thrown. Because of their different fitness levels and some minor injuries they can all cross at different speeds. Adam can cross in 1 minute, Bob in 2 minutes, Clair in 5 minutes and Dave in 10 minutes. Adam, the brains of the group thinks for a moment and declares that the crossing can be completed in 17 minutes. There is no trick. How is this done?
Solution:
Before reading the answer can I interest you in a clue?
Initially most people would tend to assume the quickest way is to have Adam (1) carry the torch and do all the running. This however, is not the case, the quickest time is achieved by having Clair(5) and Dave(10) cross together.
For simplicity Adam, Bob, Clair and Dave will be know as (1), (2), (5) & (10) respectively

The moves are as follows:
Move
Time
(1) & (2) Cross with Torch
2
(1) Returns with Torch
1
(5) & (10) Cross with Torch
10
(2) Returns with Torch
2
(1) & (2) Cross with Torch
2

17
NOTE: Alternatively the second move can be (2) returning with the torch, the times are the same.

3.How many squares are there is a chessboard.
Solution:

If you are thinking answer is 64 think again:)
How about the squares that are formed by combining the smaller square on the chess board [2x2 ,3x3 ,4x4 and so on..]

A 1x1 square can be placed in a chess broad in 8 horizontal and 8 vertical position thus making a total of 8x8=64.
Now lets consider a 2x2 square ,there are 7 horizontal and 7 vertical positions in which a 2x2 square can be placed .WHY ?? because picking 2 adjacent squares from a total of 8 squares  on a side can be done in 7 ways so we have x7x7=49 Ways for 2x2 .Similarly for 3x3 ,4x4 and so on we have the below calculation. 

1x1- 64      8x8
2x2- 49     7x7
3x3- 36     6x6
4x4- 25     5x5
5x5- 16     4x4
6x6- 9       3x3
7x7- 4       2x2
8x8- 1       1x1
Total- 204
64 (1x1) squares on a chess board. But, there are a total of 204 squares on the chessboard.

The answer can be  given by the equation-
Σ{(n)^2} from i=1 to 8.
Here every number, from 1 to 8, denotes the side length of the squares that are possible to be made on the chessboard. The width or every row and column is considered of unit length

        4. A Sailor needs to bring a wolf, a goat, and a cabbage across the river. The boat is tiny and can only carry one passenger at a time. If he leaves the wolf and the goat alone together, the wolf will eat the goat. If he leaves the goat and the cabbage alone together, the goat will eat the cabbage.How can he bring all three safely across the river?










Solution: 
The trick to this puzzle is that you can keep wolf and cabbage together. So the solution would be

The sailor will start with the goat. He will go to the other side of the river with the goat. He will keep goat there and will return back and will take cabbage with him on the next turn. When he reaches the other side he will keep the cabbage there and will take goat back with him.

Now we will take wolf and will keep the wolf at the other side of the river along with the cabbage. He will return back and will take goat along with him. This way they all will cross the river.


    5. You have 8 balls. One of them is defective and weighs less than others. You have a balance to measure balls against each other. In 2 weighing, how do you find the defective one?
Solution:

Defective ball is light

            Weigh(123 and 456) //Try-1

Case:1
if(123 == 456) 
weigh( 7 and 8) //Try-2

Case:2
Weigh(123 and 456)
if( 123 > 456) 
weigh( 1 and 2) //Try-2
If (1 == 2), 3 is heavy
Case:3 
if( 123 < 456) 
weigh( 4 and 5) //Try-2
If (4 == 5), 6 is heavy

So, in TWO tries you can find out the heaviest ball.
So by following above steps in 2 steps, lighter ball can be find out.


     6. A man has two ropes of varying thickness (Those two ropes are not identical, they aren’t the same density nor the same length nor the same width). Each rope burns in 60 minutes. He actually wants to measure 45 mins. How can he measure 45 mins using only these two ropes?
He can’t cut the one rope in half because the ropes are non-homogeneous and he can’t be sure how long it will burn.
Solution: 
He will burn one of the rope at both the ends and the second rope at one end. After half an hour, the first one burns completely and at this point of time, he will burn the other end of the second rope so now it will take 15 mins more to completely burn. So total time is 30+15 i.e. 45 mins.


      7.You have a 3 and a 5 litre water container, each container has no markings except for that which gives you its total volume. You also have a running tap. You must use the containers and the tap in such a way as to exactly measure out 4 litres of water. How is this done?
Solution:            
There are two ways to solve this, maybe the question could be modified to say the 5 litre can doesn't fit under the tap...

 Solution:1
  • Fill the 5 litre can from the tap
  • Empty the 5 litre can into the 3 litre can - leaving 2 litres in the 5 litre can.
  • Pour away the contents of the 3 litre can.
  • Fill the 3 litre can with the 2 litres from the 5 litre can - leaving 2 litres in the 3 litre can.
  • Fill the 5 litre can from the tap.
  • Fill the remaining 1 litre space in the 3 litre can from the 5 litre can.
  • Leaving 4 litres in the 5 litre can.
Solution:2
  • Fill the 3 litre can from the tap.
  • Empty the contents of the 3 litre can into the 5 litre can.
  • Fill the 3 litre can from the tap.
  • Empty the contents of the 3 litre can into the 5 litre can. - Leaving the 5 litre can full and 1 litre      in the 3 litre can?
  • Pour away the contents of the 5 litre can
  • Pour the 1 litre from the 3 litre can into the 5 litre can.
  • Fill the 3 litre can from the tap.
  • Empty the contents of the 3 litre can into the 5 litre can.
  • Leaving 4 litres in the 5 litre can.
        8.You have two beakers – one of 4 litres and other of 5 litres. You are expected to pour exactly  7 litres in a bucket. How will you complete the task?
        Solution: 
  •  Fill in 5 litre beaker and empty it in the 4 litre beaker. You are left with 1 liter in the 5 liter    beaker.  Pour this 1 litre in the bucket.
  •  Repeat step 1 and you will have 2 litres in the bucket.
  •  Fill in the 5 litre beaker and add to the bucket. You now have 7 litres in the bucket.

       9.You have a set of 3 light switches outside a closed door. One of them controls the light inside the room. With the door closed from outside the room, you can turn the light switches on or off as many times as you would like. You can go into the room - one time only - to see the light. You cannot see the whether the light is on or off from outside the room, nor can you change the light switches while inside the room.
No one else is in the room to help you. The room has no windows. Based on the information above, how would you determine which of the three light switches controls the light inside the room?
Solution: 
First Switch on any one of the switch and let it be on for 10 min and then close on the another switch and go inside the room if the bulb is lighted then the correct switch is 2 and if the bulb if not lighted but it is hot (feel it by touching) then it is the 1st switch otherwise the 3 rd. switch.

Let consider the switches: Switch A, B, and C.
1. Turn on the switch A for about 5 minutes.
2. Turn off switch A, and turn on switch B then open the door.


If the bulb is "ON" it is switch B. Else if the bulb in "OFF" but it's hot, it is switch A. Otherwise, it is switch C.

10.Red and Blue balls in a bag: 
You have 20 blue and 13 red balls in a bag. You pull out 2 balls one after another. If the balls are of same color, then you replace them with a Blue ball – but if they are of different colour, you replace them with a Red ball. Once you take out the balls, you do not put them back in the bag so the balls keep reducing. What would be the color of the last ball remaining in the bag?
Typical solution:
A lot of people start calculating probabilities and try to create some sort of a series out of the pattern. I got a similar puzzle in one of the interviews and I tried doing that as well. But, if you go down that route, this becomes very tough and unmanageable. The key to solving this puzzle is to realize that there are odd number of red balls in the bag.
Correct solution:
The right answer is Red. This puzzle looks like a difficult one, till you find out the solution. But, the minute you get the solution, you feel that this was dead simple. If you pull out 2 red balls, you replace them with a blue ball. On the other hand, if you pull out one red and one blue – you replace it with a red ball. So, the red balls would always be odd in numbers – either you remove 2 together or remove 1 and add 1 – so they remain odd always. Hence, the last ball to stay in the bag would be a red ball.


11.A person shoots her wife. Then holds her under water for 5 minutes. Finally, he hangs her. But after 10 minutes they both go out together and enjoy a wonderful dinner together. How can this be?
Solution:
He is photographer :).

12.Find the defective coin: There are 10 stacks of 10 coins each. Each coin weights 10 gms. However, one stack of coins is defective and each coin in that stack weights only 9gms. What is the minimum number of weights you need to take to find which stack is defective? How?




Typical Solution:
The dumbest answer in this situation would be 10 (or 9) attempts, when you weigh each stack. A few people try to arrive at a solution with divide and rule method – divide the stacks in 2 groups of 5 each and weigh any one of them – if it weighs 500 gms then the other group has defective stack. In the next turn you divide the remaining stacks in 2 groups and weigh again. In this manner, you can get to the defective coin in a maximum of 4 measurements at your weighing machine. While this approach is smarter than 10 attempts, it is still not the most efficient way.

Correct solution:
The trick in solving this puzzle lies in creating a weighted stack for measurement. You can find the defective stack in one measurement. How? You take 1 coin from the first stack, 2 coins from the second, 3 from the third and so on. In total you will have 55 coins. If all of them were non-defective, they would weigh 550 gms. If stack 1 is defective, the measure would read 549 gms. If stack 2 is defective, you will read 548 gms. and so on. So by taking one measurement you can identify, which is the defective stack.

                                                                  ALITER
There are 10 bottles each containing 10 tablets. 9 of the bottles have each tablet weighing 1 gram while one bottle has tablets weighing 0.9 grams. You are given a weighing machine (not a weighing balance) and we have to determine in one attempt which is the unique bottle?

Solution:
Take different number of tablets from each bottle and weigh them together. Better way would be to take n number of tablets from the n'th bottle and weigh them together. The answer, in normal case (where each bottle has same 1 gram of tablets) should be (1+2+3...+10) = 55 grams, but as there are some tablets of 0.1 gram lesser weight the error would tell the value of n which is the bottle number. For example, if the total weight is 54.9 grams, the bottle from which only 1 tablet had been taken has lesser weighing tablets.

                                                                  ALITER
You have 10 Jars filled with marbles. Each marble weighs 10 gm, except one Jar which contains defective marbles which weighs 9 gm. Given a scale to weigh, how do you find Jar with defective marbles in just one measurement? Here scale is modern day electronic scale to measure weight
Solution:
Take one marble from jar one, two from jar two, three from jar three etc... And weigh.
Total weight if all jar contained 10 g each should be 550. Subtract the actual weight from 550, and that will be the index of the jar (eg, if 5th jar contain 9 g marbles, then total weight would be 545; 550-545=5(5th jar)

13.You have 12 balls all look identical (in shape, color etc.). All of them have same weight except one defective ball. You don’t know that the defective one is heavier or lighter than other balls. You can use a two sided balance system (not the electronic one). It is given that the minimum no. of measures required to separate the defective ball is three. Find the way you separate the defective ball.

Solution:
Divide the balls into 3 groups of 4 balls.
Number the balls 1, 2, 3, ... 10, 11, 12
Start off with them in 3 groups: [1, 2, 3 and 4], [5, 6, 7 and 8] and [9,10,11 and 12]
Weigh 1, 2, 3 and 4 vs 5, 6, 7 and 8 with 3 possible outcomes:


1. If they balance then 9,10,11,12 have the odd ball, so weigh 6,7,8 vs 9,10,11
    with 3 possible outcomes:
1a   If 6,7,8 vs 9,10,11 balances, 12 is the odd ball. Weigh it against any other ball 
       to  determine if heavy or light.
1b   If 9,10,11 is heavy then they contain a heavy ball. Weigh 9 vs 10, if balanced
       then 11 is the odd heavy ball, else the heavier of 9 or 10 is the odd heavy ball.
1c   If 9,10,11 is light then they contain a light ball. Weigh 9 vs 10, if balanced
       then 11 is the odd light ball, else the lighter of 9 or 10 is the odd light ball.
                               
2. If 5,6,7,8 > 1,2,3,4 then either 5,6,7,8 contains a heavy ball or 1,2,3,4 contains
    a light ball so weigh 1,2,5 vs 3,6,12 with 3 possible outcomes:
2a   If 1,2,5 vs 3,6,12 balances, then either 4 is the odd light ball or 7 or 8 is the
       Odd heavy ball. Weigh 7 vs 8, if they balance then 4 is the odd light ball, or
       the heaviest of 7 vs 8 is  the odd heavy ball.
2b   If 3,6,12 is heavy then either 6 is the odd heavy ball or 1 or 2 is the odd
       light  ball. Weigh 1 vs 2, if balanced then 6 is the odd heavy ball, or the lightest
       of 1 vs 2 is the odd light ball.
2c   If 3,6,12 is light then either 3 is light or 5 is heavy. Weigh 3 against any
       other ball, if balanced then 5 is the odd heavy ball else 3 is the odd light ball.
                               
3. If 1,2,3,4 > 5,6,7,8 then either 1,2,3,4 contains a heavy ball or 5,6,7,8 contains
    a light ball so weigh 5,6,1 vs 7,2,12 with 3 possible outcomes:
3a    If 5,6,1 vs 7,2,12 balances, then either 8 is the odd light ball or 3 or 4 is
        the odd  heavy ball. Weigh 3 vs 4, if they balance then 8 is the odd light ball,
        or the heaviest of  3 vs 4 is the odd heavy ball.
3b    If 7,2,12 is heavy then either 2 is the odd heavy ball or 5 or 6 is the odd
        light ball. Weigh 5 vs 6, if balanced then 2 is the odd heavy ball, or the lightest
        of 5 vs 6 is the odd light ball.
3c    If 7,2,12 is light then either 7 is light or 1 is heavy. Weigh 7 against any
        other ball, if balanced then 1 is the odd heavy ball else 7 is the odd light ball.



14.There were six apples in a basket and six girls in the room. Each girl took one apple, yet one apple remained in the basket how?
Solution: 
The first five girls each took an apple. The sixth girl took the basket as well as the apple in it.

15. What can you  travel around the world while staying in a corner?
 Solution:
 Only a Stamp can do it :)

16.You have 8 jars of the same size and shape. Seven of the jars weigh 5 ounces while the eighth jar weights 6. You have a scale you can use to measure the jars but you can only measure twice. How do you find out which is the heavier jar?
Solution:
You take two jars away. Put 3 jars on one side of the scale and 3 jars on the other side. Measure. If neither of them are heavier, the jar must have been in one of the two you took away. Measure and you will find the jar. If one of the scales did tilt, take 1 jar away of the remaining 3 and measure the two remaining jars on each side of the scale. If one tilts, that is the heavier jar. If neither tilts, the one remaining jar must be the heavy one.


17.A bag contains 64 balls of eight different colors. There are eight of each color (including red). What is the least number you would have to pick, without looking, to be sure of selecting 3 red balls?
Solution:59
The first 56 balls could be of all colors except red. This would leave 8 balls, all of which are red. So any three chosen would be red.

Let's go with the worst case.

Pick 8 balls -- This lot does not contain any red ball
pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.
Pick 8 balls -- This lot does not contain any red ball again.

Up till now we have picked 56 balls of colors other than red.

Now the bag will contain exactly 8 red balls and 
we pick 3 more red balls.
Thus picking a total of 59 balls.

18. A snail is at the bottom of a 30 foot well. Every hour the snail is able to climb up 3 feet, then immediately slide back down 2 feet. How many hours does it take for the snail to get out of the well?
Solution:
There's a whole raft of puzzles of this form, usually 3 steps forward, usually 2 steps back; It might be walking uphill in the snow, something like that, there's a tiny little catch, let’s take a look:

Before reading the answer can I interest you in a clue?
The plan here is that you will slightly out smart yourself and having worked out that the net speed is +1 foot per hour you will just use common sense and say 30feet at 1 foot an hour is 30 hours...

The answer is actually 28 hours. Why? The logic is true, 1 foot an hour for, let's say, 27 hours/feet, then at the end of the 28th hour the snail climbs 3 feet (making 30 feet,) and reaches the top before, this time, not sliding back.
The wording on these types of puzzle is always very precise. Or at least includes specific phrases like 'immediately slide back down' these are obviously physical nonsense but they do point to the answer. The the plan to my mind is to work out the obvious route up to a safe distance from the end, in this case 27 hours/feet then model the exact behaviour. ie that in the next hour it climbs 3 feet and is instantly at the top of the well. Read the wording carefully.

19.How can you measure 6 liters of water using only 4 and 9-liter bowls?
Solution:
  • First fill the 9-liter bucket.
  • Then pour 4 liters over to the 4-liter bucket (there are now 5 liters in the 9-liter bucket), and then pour out the water from the 4-liter bucket.
  • Again pour 4 liters from the 9-liter bucket to the 4-liter bucket and empty it.
  • There will now be just 1 liter left in the 9-liter bucket.
  • Now pour that remaining 1 liter to the 4-liter bucket but this time keep it there.
  • Fill the 9-liter bucket again and then pour water to fill the 4-liter bucket to the top (this only needs 3 more liters).
  • The 9-liter bucket will now contain exactly 6 liters. 
20.Measure exactly 2 liters of water if you have:
  1. 4 and 5-liter glass
  2. 4 and 3-liter glass
Solution:
1.1st Fill the 5-litre bowl, pour water from it to fill the 4-litre bowl, which you empty afterwards. Pour the remaining 1 litre to the 4-litre bowl. Refill the 5-litre bowl and pour water from it to fill the 4-litre bowl (where there is already 1 litre). Thus you are left with 2 litres in the 5-litre bowl.

2.2nd The same principle - this time from the other end. Fill the 3-litre bowl and pour all of the water to the 4-litre bowl. Refill the 3-litre bowl and fill the 4-litre bowl to the top. And there you have 2 litres in the 3-litre bowl.

21.Having 2 sand-glasses: one 7-minute and the second one 4-minute, how can you correctly time 9 minutes?
Solution:
Turn both sand-glasses. After 4 minutes turn upside down the 4-min sand-glass. When the 7-min sand-glass spills the last grain, turn the 7-min upside down. Then you have 1 minute in the 4-min sand-glass left and after spilling everything, in the 7-min sand-glass there will be 1 minute of sand down (already spilt). Turn the 7-min sand-glass upside down and let the 1 minute go back. And that's it. 4 + 3 + 1 + 1 = 9

22.Connect all 9 dots with 4 straight lines without lifting the pencil off the paper, and without          going over the same line twice.









Solution:









23.Three boxes are all labelled incorrectly, and you must get the labels right.
To gain the information you need to move the labels to the correct boxes, you may remove a single item from one of the boxes. You may not look into the boxes, nor pick them up and shake them, etc. Can this be done? If so, how? If not, why not? The labels on the boxes read as follows:
Solution:
The key bit of information here is that "Three boxes are all labeled incorrectly". With this, you can assume that box 3 either has nails or has screws. So if you take an item from that box, you can be sure that that box contains only those items. If the box contained a nail, then box 1 would be screws, and box 2 would be nails and screws. If the box contained a screw, then box 1 would be nails and screws, and box 2 would be nails.

If it is a possibility that any of the boxes could be already correct, then it is impossible to solve this problem. This is because if you pick an item from any box, you would have at least 2 choices for what could actually be in that box.

                                                                     ALITER

There are three boxes, one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly?
Solution:The trick is to actually pick a fruit from the A+O labelled box.

Pick a fruit from box 3:

1) if you pick an Orange:
- box 3's real label can only be O or A
- box 3's current label is A+O
- since ALL LABELS ARE INCORRECT then box 3's real label cannot be A+O
- box 3's new label should then be O by elimination
- since ALL LABELS ARE INCORRECT
- box 1's label is changed to A
- box 2's label is changed to A+O
- SOLVED

2) if you pick an Apple:
- box 3's real label can only be O or A
- box 3's current label is A+O
- since ALL LABELS ARE INCORRECT then box 3's real label cannot be A+O
- box 3's new label should then be A by elimination (not O)
- since ALL LABELS ARE INCORRECT
- box 1's label is changed to A+O
- box 2's label is changed to O
- SOLVED



24.Three people check into a hotel. They pay $30 to the manager and go to their room. The manager finds out that the room rate is $25 and gives the bellboy $5 to return to the guests. On the way to the room the bellboy reasons that $5 would be difficult to split among three people so he pockets $2 and gives $1 to each person. Now each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has another $2, adding up to $29.
Where is the remaining dollar?
Solution:

This is a nice nonsense. Each guest paid $9 because they gave $30 and they were given back $3. The manager got $25 and the difference ($2) has the bellboy. So it is nonsense to add the $2 to the $27, since the bellboy kept the $2.

25.Parents with two children - a son and a daughter - came to a wide river. There was no bridge there. The only way to get to the other side was to ask a fisherman if he could lend them his boat. However, the boat could carry only one adult or two children.
How does the family get to the other side and return the boat to the fisherman?
Solution:
First go the children. Son comes back, and father goes on the other side to his daughter. Then daughter goes back to pick her brother up and they both go to the other side to the father. Son comes back to give the boat to mother who goes to the other side (to father and daughter). Daughter jumps in and goes to her brother so they can both return to their parents. Daughter gets off and son gives the boat back on the first side of the river to the fisherman, who goes on the other side. There the daughter jumps in and goes to her brother to take him back to parents where she (where the whole family meets at last) returns the boat to the fisherman.
The boat crossed the river 13 times.

26.Three missionaries and three cannibals want to get to the other side of a river. There is a small boat, which can fit only two. To prevent a tragedy, there can never be more cannibals than missionaries together.
Solution:
Let M = Missionary and C = Cannibal.
This is how they cross the river:
  1. M,C
  2. M
  3. C,C
  4. C
  5. M,M
  6. M,C
  7. M,M
Now, there are 3 missionaries and 1 cannibal on the other side of the river. This cannibal can fetch the other 2 cannibals one by one.
27.How can you throw a ball as hard as you can and have it come back to you, even if it doesn't bounce off anything? There is nothing attached to it, and no one else catches or throws it back to you. Throw the ball straight up in the air.
Solution:


28.A basket contains 5 apples. Do you know how to divide them among 5 kids so that each one has an apple and one apple stays in the basket?
Solution:

Answer to this riddle goes as follows: 4 kids get an apple (one apple for each one of them) and the fifth kid gets an apple with the basket still containing the apple.

29.Given three bowls: 8, 5 and 3 liters capacity, divide 8 liters in half (4 + 4 liters) with the minimum number of water transfers. Note that the 8-liter bowl is initially filled with 8 liters of water and the other two bowls are empty - that is all water you have.
Solution:
  1. Pour 5 litres from the 8-litre to the 5-litre bowl,
  2. Pour 3 litres from the 5-litre to the 3-litre bowl,
  3. Pour these 3 litres back to the 8-litre bowl,
  4. Pour the remaining 2 litres from the 5-litre to the 3-litre bowl,
  5. Pour 5 litres from the 8-litre to the 5-litre bowl,
  6. Pour the missing 1 litre from the 5-litre to the 3-litre bowl (there should be 4 litres left in the 5-litre bowl),
  7. Pour the 3 litres back from the 3-litre to the 8-litre bowl (and that's it - in 8-litre bowl 4 litres).
30.You have two sand timers with you. One can measure 7 minutes and the other sand timer can measure 11 minutes. This means that it takes 7 minutes for the sand timer to completely empty the sand from one portion to the other.
You have to measure 15 minutes using both the timers. How will you measure it?















Solution: Mathematically

7 Minutes Sand Timer Finished.

Time Remaining in 11 minutes timer – 4 minutes

Reversing the 7 minutes timer – 4 minutes will elapse. 3 Minutes will left.

Once 11 minutes gets over reverse the 11 minutes timer again to use that 3 minutes. 8 Minutes left.

Now Reverse 7 minutes timer to measure 7+8 =  15 minutes.

31.Pirate puzzle











There are 5 pirates in a ship. Pirates have hierarchy C1, C2, C3, C4 and C5.C1 designation is the highest and C5 is the lowest. These pirates have three characteristics:
1. Every pirate is so greedy that he can even take lives to make more money.
2. Every pirate desperately wants to stay alive.
3. They are all very intelligent.

There are total 100 gold coins on the ship. The person with the highest designation on the deck is expected to make the distribution. If the majority on the deck does not agree to the distribution proposed, the highest designation pirate will be thrown out of the ship (or simply killed). Only the person with the highest designation can be killed at any moment. What is the right distribution of the coins proposed by the captain so that he is not killed and does make maximum amount?

Maximum time to solve the puzzle: 15 Minutes J
Difficulty level of the puzzle: High
What is the interviewer looking for: Your approach and solution structure.
Solution:
The solution of this problem lies in thinking through what will happen if all the pirates were thrown one by one and then thinking in reverse order.

Let us name pirates as A, B, C, D and E in hierarchy (A being highest).

If only D and E are left at end, D will simply give 0 coins to E and still escape because majority cannot be reached. Hence, even if E gets 1 coin he will give his vote to the distributor.

If C, D and E are there on the deck, C will simply give one coin to E to get his vote. And D simply gets nothing. Hence, even if D gets 1 coin he will give his vote to the distributor.

If B, C,D and E are there on the deck, B will simply give one coin to D to get his vote. C & E simply gets nothing.

If A, B,C,D and E are there on the deck, A simply gives 1 coin each to C and E to get their votes.

Hence, in the final solution A gets 98 coins and only C & E get 1 coin each.

32.Four glasses are placed on the corners of a square table. Some of the glasses are upright (up) and some upside-down (down). A blindfolded person is seated next to the table and is required to re-arrange the glasses so that they are all up or all down, either arrangement being acceptable, which will be signaled by the ringing of a bell. The glasses may be re-arranged in turns subject to the following rules. Any two glasses may be inspected in one turn and after feeling their orientation the person may reverse the orientation of either, neither or both glasses. After each turn the table is rotated through a random angle. The puzzle is to devise an algorithm which allows the blindfolded person to ensure that all glasses have the same orientation (either up or down) in a finite number of turns. The algorithm must be non-stochastic i.e. it must not depend on luck.











Solution:

  1. On the first turn choose a diagonally opposite pair of glasses and turn both glasses up.
  2. On the second turn choose two adjacent glasses. At least one will be up as a result of the previous step. If the other is down, turn it up as well. If the bell does not ring then there are now three glasses up and one down(3U and 1D).
  3. On the third turn choose a diagonally opposite pair of glasses. If one is down, turn it up and the bell will ring. If both are up, turn one down. There are now two glasses down, and they must be adjacent.
  4. On the fourth turn choose two adjacent glasses and reverse both. If both were in the same orientation then the bell will ring. Otherwise there are now two glasses down and they must be diagonally opposite.
  5. On the fifth turn choose a diagonally opposite pair of glasses and reverse both. The bell will ring for sure.


33.You have two identical eggs. Standing in front of a 100 floor building, you wonder what is the maximum number of floors from which the egg can be dropped without breaking it. What is the minimum number of tries needed to find out the solution?
Solution:
Answer: The easiest way to do this would be to start from the first floor and drop the egg. If it doesn’t break, move on to the next floor. If it does break, then we know the maximum floor the egg will survive is 0. If we continue this process, we will easily find out the maximum floors the egg will survive with just one egg. So the maximum number of tries is 100 that is when the egg survives even at the 100th floor.

Can we do better? Of course we can. Let’s start at the second floor. If the egg breaks, then we can use the second egg to go back to the first floor and try again. If it does not break, then we can go ahead and try on the 4th floor (in multiples of 2). If it ever breaks, say at floor x, then we know it survived floor x-2. That leaves us with just floor x-1 to try with the second egg. So what is the maximum number of tries possible? It occurs when the egg survives 98 or 99 floors. It will take 50 tries to reach floor 100 and one more egg to try on the 99th floor so the total is 51 tries. Wow, that is almost half of what we had last time.

Can we do even better? Yes we can (Bob, the builder). What if we try at intervals of 3? Applying the same logic as the previous case, we need a max of 35 tries to find out the information (33 tries to reach 99th floor and 2 more on 97th and 98th floor).

Interval – Maximum tries
1 – 100
2 – 51
3 – 35
4 – 29
5 – 25
6 – 21
7 – 20
8 – 19
9 – 19
10 – 19
11 – 19
12 – 19
13 – 19
14 –  20
15 –  20
16 –  21

So picking any one of the intervals with 19 maximum tries would be fine.

Update: Thanks to RiderOfGiraffes for this solution.

Instead of taking equal intervals, we can increase the number of floors by one less than the previous increment. For example, let’s first try at floor 14. If it breaks, then we need 13 more tries to find the solution. If it doesn’t break, then we should try floor 27 (14 + 13). If it breaks, we need 12 more tries to find the solution. So the initial 2 tries plus the additional 12 tries would still be 14 tries in total. If it doesn’t break, we can try 39 (27 + 12) and so on. Using 14 as the initial floor, we can reach up to floor 105 (14 + 13 + 12 + … + 1) before we need more than 14 tries. Since we only need to cover 100 floors, 14 tries is sufficient to find the solution.

Therefore, 14 is the least number of tries to find out the solution.


34.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 black and two white hats; that each prisoner is wearing one of the hats; and that each of the prisoners is only to 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 prisoners is allowed.
If any prisoner can figure out and say to the jailer what color hat he has on his head all four prisoners go free. If any prisoner suggests an incorrect answer, all four prisoners are executed. The puzzle is to find how the prisoners can escape, regardless of how the jailer distributes the hats.


Solution:C calls out he is wearing a black hat.
Explanation1: Prisoner A and B are in the same situation – they have no information to help them determine their hat color so they can’t answer. C and D realize this.
Prisoner D can see both B and C’s hats. If B and C had the same color hat then this would let D know that he must have the other color.
When the time is nearly up, or maybe before, C realizes that D isn’t going to answer because he can’t. C realizes that his hat must be different to B’s otherwise D would have answered. C therefore concludes that he has a black hat because he can see B’s white one.

Explanation2:
C calls out that he is wearing a black hat. Why is he 100% certain of the color of his hat?
After a while, C comes to the realization that he must answer.
This is because D can't answer, and neither can A or B.
D can see C and B, but can't determine his own hat color. B can't see anyone and also can't determine his own hat color. A is in the same situation as B, where he can't see anyone and can't determine his own hat color.
Since A, B, and D are silent, that leaves C. C knows he is wearing a black hat because if D saw that both B and C were wearing white hats, then he would have answered. But since D is silent, C knows that he must be wearing a black hat as he can see that B is wearing a white hat.