How Do You Solve FizzBuzz?

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”.

This is one of the most well known interview questions for getting a development job. It’s meant to get rid of all those who lack skills in programming, and simple logic. You require programming skills to figure out a clean way to implement it, and logic skills to figure out how to implement it.

There have been many, many different ways to implement it in all different sorts of languages over time. Let’s look at some clever, and some not-so-clever, ways of doing it.

The Simple Solution

The totally understandable and language independent method with minor code duplication.
You can do it in C.

Or you can do it in Python.

Or really any other language out there. You can make it slightly worse by nesting the if statements.

The idea is simple and works. Easy to understand and has decent performance, but this is FizzBuzz, so performance probably isn’t a concern.

The Other Simple Solution

You can sacrifice a bit of performance and you will end up with a bit shorter version of the simple solution. This solution will look a bit different depending on the language it is written in, simply by the fact that string concatenation is done differently in different languages.

In Python:

In C:

 

And in Java:

This is one of the most common solutions I see, but it does use a the slightly higher overhead of concatenating strings.

The “No Math” Solution

This isn’t a problem that requires user input. The output is fixed. Why should we bother calculating anything?

Technically only three lines of code. That’s less than the simple solution! In this day and age, with the extra disk space everywhere, this could be a decent solution, but it lacks the simple logic that FizzBuzz is supposed to show.

The “What’s a Modulo?” Solution

Watch out, if somebody has never used the modulo operator, they’re probably not the most well-versed programmer. But it is a cool experiment.

You could always reset the fizz or buzz counters inside each if/elif statement, but readability would slightly go down.

The Lookup Table with Tuples Solution

This is very dependent on the language it is used in. But it’s one of my favorite ways to solve FizzBuzz. Here it is in Python.

This may not be the best for performance, or the simplest. But, it has an elegant look to it.

The Enterprise Solution

This one seems to come up a lot in discussions about FizzBuzz. It’s a satire of FizzBuzz and enterprise software. But, it does work.

Since this solution consists of 1704 lines of code at the moment, I don’t think I’ll put this on directly on the blog. You can find the its Github page here.

How Do You Solve FizzBuzz?

There are many more interesting solutions out there. Most of them are probably way over the top, or totally wrong. Finding a new way to solve it (or fail to solve it) is something I look forward too.

If you have a unique solution, or an improvement on the generic ones, share it!

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">