Wednesday, August 15, 2012

.Net Interview Puzzles

Links to good Puzzels:
http://www.yoda.arachsys.com/csharp/teasers.html
http://weblogs.asp.net/ssadasivuni/archive/2005/01/12/351335.aspx
http://www.acetheinterview.com/questions/cats/index.php/microsoft_google

-------------------------------------------------------------------------------------------

Swap two variables without using a temp variable

startAngle = startAngle+stopAngle;
stopAngle = startAngle-stopAngle;
startAngle = startAngle-stopAngle;

or completeness, here is the binary XOR swap:
int x = 42;
int y = 51236;
x ^= y;
y ^= x;
x ^= y;

--------------------------------------------------------------------------------------------
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
for(int i =1;i<=100;i++)
            {
                 if (((i % 5) == 0) && ((i % 3) == 0))
                {
                    Console.WriteLine("FizzBuzz");
                }
                else if (((i % 5) == 0) )
                {
                    Console.WriteLine("Buzz");
                }
                else if (((i % 3) == 0) )
                {
                    Console.WriteLine("Fizz");
                }
                else
                {
                    Console.WriteLine(i);
                }
            }
------------------------------------------------------------------------------------------------

"There are two ropes. Each is one metre long. You have some matches. Each rope burns in one hour. The ropes do not burn linearly. That is, half the rope does not necessarily burn in half an hour. How do you measure out forty-five minutes?"

"You set light to both ends of the rope 1 and just one end of rope 2. It will take half an hour for the two burning ends of rope 1 to meet. Then you set light to the remaining end of rope 2. The time it will take for rope 2 to finish burning will be a further 15 minutes. Hence all together, both ropes burned in this manner will take 45 minutes to burn."
From: http://www.brainj.net/puzzle.php?id=interview
-------------------------------------------------------------------------------------------------

static void Main(string[] args)
{
    for (byte b = byte.MinValue; b <= byte.MaxValue; b++)
        Console.Write(b);
}

// A little tip
// byte.MinValue = 0
// byte.MaxValue = 255

How many times the above statement will be executed ?
Answer: Endless loop
From: http://kossovsky.net/index.php/2009/07/csharp-byte-iteration-question/

No comments:

Post a Comment