Fizz Buzz October 1st, 2007

It's frustrating to spend hours with a prospective hire only realize he couldn't code to save his life. The only thing worse is finding out after you've hired him.

It's not their fault. Studies show incompetent people don't know they're incompetent. It's up to you to save everyone's time. Use a short test to screen out the fools.

An example is "Fizz Buzz." The instructions are dead simple. Write a program that:

  1. Prints the number 1 to 100
  2. When it reaches a number divisible by 3, print "fizz"
  3. When it reaches a number divisible by 5, print "buzz"
  4. When it reaches a number divisible by both, print "fizz buzz"

Too many candidates can't do this. Even if they can, you can tell how bad they are by the implementation.

Here's a submission ported from Visual Basic to Ruby. It's ported because Ruby is a great common language, and to avoid legal trouble from posting a candidate's submission.

1.upto(100) do |i|
  fizz = false
  buzz = false
  if i % 3 == 0
    fizz = true
  end
  if i % 5 == 0
    buzz = true
  end
  if fizz == false and buzz == false
    puts i
  elsif fizz == true and buzz == false
    puts "#{i} fizz"
  elsif fizz == false and buzz == true
    puts "#{i} buzz"
  elsif fizz == true and buzz == true
    puts "#{i} fizz buzz"
  end
end

Of course he failed before his first line of code. If you're interviewing for a Ruby position, and you're allowed to pick any language, don't pick Visual Basic.

How would I write it?

1.upto(100) do |i|
  output = [i]
  output << 'fizz' if i % 3 == 0
  output << 'buzz' if i % 5 == 0
  puts output.join(' ')
end