Two examples of test programs asked during interviews

I include these examples purely for instructional purposes. Generally the subject is given 10 minutes to complete each task.

Please note that if you are ever interviewing with me and you use these for your code examples, I’ll know.

The first problem involves coding the algorythm called "FizzBuzz".

For the numbers from 1 to 100:

  • If the number is a multiple of 3, output "Fizz".
  • If it is a multiple of 5, output "Buzz".
  • If it is a multiple of both, output "FizzBuzz".
  • Otherwise, output the number.

I offer the following as a good example of how to do this. What I find astounding, is that about 40% of so-called programmers cannot execute this task.

My example not only solves the request, but allows for enhancement capability.

/** * Computes the fizzbuzz for the specified range and returns the text * * @param $max how many terms to return * @param $tests an array of additional values to check * @return a string of comma separated terms */ function fizzBuzz($max=100, $tests=array()) { // nomalize maximum value $max = $max + 0; if ($max < 1) $max = 100; // set up test values $defaultTests = array('Fizz' => 3, 'Buzz' => 5); $tests = array_merge($defaultTests, $tests); $results = ''; for ($i = 1; $i <= $max; $i++) { $result = ''; foreach ($tests as $label => $modulo) if ($i % $modulo == 0) $result .= $label; if (!$result) $result = $i; $results .= $result; if ($i == $max) { $results .= "\n"; } else { $results .= ', '; } } return $results."\n"; } echo '
FizzBuzz: an exercise in silly logic

'; echo fizzBuzz(123, array('Baz' => 47));


This example includes the additional test for multiples of 47


FizzBuzz: an exercise in silly logic

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, Baz, 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, Baz, Buzz, Fizz, 97, 98, Fizz, Buzz, 101, Fizz, 103, 104, FizzBuzz, 106, 107, Fizz, 109, Buzz, Fizz, 112, 113, Fizz, Buzz, 116, Fizz, 118, 119, FizzBuzz, 121, 122, Fizz


Before we move on, one more example.

while($n++<100)echo$n%3?!$$n=$n:Fizz,$n%5?$$n:Buzz,"\n";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


Although this code does solve the problem, if anyone ever submits such an example in an interview, I'll walk them out the door.


The second problem involves coding the algorythm for the Fibonacci sequence.

Starting with 1 and 1 as the first two terms, each succesive term is defined as the sum of the previous two.

While this example goes way beyond the original request, it does show how someone could generalize the problem to allow for other sequences of a similar type.

/** * Determines a sequence based on the sum of the previous n terms * * @param $terms how many terms to return * @param $prior the number of terms to sum * @param $seed the seed value for the first terms * @return a string of comma separated terms */ function sumSequence($terms=50, $prior=2, $seed=1) { // seed the algorithim with $prior terms of $seed $sequence = array($seed); $results = $sequence[0]; for ($p = 1; $p < $prior; $p++) { $sequence[$p] = $sequence[0]; $results .= ', '.$sequence[$p]; } for ($i = $prior; $i < $terms; $i++) { // next term is sum of the previous n prior elements $sequence[$prior] = $sequence[$prior-1]; for ($p = 2; $p <= $prior; $p++) $sequence[$prior] += $sequence[$prior-$p]; // toss oldest term array_shift($sequence); // output the next term $results .= ', '.$sequence[$prior-1]; } return $results."\n"; } function fibonacci($terms) { return sumSequence($terms); } echo '
Fibonacci: an exercise in recursive logic

'; echo fibonacci(20);
Fibonacci: an exercise in recursive logic

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765