FizzBuzz — A Simple Algorithm Question Yet Interesting

Sam Chen
4 min readDec 11, 2020

For anyone who has studied and practiced algorithm problems, you have probably seen the FizzBuzz problem. But what is FizzBuzz and where did it come from? FizzBuzz is a traditional children’s game, it is played between two people where they each take turn to count from one to one hundred, and at number 3 and multiplication of 3, they say Fizz; at number 5 and multiplication of 5, they say Buzz; at multiplication of 3 and 5 they say FizzBuzz. While it is a very simple counting question, yet when we implement it into programming to solve this question, there are so many different approaches to it. Today we will take a look at one of the approach and my perspective on this problem.

Let us take a look at my first approach to solve FizzBuzz. Demonstration from code snippet above is using JavaScript / Node.js. Notice the results on the right side shows the intended sequence of numbers from 1, 2, Fizz, 4, Buzz, and the first FizzBuzz is shown on 15th number. We start by using a for loop and let the number start at 1, and run the codes inside the loop repeatedly until it reaches 100. Now what do we want to do inside the loop is to show case four different situations, first printing out the number itself; second if the number is 3 or multiplication of 3, print Fizz; if the number is 5 or multiplication of 5, print Buzz; if the number is multiplication of 3 and 5, print FizzBuzz. When we first approach the problem, we might be thinking let’s start by printing out the number, then when it gets to 3 and we will print out Fizz, and so on. But if we take a look at the code snippet above, we realize that it isn’t the case. We first have to tackle the FizzBuzz part, then Fizz, Buzz at last we are printing out the number itself. Why is it that way you might ask? It is simply because we have to think ahead before we start solving the problem, by listing out the possible outcomes, we have a clue of what to put first. In this problem, we have to tackle the FizzBuzz first because if we don’t do that, at #15 it won’t print out FizzBuzz, instead it will print out whichever possible outcome we tackle down first, whether it is Fizz or Buzz. One other thing we notice is that we are trying to eliminate all other possible outcomes before we simply output the actual number. I think of this method as process of elimination, where we eliminate all the other possible outcomes, and everything else is simply printing out the number itself.

If we look at the original question, the last line says: write the solution so it has as few characters as possible. A lot of times as programmers we want to minimize our codes (because we are lazy), and if we are repeating our codes a lot, it means we can refactor it to a simpler and better code. Taking a look at the code snippet above, first thing we notice is that the results printed out on the right side is correct, which means we didn’t break our solution with new codes. Looking on the left side, we notice that there are only 8lines of code, which is better than our previous solution. Not only that, if we want to change Buzz to 5 and multiplication of 5, it is simply a change of number on line 4. With this solution, not only we are minimizing the lines of codes that we have to write, if we decide to change the problem from 5 to 7, we are eliminating some of the errors of forgetting to change our code from 5 to 7.

In conclusion, there are a lot of ways to solve a simple FizzBuzz problem, but the approach and the perspective to this problem is what matters! Thanks for reading and have a wonderful day!

--

--

Sam Chen

Yesterday I was clever, so I wanted to change the world. Today I am wise, so I am changing myself.