Good Things About FizzBuzz

Posted Wednesday, February 29th, 2012 at 6:08 pm

Over a year ago, I mentioned FizzBuzz as a basic competence screen during interviews. At the time, I said: “My only real quarrel with FizzBuzz is that, at this point, any developer worth their salt is familiar with it.” I seem to have been wrong, becuase I keep running into coders who definitely are competent, and who haven’t encountered it yet.

That’s fine, because FizzBuzz does have a few nice qualities as a test.

It’s Language-Agnostic

It makes sense for someone to code a FizzBuzz in anything from JavaScript to Haskell to C++ to Ruby to Python to assembler (pick your instruction set). The same cannot be said of various other standards, like “Reverse a string in-place” (which takes a bit of thought and care in C, and so makes a good test — but is utterly trivial in languages like Perl or PHP that have built-in string-reversal functions) or “connect to a webpage, count the frequency of words in it, and output them sorted in descending order” (which makes sense in scripting languages with well-known HTTP or cURL packages, but is far too big to make a decent whiteboard question in ones like C or C++).

It Doesn’t Require Memorization

There are some questions that require that the applicant knows a particular library call or special function. In the previous paragraph, I mentioned Perl’s reverse() and PHP’s strrev() — those aren’t what I mean, because those are pretty core functions. Those are the sort of thing anyone who does any text manipulation will have memorized, because they use it every day. But what about some of the more obscure ones, like PHP’s strncmp()? If you ask a question that requires it, you’re basically asking your applicant, “Have you heard of this really obscure function?” (In the case of languages like PHP, with its over-3,000 core-level functions, this is particularly evil, but the same results can be obtained by using a little-known CPAN package, C++ stdlib function, or Java Class Library function.)

FizzBuzz doesn’t require any weird functions to solve. It’s nothing but math, plus a for loop and an if/else. The most obscure thing in it is modulo arithmetic. And sure, maybe the applicant might have forgotten what the modulo operator is, because it’s not the sort of thing most people use every day.

But you can still solve it without the mod operator. Instead of if (foo % 3 == 0), you just do if (int(foo / 3) == foo / 3) — and that is the sort of thing that a decent coder should be able to figure out in a minute or two. (For int(), substitute floor() or Math.floor() or your language’s equivalent.)

There’s More Than One Way to Solve It

The infamous “Microsoft/Google interview brain-teaser” puzzle questions are notorious for the fact that there’s only one “right” answer — and it’s generally something that nobody could possibly figure out on their first try in an interview situation. Questions like how to figure out which light switch was on in a sealed, opaque box, or how to determine which coin weighs a different amount from a bunch of others with a minimal number of weighings — these basically boil down to, “Do you already know the trick behind this really contrived puzzle?”

With FizzBuzz, there’s no requirement that the applicant solve it one particular way. For example, my preferred logic (expressed in pseudocode) is:

if (multiple of 3 and multiple of 5)
    print "FizzBuzz"
else if (multiple of 3)
    print "Fizz"
else if (multiple of 5)
    print "Buzz"
else
    print number
end

But there’s no reason why you can’t do it this way:

if (multiple of 3)
    print "Fizz"
    if (multiple of 5)
        print "Buzz"
    end
else if (multiple of 5)
    print "Buzz"
else
    print number
end

Or even like this:

retval = ""
if (multiple of 3) retval += "Fizz"
if (multiple of 5) retval += "Buzz"
if (not multiple of 3 && not multiple of 5) retval = number
print retval

All of these will work just fine. In fact, I’ve seen people solve it each of those ways, and given them full marks for a valid solution. And if someone asks you to solve FizzBuzz, and then gives you grief because you choose a different option than they would have? Just walk out of that interview, because that person’s just turned it back into one of those “guess what number I’m thinking of” questions that it has the potential not to be in the first place!

Post a Comment

Your email is never shared. Required fields are marked *

*
*