Fizz Buzz – The Easiest Interview Question

Have you heard of Fizz Buzz?  It’s commonly used as an basic software interview question or an intro programming example.  It’s based on a game meant to teach children division and goes like this.  The children sit in a circle and count up from one; but, if your number is divisible by 3 you say “Fizz” instead of your number and if your number is divisible by 5 you say “Buzz” instead of your number.  If your number is divisible by both 3 and 5 you say  “Fizz Buzz”.

You can easily see how this would relate very well to coding and could quickly demonstrate your ability to do things like loops, conditionals, etc..  And for anybody who has ever a few weeks of programming, it should be a really easy question.  Hell, it’s a gimme.  But apparently many people struggle with it.  Here is a good article and here is another and here is a youtube video about FizzBuzz.

Many computer science courses teach the mechanics of programming but don’t bother teaching how to think about a problem or how to optimize for it so you end up with competent (supposedly) programmers who can’t code there way out of a wet paper bag.  So the first goal of FizzBuzz is to tell you if a programmer can code even a little bit.  Every programmer worth anything should have no problem with this.  So why bother?  Because the second goal of FizzBuzz is to show how somebody thinks and approaches a problem.  This question is usually asked in person and the interviewer watches you solve it and can learn a lot by the way you approach it.

So lets start with a very simple solution to FizzBuzz.  I’m going to be doing my examples in C because that is what I’m most familiar with.

NOTE: WordPress is currently screwing up the less than sign so I’ve changed the middle part of the for loop to use a != instead.  Should be the same functionality.

for(int i=0; i!=100; i++)
{
    if(i%3 == 0)
        printf("fizz\n");
    if(i%5 == 0)
        printf("buzz\n");
    if(i%3 != 0 && i%5 != 0)
        printf("%d\n", i);
}

And doing this would prove that you know how to code, or at least use a for loop and some if statements.  And this would pass the first test of FizzBuzz, to make sure you know the basics of coding and should be allowed to continue in the interview.  But there are some problems.  The easiest is that it starts at 0 and goes while less than 100.  This does in fact run 100 times but it means the coder is just regurgitating code from muscle memory without thinking about the use case for the for loop.  In this case you would want to start at i-1 and go while i is less than 101.  But the biggest problem is that if you actually run this and print it out, it will print out something like 120 lines instead of the expected 100.  This is because there is a newline character at the end of “fizz” and “buzz” so that they are on separate lines instead of combined for numbers like 15, 30, 45, etc.. Here’s a decent second attempt that fixes these issues.

for(int i=1; i!=101; i++)
{
	if(i%3 == 0 && i%5 == 0)
		printf("FizzBuzz\n");
	else
	{
		if(i%3 == 0)
			printf("fizz\n");
		if(i%5 == 0)
		    printf("buzz\n");
	    if(i%3 != 0 && i%5 != 0)
            printf("%d\n", i);
    }
}

But a better fix would be this:

for(int i=1; i!=101; i++)
{
	if(i%3 == 0)
		printf("fizz");
	if(i%5 == 0)
        printf("buzz");
	if(i%3 != 0 && i%5 != 0)
		printf("%d", i);
	
    printf("\n");
}

So those problems are fixed and this is a valid solution. But is it the best it could be? Personally, if I was asked this in an interview I would write this step and then say something like “ok, this works and this isn’t computationally intensive so any further attempt to optimize it are probably going to just make it harder to read and debug without speeding it up substantially. But if you did want to optimize it you could notice that it’s always checking the fizz and buzz even though they aren’t nearly as common as the numbers so checking numbers first and then only checking fizz and buzz in the ‘else’ case would speed it up.” Before, it was always checking 4 conditionals but now it would check 2 in the best case that occurs 66% of the time and only checking 4 in the other 33%.

for(int i=1; i!=101; i++)
{
	if(i%3 != 0 && i%5 != 0)
		printf("%d", i);
	else
	{
		if(i%3 == 0)
			printf("fizz");
		if(i%5 == 0)
			printf("buzz");
	}
	printf("\n");
}

Anyway, that’s how I’d work through it.  Remember that a big part of this interview question is seeing how you think through a problem so talk out loud and explain what you’re thinking.  And I always prefer to get a working version and then try to optimize it rather than optimizing while working.  Anyway, good luck and hope this helped somebody.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s