I do quite a few technical interviews in my current position.  Generally this means that I am responsible for determining a candidate's ability to code clearly, comprehensively, and as fulfills the requirements given them.

I ask lots of different questions to prospective candidates, most of which I'm not going to share here. But one particular question often becomes useful: the FizzBuzz problem.

(Cue thousands of experienced developers rolling their eyes.  I saw you.)

This is a common technical interview question that tends to demonstrate if the interviewee can code at all, rather than if they can code well.  But given how many candidates can't even write code (yes, this does still happen), it's become a useful tool in my interviewing toolbox.

In this post, we're going to show a definitive guide to answering the FizzBuzz problem, with lots of examples in C#.  Let's go!

### The Standard Question

In technical interviews, I normally state the FizzBuzz problem as follows:

"You have a collection of integers, 1 to 100.  I want you to cycle through this collection.  For each number found that is evenly divisible by 3, output the word "Fizz".  For each number that is evenly divisible by 5, output the word "Buzz".   For each number that is evenly divisible by both 3 AND 5, output the word "FizzBuzz".

### What Are Interviewers Looking For?

There are several things that I, as the interviewer, am looking for in candidates who are given this question:

1. I want the candidates to be aware of the remainder operator.  In C#, this operator is the percent symbol %.  It gives the remainder of a division operation (So, 5 % 3 would be 2, and 50 % 5 would be 0).
2. I want candidates to understand the concept of a loop and how to implement one.
3. I want candidates to know how to output text.

A "valid" solution will do these three things.  Exactly how they are done is open for interpretation.

### The Basic Solution

Here's a common pseudocode solution for this problem as stated:

``````GIVEN COLLECTION 1-100
FOR EACH NUMBER X IN COLLECTION
IF X DIVISIBLE BY 3 AND 5
OUTPUT "FizzBuzz"
ELSE IF X DIVISIBLE BY 3
OUTPUT "Fizz"
ELSE IF X DIVISIBLE BY 5
OUTPUT "Buzz"
END IF
END FOR EACH
``````

This same solution in C# would be the following:

``````//Assume this list already exists
List<int> collection = ...;

foreach(int x in collection)
{
if(x % 3 == 0 && x % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (x % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (x % 5 == 0)
{
Console.WriteLine("Buzz");
}
}
``````

### Two Common Mistakes

Assuming the interviewee can solve this problem at all, they often make one of two mistakes.

#### Forgetting the Remainder Operator

By far the most common mistake is forgetting what the remainder (aka modulus) operator is.  To be fair to them, in my experience this is not a commonly-used operator, and so forgetting it is forgivable depending on how the interviewee ultimately solves this problem.  Alas, these people often end up with a solution similar to the one below.

``````List <int> collection = ...;

foreach(int x in collection)
{
if(x / 3 == 0 && x / 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (x / 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (x / 5 == 0)
{
Console.WriteLine("Buzz");
}
}
``````

They use the division operator (/) instead of the remainder operator.  This poses an issue because those IF statements will now never be true, and hence we will never get any output.  In these situations I often have to remind them, gently, of this fact, and more often then not they will reach an alternative, acceptable solution.

The second most common issue interviewees have with the FizzBuzz problem is forgetting that Foreach is a construct in most modern languages.  They then write a solution using a For loop instead:

``````List<int> collection = ...;

for(int x = 0; x < collection.Length; x++)
{
if(collection[x] % 3 == 0 && collection[x] % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (collection[x] % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (collection[x] % 5 == 0)
{
Console.WriteLine("Buzz");
}
}
``````

There's nothing wrong with this solution, at least not technically; it will output the proper "Fizz" and "Buzz" items.  The only thing I'd point out to the interviewee in this situation is that it is a bit more difficult to read.  That said, people who provide this solution will, in my mind, be considered to have solved the problem.

### A Better Solution

If I'm interviewing you, and I ask the FizzBuzz problem, and you provide me with the basic answer from earlier, I will be completely satisfied with that.  It's a good, valid answer, provided you arrived at it naturally and didn't just write it down from memory.  However, there are ways you can make this basic solution better.

Let's re-state the problem:

"You have a collection of integers, 1 to 100.  I want you to cycle through this collection.  For each number found that is evenly divisible by 3, output the word "Fizz".  For each number that is evenly divisible by 5, output the word "Buzz".   For each number that is evenly divisible by both 3 AND 5, output the word "FizzBuzz".

That last sentence, the one that states "evenly divisible by both 3 AND 5" is a trap statement.  I word the problem this way because I want to be super clear about the requirements.  Most interviewees interpret this as an order, translating it to mean that there are three conditions (divisible by 3, divisible by 5, divisible by 3 and 5) that must be fulfilled.  Consequently they will write the three IF statements from the basic solution.

But in many languages you do not need three IF statements; you only need two. Consider the following:

``````List<int> collection = ...;

foreach(int x in collection)
{
if (x % 3 == 0)
{
Console.Write("Fizz");
}
if (x % 5 == 0)
{
Console.Write("Buzz");
}
}
``````

Notice that there are no longer any "else" keywords.  This solution fulfills the requirements as stated.  However, the output from this doesn't look very nice:

"FizzBuzzFizzFizzBuzzFizzFizzBuzzFizzBuzzFizzFizzBuzz..."

I've had two candidates reach this solution, and in my mind they have not only completed this problem but improved upon it: they made the code simpler (at the expense of the output's formatting, but since the technical interview is not the real world I will often let this slide).

The reason I will be impressed by this solution is that it shows that the interviewee can think outside of the bounds of the problem defined; that they can think about the code and how to make it better.  This is a critical skill for modern software developers, and one that I find is all too lacking.

Of course, if you really wanted to blow me away, you could make some modifications to this answer:

``````//Assume this list already exists
List<int> collection = ...;

foreach(int x in collection)
{
bool insertNewline = false;
if (x % 3 == 0)
{
Console.Write("Fizz");
insertNewline = true;
}
if (x % 5 == 0)
{
Console.Write("Buzz");
insertNewline = true;
}

if(insertNewline)
{
Console.Write(Environment.NewLine);
}
}
``````

Which outputs the following:

Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
Fizz
Buzz
Fizz
Fizz
Buzz

...and so on.

There are quite a few ways to improve upon the basic FizzBuzz solution, and using any of them will impress your interviewer.  At the very least, going the extra mile can't hurt.

### Summary

The FizzBuzz problem is a commonly-used technical interview question that helps determine if candidates can write code at all.  It means to show that the interviewee can write a loop, use the remainder operator, and write to the output.  There are two common mistakes, both of which can be avoided.  Finally, going the extra mile and improving upon the original "basic" solution is never a bad thing and may even impress your interviewer.