Fizz-Buzz Test
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment 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"?.
Why Fizz-Buzz is "hard:"
We can't understand why so many people "fail" the Fizz-Buzz test unless we understand why it is "hard" (for them). Understanding that, we may be able to evaluate the usefulness of this tool, and others, as filtering tools for candidates. Fizz-Buzz is "hard" for some programmers because:
- it doesn't fit into any of the patterns that were given to them in school assignments,
- it isn't possible to directly and simply represent the necessary tests, without duplication, in just about any commonly-used modern programming language.
To further illuminate both point 1 and 2:
- that it doesn't match the patterns they memorized from lectures and class assignments: I think this makes it a good discriminator, because I wish to hire candidates who can think for themselves -- not those who are limited to copying solutions from others.
- that it's hard to directly code it.
Fizz-Buzz does not fall into the common pattern of:
if 1 then A
else if 2 then B
else if 3 then C
else/otherwise D
(Well-- it could, but not when you consider "1,2 & 3" to be atomic tests, like "is divisible by 3.")
So I'm going to try to do this first in PHP,
I will print a table out of the resultant code below the code blocks.
//quick server-side php code...
$fizz = "fizz";
$buzz = "buzz";
for ( $x=1; $x<=100; $x++){
switch($x){
case ($x % 3 == 0 && $x % 5 == 0):
echo $fizz.$buzz;
break;
case ($x % 3 == 0):
echo $fizz;
break;
case ($x % 5 == 0):
echo $buzz;
break;
default:
echo $x;
break;
}
}
FizzBuzz in Javascript
So, here is another example of some quick code client-side in javascript.
//I mimic'd the PHP code closely here, of course, you can do more with this...
var fizz = "fizz";
var buzz = "buzz";
for (i=1;i<=100;i++){
switch (i){
case (i % 3 == 0 && i % 5 == 0):
document. write( fizz + buzz );
break;
case (i % 3 == 0):
document. write( fizz );
break;
case (i % 5 == 0):
document. write( buzz );
break;
default :
document. write( i );
}
}
Code output Results:
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"?
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz
Final Thoughts
Well, that is it. I really enjoyed going through this with you. Please drop me a line if you benefitted from the information given. Stay curious!