C# - Interviews FizzBuzz

10. July 2011 20:13

 

I am going to be posting some interview questions. So lets start with something really simple. There is a large number of people who consitantly fail the fizz buzz test.

 

The question is simple. 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".

 

The solution is also very simple.

 

static void Main(string[] args)
{
    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);
	}
    }
    Console.ReadLine();
}

 

 

The above is a simple test to verify that the person taking the test has the ability to convert logic written in english to logic written in code. For some reson there are a large number of people who still fails this. Here is the list of most common failures and excuses I have seen and what I though about them at the time.

 

Failures

  • They start the loop from 0 instead of 1
  • They get the order of the the if / else if wrong. They check for the i mod 3 before checking for i mod 3 and i mod 5.
  • They get the order of the if wrong. They print out BuzzFizz instead of FizzBuzz.

 

Excuses

The best excuse that I have heard about it is typically oh. Its the stress of a job interview. I would agree that interviews can be stressful. However so can work. The candidate has just told me that they crack under pressure and will be unable to perform inhigh pressure situation or when trying to fix bugs in a stress enviroment eg a tight deadline.

E-mail Kick it! DZone it! del.icio.us Permalink


Comments (4) -

9/24/2012 1:23:02 AM #

I don't crack under work pressure and I have a 20+ years work history to bear that out.  However I do crack under exams and interview pressure.  Go figure.

subs Australia | Reply

11/19/2012 10:46:09 AM #

I feel you! I have 10+ years of work history and deal with the pressure very well! I whine a bit but I do get the job done...

BUT I HATE exams and tech interviews! I just cant stand the pressure. Never understood why.

RagingKore United Kingdom | Reply

2/12/2013 8:03:16 AM #

Same here. Programming for 23 years now and interviews still turn my brain to mush - although I did get the FizzBuzz problem without a hitch. Still, I know I don't think clearly in interviews. And I don't accept that the stress is anywhere near the same as with a tight deadline or real work situation.

In a work situation, you are typically left to battle with the problem. It's not the same as someone sitting and looking at you, watching your every move while you are doing it. Also, you typically have had some time to feel comfortable in the environment. Sitting down at a prospective employer for the first time, to have your entire skill set, all that you are capable of, evaluated on the strength of one or two specific problems is extremely uncomfortable for me.

Furthermore, not that there's a way to measure it in an interview I suppose, there are other important characteristics. What about diligence? The ability to see something through, even if you struggle with part of it. Teamwork. The willingness to assist others. Being able to communicate with users/clients effectively, etc. In my time, I have seen a fair number of mercurial talents who can impress with knocking something out at speed, but then fail to care about validation, or presentation, or much else at all really.

I think it's a little sad that references don't appear to count at all anymore. There was a time when it mattered what previous employers though of you. I suppose it's a reflection on how much deception is being attempted.

Mark South Africa | Reply

2/12/2013 8:15:16 AM #

You mention checking for i mod 3 first as being incorrect, but I actually prefer it. Sometimes, it is a matter of personal preference. I like this solution:

static void Main(string[] args)
{
    for (var i = 1; i < 101; i++)
    {
        if (i % 3 == 0)
        {
            Console.Write("Fizz");
            if (i % 5 == 0)
                Console.Write("Buzz");
        }
        else
        {
            if (i % 5 == 0)
                Console.Write("Buzz");
            else
                Console.Write(i);
        }
        Console.WriteLine();
    }
    Console.ReadLine();
}

Mark South Africa | Reply

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading