I know, I know. I'm just one more in the litany of pundits offering their opinion on the FizzBuzz test that Jeff Atwood described in his
Feb/07 blog post, among many others. It intrigued me to see what the big deal was and how panicked I might get if I was issued the challenge in an interview.
For the uninitiated, the FizzBuzz test requires a programmer candidate to write a simple program that outputs the number 1 to 100 with a twist: Numbers that are a multiple of three are replaced with the word "fizz"; those that are a multiple of five are replaced with "buzz". Those that are a multiple of three and five are replaced with "fizzbuzz".
It's a simple enough problem that makes your fingers itch to write the loop and get to it. I must admit that the solution I came up with was not my first - I arrived at the code below after thinking of a refactoring. All in all, it took me under 10 minutes, which I think would be ok under Atwood's guidelines:
public void RunFizzBuzz()
{
for (int n = 1; n <= 100; n++)
{
Console.Write((n % 3 == 0 && n % 5 == 0) ? "fizzbuzz" :
(n % 3 == 0) ? "fizz" : (n % 5 == 0) ? "buzz" : n.ToString());
Console.Write("\n");
}
}
Originally, I approached the solution from the wrong way 'round, starting with checking for a multiple of 3, then 5. Checking for the multiples of 3 and 5 first made for a much more streamlined solution.
Right then! I am eminently qualified for any programming position...