Adventures In FizzBuzz

Tests for programmers in an interview process are not uncommon. For the last couple of years I have asked a quick pre-interview question to junior developers to see what sort of stuff they come up with.

As I don't want to set any developer a task that will take longer than absolutely needed I opted to set a very simple task for them. Commonly known as "FizzBuzz", this task is as follows.

"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"."

The expected output for this would be something like this.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

This task is particularly important as it shows knowledge of loops, if statements, the modulus operator and of printing output. The modulus operator is especially important as it is something that not everyone comes across this in their first week of development.

I set this task at least a week before the interview so they generally have lots of time to work on it. It should take a junior developer around 30 minutes to complete this task. I also don't stipulate what language the task should be in, so I get a variety of responses in JavaScript and PHP, although JavaScript seems quite popular so that's generally the language of choice.

I thought I would post some of the responses to this task in a post as a way of showing the varying responses I get, although I will absolutely name no names.

The first example was written starts off with a prompt to ask the user what number they should count up to. This is a nice touch and it even shows a little personality in the effort. The good thing is that this produces the correct result.

var num = prompt("Enter a number and see the result in the bottom right window :) ");
for (i = 1; i <= num; i++) {

  if (i % 15 == 0) {
    document.write("FizzBuzz");
    document.write("<br>");
  } else if (i % 3 == 0) {
    document.write("Fizz");
    document.write("<br>");
  } else if (i % 5 == 0) {
    document.write("Buzz");
    document.write("<br>");
  } else {
    document.write(i);
    document.write("<br>");
  }

}

Next is perhaps the strangest submission I've ever seen. Thankfully, it does produce the correct result, but the two DOCTYPE tags and even the closing DOCTYPE at the end really did make this stand out. When I asked the candidate about how they had but this together they said that they "didn't know". I can expect that a junior programmer wouldn't know everything, but there is something very cargo cultish about this candidates approach here. They have obviously copied and pasted much of this from somewhere.

<!DOCTYPE html>
<html>
<h2>TEST</h2>
<body>
  Javacsript FizzBuzz

  <DOCTYPE! hmtl>
  <html>
  <body>

  <h2>
  FIZZBUZZ
  </h2>

  <p id=ptag>

  </p>

  <script>
  const ptag = document.getElementById('ptag');

let i=1;

for (let i=1; i<=100; i++){

 	if (i%15 ===0){
  ptag.innerHTML += "FizzBuzz, ";}

 else if (i%3 ===0){
  ptag.innerHTML += "Fizz, ";
}
	else if (i%5 ===0){
  ptag.innerHTML += "Buzz, ";
}
else {ptag.innerHTML += i+ ', ';}

  }

  </script>

  </body>


  </html>

  </DOCTYPE!>


</body>
</html>

Perhaps the shortest submission I've ever seen is this one. It produces the correct result, although you need to go searching for it in the console output. This let to a discussion about code that works whilst also being readable. This took me a few moments to actually parse what is going on.

for(let i=0;i<100;)
  console.log(
    ( ++i%3 ? '' : 'Fizz' ) + ( i%5 ? '' : 'Buzz' ) || i
  )

This next one is very a very textbook approach to this problem, and the result produced the correct output. Perhaps a little too much so as when I asked the about how it worked in the interview they couldn't really tell me. This is an indication that they just copied the test from somewhere.

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body>
  <script>
    var i;

    for (i = 1; i <= 100; i++) {

      if (i % 3 == 0 && i % 5 == 0) {
        document.write("FizzBuzz" + "<br>");
      } else if (i % 5 == 0) {
        document.write("Buzz" + "<br>");
      } else if (i % 3 == 0) {
        document.write("Fizz" + "<br>");
      } else {
        document.write(i + "<br>");
      }
    }
  </script>

</body>

</html>

Here is an approach that creates an array and then pushes the items in the array with the correct items of text. It produces the correct result, but is a little difficult to see clearly what is going on. It also uses the method of injecting the output into the console.

let array = [];
  for (let i=1; i <=100; i++) {
    array.push(i)
  }
let newArray = array.map(num => {
  return (num % 15 === 0) ? 'FizzBuzz' : (
    (num % 3 === 0) ? 'Fizz' : (
    (num % 5 === 0) ? 'Buzz' : num))
})
console.log(newArray)

Finally, for the interview responses, we have the following, which actually produces an incorrect result. The first 15 numbers are correct, but then the process breaks down a little and adds Fizz and Buzz when there shouldn't be one. It's clear that an effort was made, but the eventual output was a little confused.

for (let i = 1; i<= 100; i++){
     if (i % 3 == 0){
      document.write("Fizz!" + "<br>")
       }
     if (i % 5 == 0){
       document.write("Buzz!" + "<br>");
       }
     if ((i % 3 == 0) && (i % 5 == 0)){
       document.write("Fizzbuzz!" + "<br>");
       }
     else{
       if (i % 3 && i % 5){
         document.write(i + "<br>");
      }
   }
}

Finally, as a bit of fun, I recently came across this example from a Kevlin Henney talk a while ago. Is it a Python script that essentially uses a random seed to generate the FizzBuzz structure. This is fascinating as you wouldn't normally expect random to be used in this way. It does work though, somehow.

import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]


Ultimately, the question is if setting this kind of pre-interview test useful? I think it has merit, although it isn't about the raw coding ability of potential candidates and it certainly doesn't preclude anyone from being interviewed. For me, the test is meant to elicit a conversation about what they have written and how they have written it. The resulting conversation about the code and how good they are at explaining themselves is the most important part of this. A good candidate should be able to clearly explain what they did to get the result they handed over. Talking about the coding task is also only a part of the interview process as there are a great many other things that go towards the hiring of a new developer.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.