by GhotiFish · 13y ago · 0 comments
There are a few programmers that have a proclivity towards "clean, perfect" solutions, but FizzBuzz is ugly.
Making FizzBuzz work at all is just stupidly simple; but the moment you ask yourself about the little details, you notice that you either have to make an if tree, a bunch of if-elseif's, or some other hinky thing that ruins the "purity" of this simple little program.
Alot of people take the perspective that if you go
if(i % 15) print "fizzbuzz";
else if(i % 5) print "fizz"; //or was it buzz?
else if(i % 3) print "buzz"; //or was it fizz?
else print i;
then you've found an optimal solution and you can wash your hands of it, until you think to yourself: "but an if tree would use less comparisons! Should I be readable? Or take the efficient route? But if I'm thinking about efficiency. wouldn't a lookup be faster and simpler?"I suspect when presenting a problem like this, the interviewer filters out two kinds of people: the people who "can't code", and the people who are obsessive.
You might not want obsessive people either, in which case fizzbuzz is clearly a good choice.
Someone who is obsessive wouldn't like their off the cuff solutions to FizzBuzz. They would think of a solution and stop themselves from writing anything at all.
How would you tell the difference between someone who doesn't want to write their first three solutions, to someone who can't think of even one?
Microsoft's "implement strcmp without the use of libraries" doesn't have this property. strcmp has an easy and clean solution.
FizzBuzz doesn't filter anyone, the people using it do. It is not difficult to explain the purpose of the exercise, and that easily resolves the problem for people looking for "clean" solutions.