Big Brain Time: FizzBuzz

Johnson Kow
3 min readSep 25, 2020
Photo by Garett King (@shortstache)

This is the first algorithm problem I was presented right after finishing a coding boot camp. The ‘FizzBuzz’ challenge and it’s pretty straightforward. Let’s take a look at the problem.

Problem

Write a short program that prints each number from 1 to 100. For each multiple of 3, print ‘Fizz’ instead of a number & for each multiple of 5, print ‘Buzz’. For numbers that are multiples of both 3 and 5, print ‘FizzBuzz’.

The way in which someone solves this problem goes into their character as a programer. Some programmers create code that fix one particular problem, like me, but other programmers create code that work for any future changes.

Solution 1: The Quick Fix

Intuitively, if you’re even a beginner at programing, you know you can makes use of a for loop such that you complete a process n number of times. I’ll go ahead and type out the way most people, including myself, would solve this problem.

The Quick Fix

This is for programmers that code for just one particular problem and there’s nothing inherently bad with it, especially if you’re interviewing with someone. It would be best to create code that offers a solution.

The Cons

Now, what happens when now your employer would like to add conditionals such that for numbers divisible by 7 print “Dizz”, and numbers divisible by 9 print “Wuzz”. Or maybe let’s print “Buzz” on multiples of 8 instead of 5. With code this small, either task does not seem so daunting but when you’re working on bigger programs, it would be a MAJOR pain to add new conditionals to it.

Solution 2: The Big Brain Fix

Let’s go over a solution that essentially does the same thing but is not as static as the code above.

Big Brain Solution

This is much better. In the case that we would like to instead print “Buzz” for 7 instead of 5, we only have to change it in one line. In the case that we want more conditionals, we would just repeat the lines of code and that way the output is changing in a much more dynamic way. Look at how much better this looks when adding more conditionals down below.

Adding conditionals

This way you don’t have to write specific if-statements for whether a number is a multiple of just 3 or 3 & 5 or 3 & 5 & 7. If you did it that way, you’d have to create multiple conditionals for each and every possible combination of divisors and nobody has time for that!

One thing that I noticed that I need to practice is coding for future changes. First step is getting the code to work. Secondly, start thinking about how to make this work for multiple conditions or changes in variables. As always, happy coding!

--

--

Johnson Kow

Software Engineer based out of NYC. Learning more about programming everyday 👍