30. January 2012 08:00
A short example of doing an inplace string reverse in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b) {
char tmp = *a;
*a = *b;
*b = tmp;
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s <string>\n", argv[0]);
exit(EXIT_FAILURE);
}
char *tmp = strdup(argv[1]);
int len = strlen(tmp);
int i = 0;
for(i=0;i<len/2;i++) {
swap(&tmp[i], &tmp[len - i - 1]);
}
printf("%s\n", tmp);
free(tmp);
return EXIT_SUCCESS;
}

7428fb4c-9f1f-453a-865e-5dce995323bf|0|.0

27. January 2012 17:36

The following example will check for a palindrom in C. You know one of thoose words spelt the same forwards as backwards. The smart part of this program is the edge case where you have an odd number of letters in the string. Well actually this can simply be ignored since there is no point in comparing the middle charecter to its self.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int try(char *str) {
int len = strlen(str);
int i;
for(i =0;i<len/2;i++)
if (str[i] != str[len - 1 - i])
return 0;
return 1;
}
int main(int argc, char**argv) {
char *s1 = "ffoof";
char *s2 = "foof";
char *s3 = "fooof";
if (try(s1))
printf("%s\n", s1);
if (try(s2))
printf("%s\n", s2);
if (try(s3))
printf("%s\n", s3);
return 0;
}

0648ded8-4798-4248-a473-b8e1da1830e2|0|.0

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|2|5.0