white puzzle pieces
Shares

Coding Challenge: FizzBuzz

Last updated: Jul 7, 2024

The "FizzBuzz" coding challenge is one of the most basic code tests you will face when interviewing.

Coding challenges are not only a fact of life for technical interviews and assessments, they are also fun! Mastering the process of solving coding challenges will give you the confidence to go into any technical interview or assessment and succeed. And we all know confidence is half the battle when interviewing. In this series, I am tackling various coding challenges and explaining my process along the way. For this challenge, I am using Javascript since that is my primary focus these days.

The Challenge: FizzBuzz

The FizzBuzz challenge is one of the easiest ones you will face. So easy in fact, that you may not even see it in the wild except as a practice question. But it is a good warm up to the tougher challenges that you will have to tackle.

Print all numbers from 1 to 100. If the number is divisible by 3, print "Fizz". If it is divisible by 5, print "Buzz". If it is divisible by both 3 and 5, print "FizzBuzz". Otherwise, print the number itself.

Time Management

Time management is an important aspect of interviews and assessments and should be taken into consideration right from the start. It is better to have completely coded correct answers to all the challenges than to have one perfect answer and the rest unfinished. For that reason, I usually start with the "brute force" method. Just get a solution down. Worry about efficiency and elegance later. And yes, Big O\mathcal{O} considerations come after we get through the brute force phase.

Brute force

Let's just brute force this puppy:

function fizzBuzz() {
  for (let i = 1; i <= 100; i++) {
    let output = '';
    if (i % 3 === 0) {
      output = 'Fizz';
    }
    if (i % 5 === 0) {
      output += 'Buzz';
    }
    console.log(output ? output : i);
  }
}

fizzBuzz();

Simple enough. Not much to explain there.

Refactor

Our brute force version has a Big O\mathcal{O} of O(1)\mathcal{O}(1). Not bad at all. But Big O\mathcal{O} isn't the only factor. We can improve on the execution a time a bit by using if/else blocks instead.

function fizzBuzz() {
  for (let i = 1; i <= 100; i++) {
    let output = '';
    if (i % 3 === 0 && i % 5 === 0) {
      output = 'FizzBuzz';
    } else if (i % 3 === 0) {
      output = 'Fizz';
    } else if (i % 5 === 0) {
      output = 'Buzz';
    } else {
      output = i.toString();
    }
    console.log(output);
  }
}

fizzBuzz();

Now we are bailing on the logic checks as soon as we have a match, great. And we are still at O(1)\mathcal{O}(1). I could have also changed:

if (i % 3 === 0 && i % 5 === 0) {

to

if (i % 15 === 0) {

but there is a limit to how much I am willing to refactor on a coding challenge. Remember, time management, and it took an extra minute for me to think through whether division by 15 was the same as 3 and 5. Hint, it is.

I don't really see the need for any further improvement on this one, it is a basic challenge, so let's call that good and submit.

Conclusion

This was a relatively easy coding challenge, but like all challenges, it gets us thinking about what we are writing and how we can improve upon our results. I find it is a great way to learn new techniques or approaches to a problem. I hope you do as well.

See Also