Roman Numbers

Task : Write a method that takes an integer and converts it to roman numerals where :

  • 1 = I
  • 4 = IV
  • 5 = V
  • 9 = IX
  • 10 = X
  • 40 = XL
  • 50 = L
  • 90 = XC
  • 100 = C
  • 400 = CD
  • 500 = D
  • 900 = CM
  • 1000 = M

So, as with all these interview questions, there is a trick to this one and it is in the way that numerals are laid out. It implicitly contains the rules for converting to roman numerals.
Here’s a simple solution.
While the number you’re converting is greater the current highest order of magnitude, keep subtracting the largest number in the list, and writing the equivalent numeral to the output string.
Here is a simple implementation using a dictionary as a lookup. It is very important that you subtract from highest to lowest – you can well imagine why!

public static string NumberToRoman(int number)
        {
            Dictionary<int, string> lookup = new Dictionary<int, string>()
            {
                {1,"I"},
                {4,"IV"},
                {5,"V"},
                {9,"IX"},
                {10,"X"},
                {40,"XL"},
                {50,"L"},
                {90,"XC"},
                {100,"C"},
                {400,"CD"},
                {500,"D"},
                {900,"CM"},
                {1000,"M"}
            };

            StringBuilder sb = new StringBuilder();

            foreach (int key in lookup.Keys.Reverse())
            {
                while(number >= key)
                {
                    number = number - key;
                    sb.Append(lookup[key]);
                }
            }

            return sb.ToString();
        }

Primes – Always the bloody primes

Find the first n prime numbers
Don’t make the mistake of only calculating if a number is prime up to n. Find the first n primes.

private static void primes(int n)
 {
    int primesFound = 0;
    int testingNumber = 0;

    while (primesFound < n)
    {
       bool isPrime = true;
       for (int i = 2; i < testingNumber; i++)
       {
          if (testingNumber % i == 0)
          {
             isPrime = false;
          }
       }
       if (isPrime)
       {
          Console.WriteLine(testingNumber);
          primesFound++;
       }
       testingNumber++;
    }
 }

 

Fibonacci Series

The Fibonacci series is another favourite, especially when it comes to testing if you have ever ran into recursion. If you HAVE run into recursion before, then this would probably have been your first exercise too.

A Fibonacci series is simply the series of numbers in which each number is the sum of the two numbers before it, for instance 1, 1, 2, 3, 5, 8, 13, 21, … and so on.

So without further ado – lets hack out some code!


private static int fibonacci(int i) {

    if (i==0)
        return 0;
    else if (i==1)
        return 1;

    return fibonacci(i-1) + fibonacci(i-2);

}

There is of course another way to do it, without using recursion – lets take a look

private static int fibonacci(int i) {
 
     int result = 1;
 
     if (i == 1 || i == 2)
         return 1;
 
     int current = 1;
     int next = 1;
 
     for (int j=3; j <= i; j++)
     {
         result = current + next;
         current = next;
         next = result;
 
     }
 
     return result;
 }

And there we have it. The Fibonacci series two ways. Happy white-boarding.

Factorial!

Getting this question for a whiteboard problem is fun. It allows you to give your interviewer a second long “Are you serious?” look and then turning to the white-board and knocking it out in silence. Of course if you get it wrong you get to wish that you can just melt into the carpet.

The factorial of a number is simply a number multiplied by each integer smaller than itself, all the way down to 1.


public static int factorial(int number){

    if (number < 1){
        return 0;
    }

    int fact = 1;
    for(int i=number; i>0; i--){
        fact=fact*i;
    }

    return fact;

}

Of course, you will probably be asked if you can do this recursively too, so you turn around and and scribble this on the white-board


public static int factorial(int number){

    if (number < 1){
        return 0;
    }

    if (number==1){
        return 1;
    }

    return number*factorial(number-1);

}

Palindrome Test

The palindrome tester is another white-board favorite for interviewees. For those that do not know, a palindrome is any sequence of characters and/or numbers that is the same read forwards or backwards. BOB is a palindrome since BOB is BOB backwards. Get it? Got it? Good!

So, the easiest way of checking if a given string is a palindrome, is to reverse it and check it against the original. As usual, there are a whole bunch of ways to solve this. This solution is the easiest I can think of.

public static void main(String[] args) {

  String input = args[0];

  String reversed = "";

  for (int i = input.length()-1; i&gt;=0; i--)
  {
    reversed += input.charAt(i);
  }

  if (input.equals(reversed))
  {
    System.out.println("Palindrome!");
  }
  else
  {
    System.out.println("NON Palindrome!");
  }
}

Fizz-Buzz

It seems that in every interview ever, this question has been asked :

Print out all the numbers between 1 and 100. If a number is divisible by 3, print “fizz” next to it. If a number is divisible by 5, print “buzz” next to it. If it is divisible by both, print “fizzbuzz” next to it.

Naturally there are many solutions to this problem. I will give my first gut-feel solution here.


public static void main(String[] args) {
 
     for (int i=0; i &amp;lt;= 100; i++){
         System.out.print(i + " ");
         if (i%3 == 0){
             System.out.print("Fizz");
         }
         if (i%5 == 0)
         {
            System.out.print("Buzz");
         }
         System.out.println();
     }
 }

Congratulations, you are a developer.

A few weeks ago I spoke to a senior developer (he calls himself a hacker and insists that the senior developer title is a misnomer, but I digress) about whether or not I should bother applying for a position in the company he works for. I admitted to him that I do not feel confident in my skill and in fact rank myself well below where I should have been at that time. He replied “Well, first of all, congratulations, you are a developer.”

That got me thinking. If this is a common feeling among devs, what can we do about it? This is my solution. I am going to go through every stupid interview question, skills assessment and code kata I can find and publish my solution here. The site will probably change radically in the future and the bog or may not survive, but this is a start at least. I hope that it will help any reader that finds this as much as I hope that it will help me.