Why Interviewers Still ask ‘FizzBuzz’ in 2022

This problem has been around for such a long time. Anyone who has been writing code for even a little while has probably come across it. It’s commonly asked as an introductory whiteboard question during interviews. It’s so easy that if you fail it, your interviewer will probably scratch their head wondering why you’ve been invited to interview in the first place.

Everyone that can actually program will be able to solve it. Those that fail this simple problem will most likely not be receiving a job offer. The point of FizzBuzz isn’t to find a great programmer, it’s to filter out candidates by asking an extremely easy question.

However, FizzBuzz is so popular, it’s probably not a good question to ask in the current year. Most beginners have probably read about the problem and know how to solve it. Nonetheless, it is still asked. Other such questions may be asked as well, which you should be able to solve them, if you can solve FizzBuzz.

Let’s explore the problem and take a look at the solution.

A look at FizzBuzz

Directions
  • Write a function that prints the numbers from 1 to n.
  • For multiples of 3, print fizz instead of the number.
  • For multiples of 5, print buzz instead of the number.
  • For numbers which are multiples of both 3 and 5, print fizzbuzz.
Example
fizzBuzz(5);
// 1
// 2
// fizz
// 4
// buzz
Solution

The solution is straightforward. It’s probably the solution every one comes up with after reading the instructions.

function fizzBuzz(n) {
  for (let i = 1; i <= n; i++) {
    // if the number is a multiple of 3 and 5
    if (i % 3 === 0 && i % 5 === 0) {
      console.log("fizzbuzz");
    }

    // else if the number is a multiple of 3
    else if (i % 3 === 0) {
      console.log("fizz");
    }

    // else if the number is a multiple of 5
    else if (i % 5 === 0) {
      console.log("buzz");
    }

    // else, log the number
    else {
      console.log(i);
    }
  }
}

We’re going to use a loop to run through the input, checking if the numbers are multiples of 3 and 5 with the help of the remainder operator. If they are, log fizzbuzz to the console. Else if a number is a multiple of 3, log fizz to the console. Else if a number is a multiple of 5, log buzz. Else, if none of the conditions are met, log the number to the console.

Conclusion

That’s it, folks. The classic FizzBuzz problem solved with JavaScript. Feel free to check out our other articles (e.g. counting vowels, counting steps, checking for palindromes). Have you been asked this question before? Let us know in the comments below.

Happy Coding!

comments powered by Disqus

Related Posts

The Art of Data Visualization: Exploring D3.js

Data is everywhere, flowing into our applications from various sources at an unprecedented rate. However, raw data alone holds little value unless it can be transformed into meaningful insights.

Read more

JavaScript’s Secret Weapon: Supercharge Your Web Apps with Web Workers

During an interview, I was asked how we could make JavaScript multi-threaded. I was stumped, and admitted I didn’t know… JavaScript is a single-threaded language.

Read more

Creating a NodeJS Budgeting Tool

In this article, we’re going to show you how to read and write to a .txt file using NodeJS by creating a small budgeting CLI (Command Line Interface).

Read more