Not sure if I got the job yet.
The top story on HackerNews last night was a blog post by Patrick McKenzie called Don’t Call Yourself A Programmer, And Other Career Advice. Buried inside the article was a reference to another blog post by Jeff Atwood, lamenting the poor coding skills of many applicants for programming jobs. Jeff post links to a small programming test called FizzBuzz which was created by Imran Ghory to identify candidates with practical coding skills from those with weak coding skills.
It reminded me a a job interview from my past where there was a technical test which was relatively simple except for a single programming task which was rather tricky… I cobbled together an answer which was a mess and the interviewing team explained that no one had successfully answered the question. It seemed that most of the test revolved around basic knowledge with one lone question there to identify programming savants. The thing I like about FizzBuzz is that it models rudimentary programming which constitutes the majority of a programming day-job (for the web at least). Here’s my ad-hoc (verbose) attempt under test conditions:
<?php
/**
* Implement FizzBuzz
*
* Rules: 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".
*
*/
// Main programmy thingy
for ($i=1; $i < 101; $i++) {
//check if $i is a multiple of 3 and five;
$three = _fizzbuzz_multipleofthree($i);
$five = _fizzbuzz_multipleoffive($i);
// print the number or fizz or buzz or fizzbuzz
if ($three && $five) {
echo "fizzbuzz\n";
} elseif ($three) {
echo "fizz\n";
} elseif ($five) {
echo "buzz\n";
} else {
echo "$i\n";
}
}
/*
* Utility functions
*/
function _fizzbuzz_multipleofthree($n) {
// is $n a multiple of 3
// i.e. if you divide it by 3 is there anything remaining
$r = ($n % 3);
if ($r == 0) {
return true;
} else {
return false;
}
}
function _fizzbuzz_multipleoffive($n) {
// is $n a multiple of 5
// i.e. if you divide it by 5 is there anything remaining
$r = ($n % 5);
if ($r == 0) {
return true;
} else {
return false;
}
}
Update: I thought I was being explicit and verbose with my utility functions… turns out it’s just bad style. Here’s my cleaned up FizzBuzz.

2 Comments
Having it test the modulus in separate methods is bad style. Especially because what those methods do can be done in one line. Ex. fizzbuzz_multipleoffive can be written as:
return ($n % 5 == 0)
hey, thanks for the feedback… I guess I was trying to be verbose. I’ve cleaned up the original and reposted: https://earthviaradio.wordpress.com/2011/10/30/fizzbuzz-refactor-with-new-improved-style/
One Trackback
[…] I’ve cleaned up my first FizzBuzz script after receiving some advice. […]