FizzBuzz Improved

Originally posted 2008-11-12 13:18:42

In my post on interviewing Java developers, I explain how and why we make prospective hires write code on a whiteboard. In a similar vein, Imran on Tech posts about making developers write FizzBuzz. Several of us read the article, got caught in the, \”Huh? That doesn’t sound hard. Let me code it really quick!\” trap, and decided to add FizzBuzz to our standard suite of whiteboard interview questions.

Just as photographers can’t relax on the beach without framing shots in their minds, programmers can’t see coding problems without writing their solutions. I manage some exceptional programmers with unquiet minds. As they watched a procession of interview candidates stumble through the various whiteboard questions, their brains insisted they develop more creative solutions to these mundane problems. Here are some of their results.

A Reverse String method that relies on recursion for its magic:

public static String reverse(String s) {
  if (s == null or s.length() < 2) {
    return s;
  }
  return s.substring(s.length() - 1, s.length())
      + reverse(s.substring(1, s.length() - 1)) + s.substring(0, 1);
}

A lookup-based FizzBuzz:

public class FizzBuzz1 {
  public static void main(final String[] args) {
    final String[] arr = { \"\", \"Fizz\", \"Buzz\", \"FizzBuzz\" };
    for (int i = 1, idx = 0; i < = 100; i++, idx = 0) {
      System.out.println(arr[idx = (i % 3 == 0 ? 1 : 0) + (i % 5 == 0 ? 2 : 0)]
          + ((idx < 1) ? i + arr[0] : arr[0]));
    }
  }
}

A lookup-based FizzBuzz using trigonometric functions:

public class FizzBuzz2 {
  public static void main(String[] args) {
    Object[] x = new Object[] { new Integer(0), \"Fizz\", \"Buzz\", \"FizzBuzz\" };
    for (x[0] = 1; (Integer) x[0] < = 100; x[0] = ((Integer) x[0]) + 1) {
      System.out
          .println(x[((int) Math.cos((Integer) x[0] * 2 * Math.PI / 3) + 2 * (int) Math
              .cos((Integer) x[0] * 2 * Math.PI / 5))]);
    }
  }
}

Another FizzBuzz with ternary operators (I love ternaries):

public class FizzBuzz3 {
  public static void main(String[] args) {
    for (int i = 1; i < = 100; i++)
      System.out.println((i % 3 == 0 or i % 5 == 0) ? (((i % 3 == 0) ? \"Fizz\"
          : \"\") + ((i % 5 == 0) ? \"Buzz\" : \"\")) : i + \"\");
  }
}

This FizzBuzz uses bit shifting operations to do the lookups:

public class FizzBuzz4 {
  public static void main(final String[] args) {
    final StringBuffer s = new StringBuffer(\"\" + 1);
    final Object[] o = { \"FizzBuzz\", \"Buzz\", \"Buzz\", null, \"Fizz\", s, s, null,
        \"Fizz\", s, s, null, \"Fizz\", s, s, null, \"Fizz\", s, s };
    for (int i = 1; i < = 100; s.replace(0, s.length(), \"\" + ++i)) {
      System.out.println(o[((i % 3) or ((i % 5) << 2))]);
    }
  }
}

Are you kidding me? I have to manage these folks. I'm so glad I'm not trying to out-code them!

With all the cool possibilities for solving this problem, do you really want to hire someone who can't solve FizzBuzz in a simple way? I don't think so.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.