FizzBuzz Examples

FizzBuzz is a very popular “test” used when interviewing programmers. It’s mostly used to filter out people who can’t actually program. Now you may wonder why such a test would be necessary, after all, why would somebody who can’t program apply for a programming job? The unfortunate truth is, many people believe they can program (Some CompSci graduates for example), when in reality they may have a strong theoretical knowledge but no practical experience. There are also people who think it’s easy and they can pick it up as they go (I’ve had a couple of these people come in when I was interviewing programmers to replace me an an old job).

Here is how FizzBuzz works. The interviewer asks the interviewee to write a program in a language of their choice (Or a specific language depending on the job, maybe even¬†pseudo-code). The program should count from 1 to X, X being a random number such as 20 or 100. For each multiple of 3, the program should output “Fizz” instead of the number. For each multiple of 5, the program should output “Buzz” instead of the number. For each multiple of 3 AND 5 the program should output “FizzBuzz” instead of the number. It’s a very trivial task for any competent programmer, and the output should look like this:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

How do you generate such output? Well here is a simple method written in PHP:

<?php
if ( PHP_SAPI != 'cli' ) {
    echo '<pre>';
}

for ( $i = 1; $i <= 20; $i++ ) {
    if ( ( $i % 3 ) == 0 && ( $i % 5 ) == 0 ) {
        echo 'FizzBuzz';
    } else if ( ( $i % 5 ) == 0 ) {
        echo 'Buzz';
    } else if ( ( $i % 3 ) == 0 ) {
        echo 'Fizz';
    } else {
        echo $i;
    } 

    echo PHP_EOL;
}

The first part of our code may look weird, but it’s basically checking if the script is being run from a web browser or command line. If it’s being run from the browser, we output a <pre> tag to preserve our newlines. This way the program looks the same regardless of how it’s run. It’s unnecessary¬†but is unlikely to hurt your chances by including it.

The next part of the code is creating a loop, with a variable that starts at 1 and increments each iteration of the loop until it reaches 20.

Inside of our loop, we are using the modulus operator (%) to determine if $i is evenly divisible by 3 AND 5, just 5 or just 3 (Or neither). The modulus operator returns the remainder in a division operation. For example, 7 % 3 = 1, and 10 % 5 = 0.

The last part echoes out a newline after each line using PHP_EOL. This is a configuration variable that contains the specific newline used by the OS (For example, Linux uses \n for newlines, Windows uses \r\n).

Now that I’ve showed you the easiest way of doing this, I thought I’d share the most complete method.

<?php
function FizzBuzz( $iterations = 20 )
{
    for ( $i = 1; $i <= $iterations; $i++ ) {
        if ( ( $i % 3 ) == 0 && ( $i % 5 ) == 0 ) {
            echo 'FizzBuzz';
        } else if ( ( $i % 5 ) == 0 ) {
            echo 'Buzz';
        } else if ( ( $i % 3 ) == 0 ) {
            echo 'Fizz';
        } else {
            echo $i;
        } 

        echo PHP_EOL;
    }
}

if ( PHP_SAPI == 'cli' ) {
    $iterations = 20;

    if ( isset( $_SERVER['argc'] ) && $_SERVER['argc'] > 1 && intval( $_SERVER['argv'][1] ) > 0 ) {
        $iterations = intval( $_SERVER['argv'][1] );
    }

    FizzBuzz( $iterations );
} else {
    echo '<pre>';

    $iterations = 20;

    if ( isset( $_REQUEST['iterations'] ) && intval( $_REQUEST['iterations'] ) > 0 ) {
        $iterations = intval( $_REQUEST['iterations'] );
    }

    FizzBuzz( $iterations );
}

Now obviously this is overkill for an interview, but what does it do exactly? It creates a function that you can re-use elsewhere, it supports the browser and the command line, and it lets you pass the number of iterations via a browser parameter (http://localhost/fizzbuzz.php?iterations=100 for example), or via the command line (php ./fizzbuzz.php 100 for example).

2 Replies to “FizzBuzz Examples”

  1. Why not just have:

    $print= true;
    if ( ( $i % 3 ) == 0 ) {
                echo 'Fizz'; $print = false;
            }
    if ( ( $i % 5 ) == 0 ) {
                echo 'Buzz'; $print= false;
      }
    if ($print) 
    echo $i;
    
    1. That’s definitely an alternative, and there is no reason not to do it that way. The way I did it was just inline with my coding style though.

Leave a Reply