Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

fizzbuzz

Name: Anonymous 2009-12-04 0:39

On occasion you meet a developer who seems like a solid programmer. They know their theory, they know their language. They can have a reasonable conversation about programming. But once it comes down to actually producing code they just don’t seem to be able to do it well.

You would probably think they’re a good developer if you’ld never seen them code. This is why you have to ask people to write code for you if you really want to see how good they are. It doesn’t matter if their CV looks great or they talk a great talk. If they can’t write code well you probably don’t want them on your team.

After a fair bit of trial and error I’ve come to discover that people who struggle to code don’t just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

So I set out to develop questions that can identify this kind of developer and came up with a class of questions I call “FizzBuzz Questions” named after a game children often play (or are made to play) in schools in the UK.

In this game a group of children sit around in a group and say each number in sequence, except if the number is a multiple of three (in which case they say “Fizz”) or five (when they say “Buzz”). If a number is a multiple of both three and five they have to say “Fizz-Buzz”.

An example of a Fizz-Buzz question is the following:

[quote]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”.[/quote]

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.

Want to know something scary ? – the majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I’m not saying these people can’t write good code, but to do so they’ll take a lot longer to ship it. And in a business environment that’s exactly what you don’t want.

This sort of question won’t identify great programmers, but it will identify the weak ones. And that’s definitely a step in the right direction.

Name: Anonymous 2010-02-23 1:30

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

        int isNum =0;

        for (int i =0; i <=100; ++i) {
        isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
            switch (isNum) {
                case 3: System.out.println("Fizz: "+ i); isNum = 0; break;
                case 5: System.out.println("Buzz: "+ i); isNum = 0; break;
                case 8: System.out.println("FizzBuzz"+ i); isNum = 0; break;
                default: System.out.println(i); isNum = 0; break;
            }
        }
    }
}

Name: Anonymous 2010-02-23 1:34

#! /bin/sh

cycle='fizz $n buzz fizz $n $n fizz buzz $n fizz $n $n fizzbuzz $n $n'

echo 1
echo 2
set -- `seq 3 100`
while : ; do
  for i in $cycle ; do
    test $# -gt 0 || exit
    n=$1 ; shift
    eval echo $i
  done
done

Name: Anonymous 2010-02-23 1:35

(progn
  (format t "~{~A~^~%~}"
          (loop for i from 3 to 100
                collect (case (mod i 15)
                          ((3 6 9 12) "fizz")
                          ((5 10) "buzz")
                          (0 "fizzbuzz")
                          (t i))))
  (values))

Name: Anonymous 2010-02-23 1:37

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

        int isNum;

        for (int i =0; i <=100; ++i) {
        isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
            switch (isNum) {
                case 3: System.out.println("Fizz: "+ i); break;
                case 5: System.out.println("Buzz: "+ i); break;
                case 8: System.out.println("FizzBuzz"+ i); break;
                default: System.out.println(i); break;
            }
        }
    }
}

Name: Anonymous 2010-02-23 1:38

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

        for (int i =0; i <=100; ++i) {
        int isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
            switch (isNum) {
                case 3: System.out.println("Fizz: "+ i); break;
                case 5: System.out.println("Buzz: "+ i); break;
                case 8: System.out.println("FizzBuzz"+ i); break;
                default: System.out.println(i); break;
            }
        }
    }
}

Name: Anonymous 2010-02-23 1:42

--- >>208-fag   2010-02-23 07:40:53.809772132 +0100
+++ >>212-fag   2010-02-23 07:41:15.676762541 +0100
@@ -1,15 +1,13 @@
 class FizzBuzz {
     public static void main(String[] args){

-        int isNum =0;
-
         for (int i =0; i <=100; ++i) {
-        isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
+        int isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
             switch (isNum) {
-                case 3: System.out.println("Fizz: "+ i); isNum = 0; break;
-                case 5: System.out.println("Buzz: "+ i); isNum = 0; break;
-                case 8: System.out.println("FizzBuzz"+ i); isNum = 0; break;
-                default: System.out.println(i); isNum = 0; break;
+                case 3: System.out.println("Fizz: "+ i); break;
+                case 5: System.out.println("Buzz: "+ i); break;
+                case 8: System.out.println("FizzBuzz"+ i); break;
+                default: System.out.println(i); break;
             }
         }
     }

Name: Anonymous 2010-02-23 1:55

>>213
repeated dynamic memory allocation over single static allocation on stack.
Nice improvement, bro.

Name: Anonymous 2010-02-23 2:10

>>214
jvm does escape analysis and also no dangling pointers.

Name: Anonymous 2010-02-23 2:17

class FizzBuzz {
   public static void main(String[] args) {
      for (int i = 0, isNum = 0; i <= 100; ++i) {
         isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
         switch (isNum) {
            case 3: System.out.println("Fizz: "+ i); break;
            case 5: System.out.println("Buzz: "+ i); break;
            case 8: System.out.println("FizzBuzz: "+ i); break;
            default: System.out.println(i); break;
         }
      }
   }
}

Sure is really Java in here.  Can someone program this in a language not yet used in the thread?

Name: Anonymous 2010-02-23 2:29

definitely too much coffee-fags around...

Name: Anonymous 2010-02-23 2:30

Just one more to drive the point home.
class FizzBuzz {
   public static void main(String[] args) {
      for (int i = 0; i <= 100; ++i) {
         System.out.println( (i%3==0 ? "Fizz" : "") + (i%5==0 ? "Buzz" : "") + " " + i);
      }
   }
}

Name: Anonymous 2010-02-23 2:46

>>218
for multiples of three print “Fizz” ...
>instead of the number

class FizzBuzz {
   public static void main(String[] args) {
      for (int i = 0, isNum = 0; i <= 100; ++i) {
         isNum = 0 + (i%3==0 ? 3 : 0) + (i%5==0 ? 5 : 0);
         switch (isNum) {
            case 3: System.out.println("Fizz"); break;
            case 5: System.out.println("Buzz"); break;
            case 8: System.out.println("FizzBuzz"); break;
            default: System.out.println(i); break;
         }
      }
   }
}

Name: Anonymous 2010-02-23 4:05


#lang scheme

(define (fizzbuzz x)
  (cond ((and (= 0 (modulo x 3)) (= 0 (modulo x 5))) (display "FizzBuzz"))
        ((= 0 (modulo x 3)) (display "Fizz"))
        ((= 0 (modulo x 5)) (display "Buzz"))
        (else (display x)))
  (newline)
  (if (< x 100)
      (fizzbuzz (+ x 1))
      (values)))

(fizzbuzz 1)

Name: Anonymous 2010-02-23 4:09

HAI
CAN HAS STDIO?
I HAS A VAR IZ 0
IM IN YR LOOP
    UPZ VAR!!1
    IZ VAR BIGR THAN 100?
        GTFO.
    KTHX
    IZ VAR LEFTOVAR 15 LIEK 0?
        VISIBLE "FIZZBUZZ"
    KTHX
    ORLY?
    IZ VAR LEFTOVAR 5 LIEK 0?
        VISIBLE "BUZZ"
    KTHX
    ORLY?
    IZ VAR LEFTOVAR 3 LIEK 0?
        VISIBLE "FIZZ"
    KTHX
    NOWAI
        VISIBLE VAR
    KTHX
KTHX
KTHXBYE

Name: Anonymous 2010-02-23 10:37

>>219
Ah, sorry.  I based it on the repetitions of >>203 onwards where that was a feature for whatever reason.

Name: Anonymous 2010-02-23 12:25

>>220
modulo
remainder

Name: Anonymous 2011-05-22 12:24

Never gets old.

Name: Anonymous 2011-05-22 12:27

>>224
IN-CORRECT.

I like how the default language for syntax parsing is Visual Basic. That's pretty upfucked.

Name: Anonymous 2011-05-22 13:12

So this was all a test to see what type of programmer you are?

Anyways, I did it in ~2 minutes 15 seconds ..


<script type="text/javascript">
var lineBreak = "<br />";
for(var i = 0; i <= 100; i += 1)
  if(i <= 0)
    document.write(i + lineBreak);
  else if(i % 3 === 0 && i % 5 === 0)
    document.write("Fizz-Buzz" + lineBreak);
  else if(i % 3 === 0)
    document.write("Fizz" + lineBreak);
  else if(i % 5 === 0)
    document.write("Buzz" + lineBreak);
  else
    document.write(i + lineBreak);
</script>

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List