25. July 2011 18:00
Another interview question which can be suitable for a test to perform some simple calculations to verify an answer. The question is to find the smallest positive number that is evenly divisible by all the numbers from 1 to 20.
You answer should look something like this.
static void Main(string[] args)
{
DateTime Start = DateTime.Now;
int Answer = 0;
do
{
Answer++;
} while (CanDiv(Answer, 20) == false);
Console.WriteLine("Answer: {0}", Answer);
Console.WriteLine("Time Taken: {0}", DateTime.Now - Start);
Console.ReadLine();
}
static bool CanDiv(int value, int max)
{
for (int i = max - 1; i > 0; i--)
if (value % i != 0)
return false;
return true;
}
The CanDiv function could also do the same calculation with a for loop counting up. However it will result in taking longer to run.
static bool OldCanDiv(int value, int max)
{
for (int i = 1; i < max; i++)
if (value % i != 0)
return false;
return true;
}
60acf657-2d99-459f-8a66-902fb49443aa|0|.0
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|0|.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
11. July 2011 18:00
Another interview favorite is the string reverse. The question is simple. Write a function to reverse a string.
Very simple solution to this.
static string Reverse2(string str)
{
char[] tmp = new char[str.Length];
for (int i = 0; i < str.Length; i++)
tmp[i] = str[str.Length - i - 1];
return new string(tmp);
}
Or you might want to answer it something like this.
static string Reverse(string str)
{
char[] tmp = str.ToCharArray();
Array.Reverse(tmp);
return new string(tmp);
}
The second solution shows more understanding of the c# language it also produces cleaner code. Which is easy to read.
Interviews tend to ask this because it shows dealing with array's and strings. Arrays in c# start at zero but the length's are a total number of items. So lots of people get the answers wrong normally because they forget to convert the length's to the array offsets.
9cf2110e-5396-4424-b08e-b7c531c70426|0|.0