22. July 2011 18:00

Another great job interview question. Get somebody to write a function that will generate a list of fibonacci numbers and calculate the sum of the values that do not exceed four million.

In case you are not aware the fibonacci sequence is generated by adding the previous two terms of the sequence. So a basic sequence of the numbers looks like. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...

static void Main(string[] args)
{
DateTime Start = DateTime.Now;
UInt64 Total = 0;
UInt64 Fib = 1;
UInt64 Last = 0;
do
{
UInt64 tmp = Fib;
Fib = Fib + Last;
Last = tmp;
Console.WriteLine(Fib);
if (Fib % 2 == 0 && Fib <= 4000000)
Total += Fib;
} while (Fib <= 4000000);
Console.WriteLine("Answer: {0}", Total);
Console.WriteLine("Time Taken: {0}", DateTime.Now - Start);
Console.ReadLine();
}

Something that an interviewed should not do here is ask you to do something with the sequence and not provide you with enough information to generate the sequence. If they do you should probably come back with a question of. do you expect your employee's to know all mathmatical sequences of numbers?

6bf5fd20-d9de-4c7e-85d6-a2ec3c5e64de|0|.0

14. July 2011 18:00

I have always been a fan of the fizz buzz test. Here is another alternative to the test which does much the same but is not quite so aggressive so it is harder to make so many mistakes. The challange actually comes from project eular.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. ind the sum of all the multiples of 3 or 5 below 1000.

The solution of course is simple.

static void Main(string[] args)
{
DateTime Start = DateTime.Now;
int Total = 0;
for (int i = 1; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
Total += i;
}
Console.WriteLine("Answer: {0}", Total);
Console.WriteLine("Time Taken: {0}", DateTime.Now - Start);
Console.ReadLine();
}

46fe63de-021d-4bc1-872e-2496b4204fa2|2|5.0

13. July 2011 18:00

Another common interview question is to write a function to detect if a string is a palindrome. For thoose who are not aware a palindrome is the same word spelt backwards as forwards eg "RACECAR".

Places tend to ask this because it can be solved in multiple ways. The two main methods are either using a for loop or using recursion. I don't think the recusion method is an advantage in c# because of the lack of pointers. So that method is going to be skipped here. The reason why they ask this is because it has a nice catch on how the issue of how to deal with an odd number of letters. This is actually very simple since the odd letter is in the middle it will be compared to its self. So there is never any need to compare the middle letter. An example is "RAC" "E" "CAR".

Here is a solution that will work for both even and odd cases ..

public static bool IsPalindrome(string str)
{
for (int i = 0; i < str.Length / 2; i++)
if (str[i] != str[str.Length - (i + 1)])
return false;
return true;
}

Something that is intresting is that as well as a simple technical test the interviewer can challange on the functionality of the operation of the odd case. Sometimes they do this to see if the person is going to defend them self or just role over and show a lack of confidence in their solution.

9e6d334b-0a2f-40a4-a305-0d3eba633d10|0|.0