FizzBuzz–A Programming Question

Projects or exercises that lend themselves to many different “correct” solutions are just about the best sorts of learning experiences I can imagine. They really open the possibilities for discussion and from discussion – questions, answers, explanations – comes real learning. Recently Dennis Shaw, a student at Old Dominion University and a Microsoft Student Partner, told me about the FizzBizz exercise. (see Using FizzBuzz to Find Developers who Grok Coding for where it comes from) This is exactly that sort of multiple solution but not that difficult sort of exercise I love. Briefly stated the exercise is:

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

So there you have it. It involves using some sort of conditional of course. Nested or unnested? It can be done either way. Which way is best? Do you check for multiples of three first or five? Do you check to see if the number ISN’T one of those cases first or if it IS one of those cases? Or rather than If statements is there a different way completely? Perhaps a select/case structure? Does creating functions/methods complicate of simplify the result? If students know multiple programming languages is there one that is better for this than another (think especially about functional languages v. procedural languages). Are there special operators (like perhaps the ternary operator in C#, C/C++ and Java) that might make the code “tighter?” Is there a trade off between complication and understanding? Oh and of course, does your solution actually work?

Justify your answers. (Don’t you just just love the teacher-ness of “justify your answer?” ) I can see asking students to write up an explanation of how their solution works and why they did it that way as a means for forcing examination of the solution. And as a side benefit a lot of students can benefit from yet another writing exercise. Code that can’t be explained isn’t properly understood and that causes problems in the long run. A piece of code should not be “magic” to the person who wrote it.

There are many solutions on the Internet to the FizzBuzz question. So yes, some student might go searching for one and even try to use it. The “justify your solution” question means that they still have to understand and explain how it works and why they like it. I can’t see a student saying “I like this solution because I found it on the Internet and it works.” Well, I hope they wouldn’t try that. But if they do, grade them on how well they explain the benefits of using borrowed code and if they credited the original source properly. And of course they should also be able to explain why this solution is better than others they might have found. Surely they were not so lazy as to hand in the first solution they found?

I would say that it goes without saying that unless specified otherwise, if you're given requirements in integers, then the expected results is also specified in integers.

The ability to understand natural language is import to being a good programmer. If for some reason there was confusion (such as a requirement that states "measure all objects between 1 foot and 5 feet in length and store them in this database" - which would cause confusion over what to do with partial measurements) then one would ask questions in order to understand the requirements more fully.

I've had the unfortunate experience of working with contractors that took the approach of pointing out inane trivialities with language - such as "but there are an infinite number of numbers between X and Y" and I will not hire them back. Such people are not only unproductive, but they excel at creating rabbit holes and then falling in them.

That's not to say if someone was legitimately confused over the requirements that I wouldn't answer clarification questions, but these people were not confused. They simply have the unfortunate habit of trying to look important or highly educated, or something. I'm never quite sure. But they come across as difficult to work with at best. Jerks at worst.

It probably goes without saying, but if I gave this question in interview and a candidate said "there are an infinite number of numbers between 1 and 100" I wouldn't hire them. That would be an immediate disqualifier. If on the other hand, they said "integers only?" then I'd confirm integers only and wouldn't think any less of them.

Mary Attenborough

1 Mar 2011 3:34 AM

Ability to see what is missing in a specification can be an important quality. Interestingly the formation of this problem has a missing requirement. The first sentence should read "Write a program that prints the numbers from 1 to 100 in order". The words 'in order' are missing.

Scott P

5 Mar 2011 6:33 PM

// Sans strong-typing...

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

var multipleOf3 = (i % 3) == 0;

var multipleOf5 = (i % 5) == 0;

var t = multipleOf3 ?

( multipleOf5 ? "fizzbuzz" : "fizz")

: ( multipleOf5 ? "buzz" : i);

print( t);

}

/* Advantages:

* Evaluate i % 3 and i % 5 only once for each i

* It's kind-of readable if you are ok with ternaries

* The coercion of i to a string is here left to the language, but inexpensive algorithms for this are a commodity

* To implement with stronger typing, this coercion would need to be explicitly performed or delegated