Wednesday, April 15, 2009

Big endian or litle endian?

Use this below code to find out whether a processor is big-endian or little endian,

 int x = 1;
if(*(char *)&x == 1)
printf("little-endian\n");
else printf("big-endian\n");
or a union:
 union {
int i;
char c[sizeof(int)];
} x;
x.i = 1;
if(x.c[0] == 1)
printf("little-endian\n");
else
printf("big-endian\n");

Fake coin!


You have 10 bags with coins (each containing more than 100 coins ) and a pair of scales (one that doesn't compare weight's ,it just counts it). 9 of the bags contain real coins each weighing 10 grams and one contains false each weighing 9 grams .How can you find out which bag contains the false coins by using the scales just one time.

Answer : Take 1 coin from the first bag, 2 coins from the second bag, etc. through 10 coins from the 10th bag. If all of the bags contained genuine coins this would total 550 grams. The number of grams short of 550 tells you the bad bag.

Monday, February 23, 2009

Boy or Girl?

This puzzle looks very simple, but is it really that simple? Hmmm, let analyze.

Problem is,
1) A (random) family has two children, and the older child is a boy. What is the probability that the younger child is a girl?
2) A (random) family has two children, and one of the children is a boy. What is the probability that the younger child is a girl?
3) A (random) family has two children, and one of the children is a boy named Jacob. What is the probability that the other child is a girl?

Solution:
1). In this problem, a random family is selected. In this sample space, there are four equally probable events:
  • Both older and Younger children are Girl
  • Older child is Girl and Younger child is Boy
  • Old Child is Boy and Younger one is Girl.
  • Both children are Boys.
Only two of these possible events meets the criteria specified in the question (e.g., BG, BB). Since both of the two possibilities in the new sample space {BG, BB} are equally likely, and only one of the two, BG, includes a girl, the probability that the younger child is a girl is 1/2.

2) This question is identical to question one, except instead of specifying that the older child is a boy, it is specified that one of them is a boy. Again, there are four equally probable events for a two-child family as seen in the sample space above. But sample space will now be {BB, BG, GB}. Only one out of these satisfies the case where younger child will be Girl. So, probability is 1/3.

3) Looking at the problem, it looks like sample space here is {BB,BG,GB,GG}. NO!. There could be the cases where first child is a boy and second child is also a Boy named Jacob. Based on this, we arise at following alternatives,
  • Both children are Boys
  • Both children are Girls
  • Older child is Boy and Younger child is Jacob.
  • Older child is Girl and Younger child is Jacob.
  • Older child is Jacob and Younger child is Boy.
  • Older child is Jacod and Younger child is Girl.
First 2 alternatives will not be included in sample space, as it doesn't have Jacob in it, and thus violating problem statement. So, our sample space will be {BJ,GJ,JB,JG}. 2 out these sample space has Girl. So, answer is 1/2??
Is this answer correct? NO!!!
Reason is, above solution doesn't take into account different frequencies of each of these answers. The likelihood of a boy being named Jacob and a boy not being named Jacob are not equal. Thus, we must replace our classical interpretation of probability with either a Frequentist or Bayesian interpretation. More details can be found at Wikipedia or CurioUser. This is one of the puzzle Adobe asks during interview.

Date cube

I do not have an calender at home. To know the day, I have 2 cube with some numbers marked in it. Everyday, when I get up, I arrange these 2 cubes such as to show the current day of the month (like 01-31). Problem here is, how each of the cube faces should be numbered?

Answer:
If you don't want the leading zero, then you only need the digit 0 on one of the cubes so you can represent 10, 20, and 30. Create one cube with (1,2,3,4,5,6) and the other with (0,1,2,7,8,9) and you can represent any date from 1 to 31 (well, really 32).

If you do want the leading zero, you need to have a 0 on both cubes to represent each date from 01 through 09. However, this presents a problem: You need 0, 1, and 2 on each cube, but that only leaves room for six more digits, and there are seven left to place (3 through 9). The trick is to note that 6 and 9 look pretty much the same when turned upside down, so you can omit one of them and turn the cube over to represent either one. So, you have cubes with (0,1,2,3,4,5) and (0,1,2,6,7,8).

Mersenne prime

A Mersenne number is a positive integer that is one less than a power of 2.
Mn = 2n − 1
The Mersenne numbers consist of all 1s in base-2. The first few Mersenne numbers are 1, 3, 7, 15, 31, 63, 127, 255, ... , corresponding to 1, 11, 111, 1111, 11111... in binary.

A Mersenne prime is a Mersenne number that is prime. Read more here.

Synchronize the clock

Guys,

Well, lets try to solve some interesting puzzles.

There is a clock at the bottom of the hill and a clock at the top of the hill. The clock at the bottom of the hill works fine but the clock at the top doesn’t. How will you synchronize the two clocks. Obviously, you can’t carry either of the clocks up or down the hill! And you have a horse to help you transport yourself. And, the time required for going up the hill is not equal to the time required to go down the hill.

Answer : Basic idea to solve this puzzle is to find out the time required to travel up the hill with horse. Once we have this, we can synchronize the clock at the top of hill with clock at bottom plus time required to travel from bottom to top with horse.
To obtain this time, setup 4 equations,
i) Time required for uphill with horse + time required for downhill with horse = w,
ii) Time required for uphill without horse + time required for downhill without horse =x,
iii) Time required for uphill with horse + time required for downhill without horse = y,
iv) Time required for uphill without horse + time required for downhill with horse = z.
Solve these four equation, involving 4 variables, you will get time required to travel uphill with horse.

Pretty simple? Huh? :)