19 April 2011

FizzBuzz Examples

I have noticed lately programmers and interviewers are all up in arms about the FizzBuzz programming task. Interviewers are making a deal of the fact that programmers cannot even write a basic FizzBuzz program, even recent graduates.

Where does one buy a domino shirt?
First off, this is an absurd trend and non-news in developer interviews. While the argument exists to make developers develop in an interview, often the tasks are absurd and impractical. This is yet another example and part of the reason for my recent series on interview questions.

So on to the question.
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.”
Seems simple enough right? Don't overthink this one. Let's break it down."Write a program that prints the numbers from 1 to 100." Okay so we need a loop. What kind of loop? Well you can do lots of things here, try and wow them with a recursive loop, use a do..while or a for loop. Let's use the KISS method and go with a for loop.
for (int i=1;i<=100;i++) {
cout << i << endl; }

Done deal. We're printing out the numbers from 1 to 100. Now onward."But for multiples of three print “Fizz”" Alright remember, how we can find out if something is a multple of three? Hmm? Divide it by 3? Is there a remainder? No? Well it's divisible by 3 then. duh. So use the dreaded mod here.
for (int i=1;i<=100;i++) { if (i % 3 == 0)   cout << "Fizz"<< endl;
else
  cout << i << endl;
}
Alright now I think you have this, continue on in this method and check if it's divisible by 5... for (int i=1;i<=100;i++) { if (i % 3 == 0)   cout << "Fizz"<< endl;
else if (i % 5 == 0)   cout << "Buzz"<< endl;
else
  cout << i << endl;
}
Oops! It seems that we've backed into a corner and missed the final step of requirements. For numbers which are multiples of both three and five print “FizzBuzz.”. Just by eliminitating the else if and the line breaks we should have it then, right? int main() { for (int i=1;i<=100;i++) { if (i % 3 == 0) cout << "Fizz"; if (i % 5 == 0) cout << "Buzz"; if (i % 3 != 0 && i % 5 != 0) cout << i; cout << endl; } } "But I can write this so much more elegantly!" You say. Please do. That is what will separate you from the rest of the pack.

3 comments:

studyy123 said...
This comment has been removed by the author.
studyy123 said...
This comment has been removed by the author.
studyy123 said...

I'm not sure how to make it look nice with correct spacing, but it was a fun exercise so here it is in Java. The recursive version is also included.

package com.fizzbuzz;

public class FizzBuzz
{
public static void main( String[] args )
{
fizzBuzzForLoop();

System.out.println();

fizzBuzzRecursion( 1, 100 );
}

public static void fizzBuzzForLoop()
{
for( int i = 1; i <= 100; i++ )
{
if( (i % 3) == 0 )
{
System.out.print( "Fizz" );
}
if( (i % 5) == 0 )
{
System.out.print( "Buzz" );
}
if ( (i % 3) != 0 && (i % 5) != 0 )
{
System.out.print( i );
}

System.out.print( " " );
}
}

public static void fizzBuzzRecursion(int start, int finish)
{
if( start > finish )
{
return;
}

if( (start % 3) == 0 )
{
System.out.print( "Fizz" );
}
if( (start % 5) == 0 )
{
System.out.print( "Buzz" );
}
if ( (start % 3) != 0 && (start % 5) != 0 )
{
System.out.print( start );
}

System.out.print( " " );

fizzBuzzRecursion( ++start, finish );
}
}