Do you actually know how to code?
I’m currently focusing on algorithms and data structures, important subjects that are the central focus of technical tests. A technical test is a significant hurdle programmers are often required to clear before they land a coveted tech role. Algorithms and data structures are pretty much the sole content of these tech exams.
Today I’d like to discuss one of the earliest algorithms I encountered at the beginning of my tech journey, FizzBuzz. According to Wikipedia, FizzBuzz originated as a children’s word game that taught them division. Players take turns counting replacing numbers divisible by three with Fizz, numbers divisible by five with Buzz, and numbers divisible by three and five with FizzBuzz.
Imran Ghory is credited with converting this children’s game into a technical challenge to determine if programming job candidates could actually code.
The FizzBuzz task as presented by Imran himself:
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”.
When solving algorithms, it helps to write pseudocode as you work through the problem. It’s also a good idea to talk out loud while you’re practicing because when you are being interviewed, you’re expected to explain your thought process to your interviewer(s).
So first, to clarify the challenge I’d write and ask the following:
Print numbers 1 to 100
(Include the numbers 1 and 100 in the count?)
Multiples of 3 = Fizz
Multiples of 5 = Buzz
Multiples of 3 & 5 = FizzBuzz
This pseudocode breaks the problem down in a way that you can start to see how it will look in code. For example, to determine multiples of a number, our code will look something like this:
10 % 5 === 0
This statement is true because 10 is divisible by 2 and has a remainder of 0.
10% 3 === 0
This would be false because 10 divided by 3 would have a remainder which is not 0.
A function named fizzbuzz is declared and contains a for loop with a starting number (num) set to 1. So the count will include one and count up by 1 using num++ to go through each loop. To ensure the number 100 is included, I set the count to continue as long as it’s less than 101. Now I’ll use an if statement inside of the for loop to get the specified output. If I wrote this function in the order the task presented, I would set it up to print numbers first, then check for multiples of 3, then check for multiples of 5, and finally check for multiples of 3 & 5. NOTE I’m testing it on a smaller number (less than 16 instead of 101)for demonstration purposes. So the function would look like this:
My function is written on the left and I called it at the bottom left with fizzbuzz( ) and the result on the right is a count of all numbers (no Fizz, Buzz, nor FizzBuzz) starting with 1 at the top all the way down to 15. So we see right away that this function failed because Fizz should have replaced number 3, Buzz should have replaced number 5, etc. This indicates that my actual code does not necessarily follow the order of my pseudocode. I have to use logic to rearrange things to ensure the correct out put.
So it looks like we’ll have to put in some checks before we print out our numbers to ensure we catch the number cases that will trigger Fizz, Buzz, & FizzBuzz. So our initial refactor could be to place the console.log for num at the end of the if statement to ensure all the other checks happen first:
Okay, it looks a lot closer to what we’re after. We see numbers 1 & 2 listed since neither is divisible by 3 nor 5. And we have Fizz for 3 because it’s divisible by 3, then 4 is listed since it’s not divisible by 3 nor 5, and Buzz for 5 because it’s divisible by 5 and so on. Everything looks in order until we get to the final Fizz which is substituted for the number 15. This is a problem because 15 is divisible by 3 & 5. So this should be FizzBuzz and not just Fizz.
We need to do another refactor. We need to check for FizzBuzz numbers before we check for Fizz and before we check for Buzz:
We did it! FizzBuzz is printed for 15. Now we put the 101 limit back in our count so it will go all the way up to and include 100:
My screen isn’t large enough to show the full resulting count from 1–100 but looking at the end of the count, we see that the 100 place is included and it’s substituted with Buzz because 100 is divisible by 5. We also see numbers 75 and 90 are represented by FizzBuzz because they’re both divisible by 3 & 5. So the function is working properly. We’ve conquered FizzBuzz!
We passed Imran’s screening test; we can actually code. That means now we have a shot at landing a coveted tech role!