# FizzBuzz: the infamous interview question

I’ve been a Software Development Manager at a couple of companies, and I find it interesting that many senior developers are aware of the classic FizzBuzz programming interview question and like to use is as an early qualifier for prospective candidates. What I didn’t realize until researching this post is that apparently FizzBuzz is a game that’s taught to children to help them learn division. Did everyone but me know this? My primary schools certainly never taught it.

You can also see a classic description of the programming task here – https://blog.codinghorror.com/why-cant-programmers-program/.  Anyway, the summary is that you ask the candidate to write a program that will count from 1 to 100 (or 0 to 99 if it makes you feel better).  Let’s just say that it’s a console application.  For each number, write the following:

• If the number is divisible by 3, write “Fizz”
• If the number is divisible by 5, write “Buzz”
• If the number is divisible by both 3 and 5, write “FizzBuzz”
• Otherwise, write the number

Honestly when you write out the question this way, you pretty much give away the answer.  However, sometimes the candidate looks at you strangely and that’s when I write out the expected output (“1…2…Fizz…4…Buzz”, etc.)  Frankly this programming question is so old that I don’t hear it very often anymore in interviews, so I don’t mind posting some solutions since it opens the door to some more interesting solutions.

Basically what you asking the candidate is:

• Can you write a basic program structure with a loop?
• Do you know what the modulus operator is?
• Can you show any initiative to make your solution better?

The most common looks something like this.  Note: I did this in the awesome development tool, LINQPad, which is universally loved by all C# developers (and sometimes those VB.NET guys too.)

 Source code
```void Main()
{
for (int i = 0; i < 101; i )
{
if (i % 15 == 0)
Console.WriteLine("FizzBuzz");
else if (i % 3 == 0)
Console.WriteLine("Fizz");
else if (i % 5 == 0)
Console.WriteLine("Buzz");
else
Console.WriteLine(i);
}
}```

Common mistakes include messing up the equality operator (= vs. ==), not understanding how to use modulo, or failing to use “else” causing multiple conditions to be hit.

There are a few variations on this question that I like to ask.  I’ll ask if the candidate can implement the algorithm without testing for modulo 15.  For really advanced developers I’ll ask them to implement it in one line using LINQ.  If you have any solutions you want to show off, please leave them in comments.  In the future I’ll add some solutions like the 1-liner but for now let’s keep it fun and see what people reply with. (Edit: I’ve started adding some and will continue in the future.)

Variation #1: Eliminate one of your test conditions

To mix things up, one variation I like to ask is “How would you implement it without checking for divisibility by 15?”, or something to that effect.  This throws people off because almost all successful implementations will include it.  The trick here, of course, is to attack the condition of divisible by 3 and divisible by 5 without the additional else condition.  Something like this…

 Source code
```void Main()
{
for (int i = 1; i < 101; i++)
{
var output = String.Empty;
if (i % 3 ==0 ) output += "Fizz";
if (i % 5 ==0 ) output += "Buzz";
Console.WriteLine("{0}", output.Length > 0 ? output : i.ToString());
}
}```

I never said it was pretty.  There are several variations here that might look better to your eye.  Sometimes people will set a bool if a condition has been hit, etc.  Sometimes they get really tripped up, trying to add an else condition, which eliminates the flow for one or the other of the conditions.

Well, feel free to contribute your own take on this.