jump to navigation

Using FizzBuzz to Find Developers who Grok Coding January 24, 2007

Posted by tickletux in Software development, job interviews.
trackback

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:

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”.

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.

Comments»

1. Monkeyget - January 24, 2007

I have asked a fizzbuzz question recently. The position was for a .NET developer.
I asked a candidate who claimed to have “one year of experience in .NET” to create the simplest thing I could think of: an application that takes two number and displays the sum of them.
It took five minuts and a bit of my help just to create a new project, as much time to create the visual aspect of the window (drag and drop 2 textbox and a button!) and in the end it didn’t even compiled.

Actually I don’t think asking a “Fizz-Buzz” question is a good thing and I wouldn’t recommend on doing it. I only did it because I knew the candidate wasn’t worth it and wanted to demonstrate it to the boss which had no technical knowledge and therefore couldn’t understand that not knowing what an interface is and not being able to describe what a class and an object are, is a big no-no.
As you said, you still don’t know if he can write good code. You only know that he/she can write the most basic code but you lost some precious time for the interview. Is it worth it? Maybe asking to design and write something more complicate but not that complicate would be better.

Every company worth a damn and caring about who they are hiring : http://weblog.raganwald.com/2006/07/hiring-juggler_02.html

2. Lionel Barret - January 24, 2007

Indeed, such an abysmal level of coding skill is simply unbelievable…
I am speechless…

Frankly, i have never seen candidate so unskilled, but in France, comp.sci is really math heavy and I suppose that’s a good filter.

But still…

3. Jacob - January 24, 2007

Want another good filter? This surprised me, but turns out to be a good indication of whether someone has actually ever programmed before.

Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.

If they don’t *start* with creating a third variable, you can pretty much write that person off. I’ve found that I can cut a third to half my (admittedly at that point unscreened) applicants with that question alone.

4. Mitch Silverman - January 24, 2007

I once worked at a company that did a lot of geometric computations. Our standard interview question (to be done as homework between interview 1 and interview 2) was write a function in c that would “Chord” a circle to an arbitrary level of precision. Inputs were radius and tolerance. Output was the number of sections needed to break up the circle.
In the second interview we would review the code and talk about error catching, what happens when radius is less than distol.. etc.
But the trick was when we asked for a function to do the same thing with an ellipse. We didnt want an answer - we were looking for the response
Was the candidate excited? Thoughtful? or did (s)he get that deer in the taillights look in his/her eyes?

5. Ben Prew - January 24, 2007

Jacob,

There are several languages that you can do this in that don’t require a temporary variable. I know Perl supports this, and I believe Ruby does as well:

ex.

($a, $b) = ($b, $a)

I’m not sure about Python or other dynamic languages, but I believe they do.

Finally, you can do it in C as well:

void XORSwap(void *x, void *y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

I’m assuming that you’re eliminating candidates who actually can’t get swap() correct. But, in several cases, not starting by creating a temporary variable is actually a good thing :)

6. Alexandre Vassalotti - January 25, 2007

“Want to know something scary ? - the majority of comp sci graduates can’t.”

That is indeed scary. I have a hard time to believe it. Maybe, the candidates were thinking it was a trick question or something like that. I don’t know.

I took me less than 1 min (I timed myself), to write the correct solution and never taken any course. Now, I’m wandering what they teach in Computer Science courses.

# Fizz-Buzz in Python
for i in range(1, 100):
if (i % 3 == 0) and (i % 5 == 0):
print “FizzBuzz”
elif i % 3 == 0:
print “Fizz”
elif i % 5 == 0:
print “Buzz”
else:
print i

# Swap in Python
a = 0
b = 1
b, a = a, b

Personally, I would ask them, as a weeder question, to write a function that sums up integers from a text file, one int per line.

7. Vineet Kumar - January 25, 2007

I was also going to respond to Jacob’s swap question with the python unpacking example, but Ben and Alexandre beat me to it. Though Ben, in your C example, you can’t dereference a void pointer. I wouldn’t say that really falls in the FizzBuzz category, but it would be a minus point for the interviewee.

8. Jos'h - January 25, 2007

“”"
fizzbuzz.py
josh 2007-01-25
Print integers 1 to 100. Sub ‘Fizz’ for multiples of 3, ‘Buzz’ for mult of 5.
Sub ‘FizzBuzz’ for mults of both. Written for the purpose of being silly.
“”"

threes = range(0, 101, 3)
fives = range(0, 101, 5)

def check(n):
. plain = 1
. if n in threes: print “Fizz”, ; plain = 0
. if n in fives: print “Buzz”,; plain = 0
. if plain: print n,
. print

[check(i) for i in range(1, 101)]

9. Geordan’s Musings » Fizz Buzz - January 25, 2007

[...] came across this post on programming.reddit.com regarding using a very simple exercise to weed out completely inept [...]

10. Mohammed Zainal - January 25, 2007

good post,
i know little programming , learnt by trial & error , internet and books.
i do complicate things while writing my code , prolly because im not really good with math , but yet & the end i’ll find the solution .

candidates some time gets tension during the interview and that will effect the results .

p.s nice blog buddy :)

11. mult.ifario.us : Laziness and fizzbuzz in Haskell - January 25, 2007

[...] peeking at Reg’s solution, I can’t resist posting a fizzbuzz implementation in Haskell (because it looks stylistically like it should be written in an FP [...]

12. flickrhoneys - January 25, 2007

I used to grok coding, but I’ve grown up since then :D

13. Miauw - January 25, 2007

“Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.” (Jacob)

How do you do that in functional languages with single assignment? :)

14. Stephen. - January 25, 2007

Ech, state variables stink. Here’s a better python solution

for i in range(1,100):
fizz = (i % 3 == 0) and “Fizz” or “”
buzz = (i % 5 == 0) and “Buzz” or “”
print “%2i %s%s” % (i,fizz,buzz)

15. john cromartie - January 25, 2007

Fizzbuzz in a nasty Ruby one-liner:

puts (1..100).to_a.collect { |i| i % 15 == 0 ? ‘fizzbuzz’ : (i % 5 == 0 ? ‘fizz’ : (i % 3 == 0 ? ‘buzz’ : i)) }.join(’ ‘)

16. john cromartie - January 25, 2007

“How do you do that in functional languages with single assignment? ” (Miauw)

You wouldn’t have variables to begin with… unless it was a monad, in which case you would just do it the normal way.

17. Joshua - January 25, 2007

Java:
public static void main(String[] args)
{
for (int i = 1; i

18. Jacob - January 25, 2007

Ben,

I’d take anything that works. If I were interviewing for a C or Perl position, your examples would be fine. The key is to pose the question and observe them tackling it. *How* they tackle it is as important as whether they come up with the “right” answer. My example of swapping the values in two variables is a pretty trivial question, so how long it takes and their approach to doing it is the information I’m parsing, not whether they end up in the right place in the end.

19. sibecker - January 25, 2007

Here it is in functional programming (OCaml):
“How do you do that in functional languages with single assignment? ” (Miauw)

OCaml one liner:

let fb x = (if x mod 3 = 0 then “Fizz” else “”) ^ (if x mod 5 = 0 then “Buzz” else “”) in let fb2 x = let f = fb x in if f = “” then string_of_int x else f in let rec fb_to x = let g = fb2 x in if x = 1 then g else (fb_to (x-1)) ^ “, ” ^ g in fb_to 100;;

20. WC - January 25, 2007

def fizzbuzz():
for n in range(1, 101):
str = “”
if not n % 3:
str += “Fizz”
if not n % 5:
str += “Buzz”
print n, str

21. anon for this one - January 25, 2007

Ruby FizzBuzz in 81 bytes. (please let my next employer ask this in an interview… please)

f,b=’fizz’,'buzz’
puts((1..100).to_a.map{|i|i%15==0?f+b:(i%5==0?b:(i%3==0?f:i))})

22. Cale Gibbard - January 25, 2007

Er, what do you mean “the majority of comp sci graduates can’t”? Do you have any data to support that?

At least where I went to uni, not just comp sci graduates, but everyone in first year mathematics had to learn to solve harder programming problems than that. By the end of second year, everyone in comp sci, and a large portion of those in any program in the mathematics faculty will have written a simple compiler. So I don’t see how this problem would be of any difficulty.

I can’t say anything about how typical my university was (in fact, I suspect it’s quite atypical, and I was there for a pure mathematics degree anyway). So sure, this is just one datapoint, but I’m curious about where your conclusions are coming from.

23. goblinfactory - January 25, 2007

How about overloading the assignment operator in C#?

24. Dr Jekyll & Mr Hyde » Blog Archive » FizzBuzz and bored programmers - January 25, 2007

[...] his blog, tickletux advocates the use of FizzBuzz to find developers who grok coding. However, this kind of test may also cause difficulties. What do you do if a candidate answers with [...]

25. azman - January 26, 2007

you’re right about comp sc student didnt rite!
even my friend with master degree in comp science cant even rite “hello world” program!!..hahahah..
nice blog

26. Andrew - January 26, 2007

;; scheme example… though, i’m not happy

(define (fizz-buzz rng)
(let loop ((n 1))
(let ((thr (remainder n 3))
(fiv (remainder n 5)))
(cond
((and (zero? thr) (zero? fiv)) (printf “fizz-buzz~n”))
((zero? thr) (printf “fizz~n”))
((zero? fiv) (printf “buzz~n”))
(else (printf “~a~n” n)))
(if (= rng n)
n
(loop (add1 n))))))

(fizz-buzz 100)

27. Brian Mitchell - January 26, 2007

If you’re going to golf it you might as well check if things like the repetition of values actually helps. Here is something short in ruby.

puts (1..100).map{|i|i%15==0?’FizzBuzz’:i%5==0?’Buzz’:i%3==0?’Fizz’:i}

I’ll also punish anyone on my team who writes a set of nested ternary expressions like that. It’s nice to know those things for golfing games though. ;-)

28. macnod - January 26, 2007

Common Lisp:
(loop for a from 1 to 100 collect
(let ((c (zerop (mod a 3))) (d (zerop (mod a 5))))
(cond ((and c d) “FizzBuzz”)(c “Fizz”)(d “Buzz”)(t a))))

29. Randy - January 26, 2007

Why do people always insist on writing one-liners?

Yes, you may get your rocks off by writing something a litty crazy, but the guy who has to maintain your code, is going to be hating you. Just say no to one-liners. And say yes, to clear, consistent coding.

30. Brian Mitchell - January 26, 2007

Randy, I agree but in this case it looks like it was so easy to solve that people went a little farther and started a game of golf [0].

[0] The original golfing language was perl but it has now become popular way for bored programmers to play with their skills in many different languages.

31. khudari - January 26, 2007

Dave Kaemmer came up with a similar test at Papyrus that I have been using for years.

This kind of test is too simple to measure programming skill, but it is a great predictor of who will be very productive and who will not. We found it much better than a person’s resume, for example. So my technique is to administer the test via email (with a 20 minute time limit) to every applicant after a very basic resume screen. This saves a lot of valuable engineering staff time doing interviews. Our only problem: it is really hard to find candidates we want to interview.

32. George - January 26, 2007

WTF is this on the Daly WTF

33. Jeff Staddon - January 26, 2007

Maybe it’s just economics? With all the technology buzz and high salaries there’s a lot of motivation to jump on board. Especially for a young person who doesn’t really know their strengths/weaknesses. Univ I’m afraid are more interested in selling students on taking classes than on helping these kids find themselves.

All the good developers I’ve known never really choose technology—they were drawn to it as if it were some alien tractor beam. Back in my university years I tried to escape myself–I majored in history–but I couldn’t help but write Perl scripts on the side because it was so much fun! (BTW–anyone else out there majored in history??)

34. Brendan - January 26, 2007

Apologies to Mr. Anon with his 81 bytes, but I did it in ruby in 71.

puts (1..100).map{|i|(s=(i%3==0?’Fizz’:”)+(i%5==0?’Buzz’:”))==”?i:s}

35. Eric - January 26, 2007

Re: No One Liners

I personally enjoy a challenge as a developer and one liners often provide just that. While it is probably not as helpful to folks maintaining code because it can be confusing, I would rather hire someone who will figure it out and learn something then someone who simply brushes things off as being unclear.

I am not saying folks should write everything on one line, be reducing line counts is a good way to learn details of the language along with different ways of implementing algorithms.

36. Brendan - January 26, 2007

I would certainly not put anything like the above in production code.

However, someone who golfs successfully is someone who also knows the syntax precisely - they know when they can go without parentheses, spaces and the like.

Of course, if someone wrote something like the above in a live interview as a first pass, I don’t know whether I’d be more or less inclined to hire them. After all, I sure don’t think well like that.

37. Reg Braithwaite - January 26, 2007

I think it’s important to avoid overthinking the question and the answer. All you want to do is eliminate the extreme time wasters taht have no clue whatsoever.

The key is that someone solves it in fifteen minutes or less. If they want to Golf with it, or show off the fact that they just read a book about Higher Order Functions, I say don’t mark them down but don’t mark them up either.

Just invite them in for the interview. That’s the place where you can find out f they’re too clever by half, or perhaps they were bored, or are really straightforward but thought they needed to show you something special just to get the job.

http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html

38. Alex - January 27, 2007

Sorry, Ben Prew, but your C solution shouldn’t even compile - I’m afraid you missed the cut for working for tickletux! :)

> void XORSwap(void *x, void *y)
> … dereferencing “void *” …

39. lev radin - January 27, 2007

It seems to me weird that in blog based on PHP nobody offered way how to swap variable in PHP. Here you go:
list($a,$b) = array($b,$a);

40. Philip Munce - January 27, 2007

Not a bad test. Although I sometimes wonder if a dev team needs a couple of guys who can’t code. Reason being is that unless you work for a startup most dev work is repeatative and simple like HTML wording changes. Just the thing for someone who doesn’t know how to go to google and type “how to use modulus arithmetic in “.

41. hairmare - January 27, 2007

crappy code, but written in the time constraints. i actually get the shivers when i see courses like programming HTML on comp sci curriculas. there are also some unis where the industry links are getting to strong and thus hindering real innovation on the merits of making money and protecting IP in new inventions.

42. hairmare - January 27, 2007

$i=1; while ($i

43. Steve Biko - January 27, 2007

Jacob said: “Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.

If they don’t *start* with creating a third variable, you can pretty much write that person off.”

B.S. You can swap the values without a third variable using XOR:

var1 = var1 XOR var2
var2 = var2 XOR var1
var1 = var1 XOR var2

44. Reg Braithwaite - January 28, 2007

Swapping two variables:

The ‘XOR’ thingie is an old C/C++ ‘Aha!’ I’ve never met anyone who figured it out from base principles, everyone seems to have been shown it by someone else.

Nevertheless, it often comes up in C++ interviews and that may not be a bad thing: if you’re a C++ programmer and you’ve never heard of this trick, you probably don’t spend a lot of time actually hanging around other C++ programmers and trading tips (I’m talking to you Mr. “Visual C++ in 21 Days”).

All that being said, the given trick obviously only works in languages where XOR applies to the values given. This will not work with arbitrary Java objects, for example. Although the references might be CPU words, the language won’t let you XOR them or even cast the references to a primitive long.

45. Joshua Volz - January 28, 2007

IMO, it’s not the answer that matters, but the explanation of the answer that they give. If they write something like:

for i in range(1,101):
s = ”
if i % 3 == 0: s = “Fizz”
if i % 5 == 0: s += “Buzz”
if i % 3 != 0 and i % 5 != 0: print i
else: print s

But then told me, “yes, I know I am retesting i in the third clause, but I think it might make the code more understandable,” I would be okay with their answer, especially if they went on to do it in a single liner. I would also accept something like:

for i in range(1,101):
if i % 15 == 0: print “FizzBuzz”
elif i % 3 == 0: print “Fizz”
elif i % 5 == 0: print “Buzz”
else: print i

This at least shows that they realize that if something is divisible by 3 and 5, then it is also divisible by 15 (not a mind-bending fact, but at least they are changing the problem to be expressed without using “not”).

The follow-up question from the interviewer is what is important. If they give either of the previous two solutions (or something similar), and then you ask them to optimize for length of code, or execution time (aka “don’t retest”) and they understand what you want, then you are learning something about them. That’s the real goal of the interview.

The deeper question is what does it say about CS education that this is a reasonable test to give someone to find out if they know what they are doing. Anyone who can’t answer this, even under the stress of an interview, isn’t really a programmer. How could you trust someone with hundreds of thousands of lines of code, and the design of classes (usually) when they can’t do this?

Another fun solution, although convoluted:

for x in range(1,101):
a,b=x%3==0,x%5==0
print “%s%s%s”%(a and “Fizz” or ”, \
b and “Buzz” or ”, not a and not b and x or ”)

## I put the ‘\’ in there so that it wouldn’t wrap in the space alloted.

46. Bart Gottschalk - January 29, 2007

Here is an HTML/JavaScript implementation. I do these exercises for fun of them and because I’ve been “promoted” in my career to the point where I don’t get to write much code on the job. Doing exercises like these keeps the mind fresh and allows me to remember what it’s like to write code. Probably took about 5 minutes but that’s because I never try to remember syntax. I just look it up when I need it.

FizzBuzz

//

47. Bart Gottschalk - January 29, 2007

Oops. Let me try posting my JavaScript solution again:

var output = “”;

for( var i = 1; i

48. Tomas - January 29, 2007

Java code snippet:

for(int i=1;i0?toPrint:i);
}

Indeed, we would need to create a whole java class with a main method just to run this.
what a nasty language.

49. Tomas - January 29, 2007

Java code snippet (again):

for(int i=1;i<=100;i++) {
String toPrint=(i%3==0)?”Fizz”:”";
toPrint+=(i%5==0)?”Buzz”:”";
System.out.println(toPrint.size()>0?toPrint:i);
}

Indeed, we would need to create a whole java class with a main method just to run this.

(sorry the duplication. < and > are guilty)

50. Leonardo Herrera - January 30, 2007

Heh, can’t resist.

for(1..100){$t="";$t="Fizz"if!($_%3);$t.="Buzz"if!($_%5);$t||=$_;print"$t\n";}

51. Leonardo Herrera - January 30, 2007

…well, that was a 78 bytes solution that it is now buried under some weird CSS property. Alas.

52. StoneCypher - February 1, 2007

Jacob: most experienced coders use the XOR swap trick instead of allocating a new variable when swapping two machine primitives; it’s generally slightly faster and can be done in registers on the smallest of embedded machines.

If you see someone starting by allocating a third variable, you shouldn’t be hiring them.

53. George - February 2, 2007

Fizzbuzz in forth

: fizz? ( s — f ) 3 mod 0= ;
: buzz? ( s — f ) 5 mod 0= ;
: num? ( s — ) dup fizz? swap buzz? or 0= ;
: ?fizz ( s — ) fizz? if .” Fizz” then ;
: ?buzz ( s — ) buzz? if .” Buzz” then ;
: ?num ( s — ) num? if . then ;
: fizzbuzz ( s — ) dup ?fizz dup ?buzz dup ?num cr ;
: fizzbuzzes ( s — ) cr 1+ 1 do i fizzbuzz loop ;
100 fizzbuzzes
bye

it is fast well factored and with dumb logic no repeated values without return values it did not take two seconds because i was just learning to think forth, but this will

54. George - February 2, 2007

Also at the http://www.jwdt.com/~paysan/why-forth.html it says what discribes a bad programer

A few people expressed doubt if they are bad programmers, or if they shouldn’t try Forth because they might be part of what I considder “bad programmers”, so here’s a small test if you are a bad programmer. The more of the following answers you answer with “yes”, the worse you are. Don’t feel bad if you have a few “yes”, nobody’s perfect. I added also a few questions where a “no” should be quite sure:

* When you program, you wince at least once every minute, and not just because your tool crashed or displayed other bugs, but because you stare a the blank screen and don’t know what to enter.
* Extra points if you avoid this by pretenting to do “design” before coding, and you just produce tons of documents and UML diagrams without ever touching the keyboard and leaving the dirty work of implementing your designs to other people.
* When you have a problem, you invite for a meeting. The more people you invite here, the more points (and pointy hairs) you get.
* You think that a team of 10 programmers can solve tasks 10 times as complex or in one tenth of the time. Bonus points if you think 100 programmers scale as linear. Point dropped if you believe that because a set of 10 programmers contains one which is capable to outperform the average programmer by a factor of 10. Tell me how you prevent the others to mess up the job of the elite one.
* You always ask you what’s so funny about these “Dilbert” strips your coworkers put on their cubicle walls. Extra points if you follow the advices in “Dogberts top secret management handbook” when managing the dolts below you.
* You do programming for money; doing it as a hobby (for fun!) is something that only madmen do.
* You believe that programming can’t be self-taught, and titles are worth more than experience.
* You believe that your self-taught skills are better than anything you can learn at a university. Remove that point here if you believe that because you are the archcancellor of an elite university, and therefore know what you are talking about.
* You can’t grasp how this free software, or as you call it, “Lie-nuts” system came around, other than being either all lies, or created by nuts. Bonus points if you believe both.
* Your programs tend to become an unmaintainable mess of spaghetti code after 1000 lines (add bonus points if you reach that state earlier, reduce if you reach it later, “master of bloatware points” if your programs exceed 100klocs and are still maintainable). Extra points if your programs grow wild even after they have become unmaintainable.
* You didn’t know what to do with these rectangular Lego bricks when you were a child, and rather played with prefabricated toys.
* You think that one programming language (and maybe assembler) is enough to be a competent programmer. Bonus points if you believe that knowing a programming language isn’t necessary at all to be a competent programmer.
* You still believe that if you think hard enough you can produce correct non-trivial code without testing, though testing in the past always proved you wrong.

55. Patrick - February 5, 2007

Who says there is no ternary in python?

print “\n”.join([str(not (i%3 or i%5) and "FizzBuzz" or not i%3 and "Fizz" or not i%5 and "Buzz" or i) for i in range(1, 101)])

;-)

56. Ralph Miner - February 6, 2007

How disappointing that not one of the solutions fro Fizz Buzz started with writing acceptance tests.

57. What Do Programmers Do? » Continuous Learning - February 12, 2007

[...] Imran says many programmers can’t write code: [...]

58. Tragomaskhalos - February 13, 2007

The FizzBuzz program is a lovely little test, and I’ve now used it a couple of times myself in interviews. One guy got a “7/10″ from the business folks interviewing him, had 2+ years of Java on his CV, but couldn’t do this challenge at all - I mean he took a few minutes to even get out the loop opening, and that was a weird mishmash of while and for syntax.
I’d hate interviewers to dismiss this test as being too easy - in my experience it is genuinely astonishing how many candidates are incapable of the simplest programming tasks.

59. Cameron Kerr - February 24, 2007

Remove the leading ‘. ‘ on each line to use. Haskell is indent-sensitive

. module Main
. where
.
. main = do
. print $ map fizzbuzz [1..100]
.
. fizzbuzz x | x `mod` 15 == 0 = “FizzBuzz”
. | x `mod` 5 == 0 = “Buzz”
. | x `mod` 3 == 0 = “Fizz”
. | otherwise = show x

60. Cameron Kerr - February 24, 2007

And it still munges the text. Check my blog for what it should look like.

61. Paul - February 24, 2007

It’s a neat little test. I submitted it as a golfing challenge here:

http://golf.shinh.org/p.rb?FizzBuzz

Best solution so far is 50B of Perl. (Unfortunately, solutions are not viewable — or even saved.)

62. Coding Horror - February 27, 2007

Why Can’t Programmers.. Program?

I was incredulous when I read this observation from Reginald Braithwaite: Like me, the author is having trouble with the fact that 199 out of 200 applicants for every programming job cant write code at all. I repeat: they…

63. Syarzhuk - February 27, 2007

QBasic is easier :)

10 DATA “”, “”, “Fizz”, “”, “Buzz”, “Fizz”, “”, “”, “Fizz”, “Buzz”, “”, “Fizz”, “”, “”, “FizzBuzz”
20 DIM A$(15)
30 FOR I = 1 TO 15
40 READ A$(I)
50 NEXT I
60 FOR I = 1 TO 100
70 j = I MOD 15
80 IF A$(j) “” THEN PRINT A$(j) ELSE PRINT I
90 NEXT I

64. Cryptnotic - February 27, 2007

How about this one (just for fun):

int main()
{
int i;
for (i=1; i

65. Cryptnotic - February 27, 2007

Weak. Stupid less than signs.

int main()
{
int i;
for (i=1; i<=100; i++) {
switch(i % 15) {
case 0: printf(”fizzbuzz\n”); break;
case 3:
case 6:
case 9:
case 12: printf(”fizz\n”); break;
case 5:
case 10: printf(”buzz\n”); break;
default: printf(”%d\n”, i);
}
}
}

66. AlxShr - February 27, 2007

PHP::

for ($i=1;$

67. AlxShr - February 27, 2007

for ($i=1;$<101;$i++) {
if ($i%3==0)
echo “fuzz”;
if ($i%5==0)
echo “buzz”;
if ($i%3 && $i%5)
echo $i;
}

68. Patrik - February 27, 2007

.NET console application written in C#:

for (int i = 1; i

69. Patrik - February 27, 2007

Hmm. There might be something wrong with posting comments with the “less than” character. Anyway, here is the .NET console application solution written in C#:

for (int i = 1; i <= 100; i++)
{
Console.WriteLine((i % 3 == 0 && i % 5 == 0) ? “FizzBuzz” : ((i % 3 == 0) ? “Fizz” : (i % 5 == 0) ? “Buzz” : i.ToString()));
}

70. Brent Ashley - February 27, 2007

fizzbuzz.sh:

echo “1\n2\nfizz\n4\nbuzz\nfizz\n7\n8\nfizz\nbuzz\n11\nfizz\n13\n14\nfizzbuzz\n16\n17\nfizz\n19\nbuzz\nfizz\n22\n23\nfizz\nbuzz\n26\nfizz\n28\n29\nfizzbuzz\n31\n32\nfizz\n34\nbuzz\nfizz\n37\n38\nfizz\nbuzz\n41\nfizz\n43\n44\nfizzbuzz\n46\n47\nfizz\n49\nbuzz\nfizz\n52\n53\nfizz\nbuzz\n56\nfizz\n58\n59\nfizzbuzz\n61\n62\nfizz\n64\nbuzz\nfizz\n67\n68\nfizz\nbuzz\n71\nfizz\n73\n74\nfizzbuzz\n76\n77\nfizz\n79\nbuzz\nfizz\n82\n83\nfizz\nbuzz\n86\nfizz\n88\n89\nfizzbuzz\n91\n92\nfizz\n94\nbuzz\nfizz\n97\n98\nfizz\nbuzz\n”

71. lowly programmer - February 27, 2007

int
main(int argc,char *argv[])
{
int ii;

for(ii=0;ii++

72. A.D. - February 27, 2007

In T-SQL:

DECLARE @i int
SET @i = 1
WHILE (@i

73. Michael Letterle - February 27, 2007

I took a slightly different approach then Patrik, but similar:

for(int x=1; x

74. Michael Letterle - February 27, 2007

I took a slightly different approach then Patrik, but similar:

for(int x=1; x <= 100; ++x) { Console.WriteLine((x % 3 == 0 ? “Fizz” : “”) + (x % 5 == 0 ? “Buzz” : “”) + ((x % 3 != 0 && x % 5 != 0) ? x.ToString() : “”) ); }

75. Casual Observer - February 27, 2007

Another observation:

If the code is only three lines long, but is still hard to read, that’s also a bad sign.

If someone else wants to read your code, the simple version is better unless you have a very good reason (such as speed) for using peculiar shortcuts. Developer time is usually more important unless you have found a bottleneck.

Verbose code >> terse code 99% of the time. Some of you are showing off. Sorry, wouldn’t want you to work on my team.

76. matt - February 27, 2007

C’mon, everyone, you’re forgetting the master of all programming languages: Visual Basic for Applications (Excel in this case)! :)

Sub fizzbuzz()
For i = 1 To 100
If i Mod 3 = 0 Then
If i Mod 5 = 0 Then
Range(”A1″).Offset(i - 1, 0).Value = “fizzbuzz”
Else
Range(”A1″).Offset(i - 1, 0).Value = “fizz”
End If
Else
If i Mod 5 = 0 Then
Range(”A1″).Offset(i - 1, 0).Value = “buzz”
Else
Range(”A1″).Offset(i - 1, 0).Value = i
End If
End If
Next i
End Sub

77. Scott Hanselman's Computer Zen - February 27, 2007

You Can’t Teach Height - Measuring Programmer Competence via FizzBuzz

78. medbob - February 27, 2007

Took about 3 - 5 minutes because I couldn’t remember if the operator was DIV or MOD….

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;
var i : Integer;
output : String;
begin
{ TODO -oUser -cConsole Main : Insert code here }
for I := 1 to 100 do
begin
Output := ”;
//Writeln(i,i mod 3);
if (i mod 3 = 0) then
Output := ‘Fizz’;
if (i mod 5 = 0) then
if (i mod 3 = 0) then
output := ‘Fizz-Buzz’
else
output := ‘Buzz’
else
if Output = ” then
Output := IntToStr(i);

Writeln(Output);
end;
Read(i);

end.

79. Fratm - February 27, 2007

PERL:
while ($i

80. gorgan - February 27, 2007

You just can’t beat the clean look of perl!

print$_%3?$_%5?$_:’Buzz’:$_%5?’Fizz’:'FizzBuzz’,”\n”for 1..100;

81. Fratm - February 27, 2007

I used an less than = but couldnt post it.. so changed it a little.

while ($i != 101)
{
if ($i%3==0) { $out= “fizz”; }
if ($i%5==0) { $out .= “buzz”; }
if ($out) {print “$out \n”} else { print “$i\n”; }
++$i;
undef ($out);
}

82. rascunho » Seção de dados do blog » links for 2007-02-27 - February 27, 2007

[...] Using FizzBuzz to Find Developers who Grok Coding « Imran On Tech 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”. (tags: tickletux.wordpress.com 2007 at_tecp Fizz Buzz FizzBuzz programming test) [...]

83. Too Embarrassed - February 27, 2007

VB 6 Immediate window:

for x = 1 To 100: Debug.Print switch(x Mod 3 = 0,”fizz”,x Mod 5 = 0,”buzz”,x Mod 15 = 0,”fizzbuzz”,True,x):next

84. Shabaka - February 27, 2007

These programming tests aren’t real world situations anyway. Every scientist knows that an object behaves differently under observation. Job interviews are already tense situations. Then to have to write code with someone breathing down your neck…I don’t think testing like this is a good way to screen out candidates. For one thing, if you are hiring a web developer, chances are s/he will be working online. What’s wrong with using Google? We’ve gotten to a point where we don’t have to recreate the wheel. What purpose does FizzBuzz serve in the real world? In my CompSci class, the prof said that programmers are lazy. If the problem has already been solved, whats the point so sweating about it?

85. Tim Balbekov - February 27, 2007

I’m 15 and have never taken a programming class. Seven minutes in C:

#include “stdio.h”

int main() {
int i;
for(i = 1; i

86. LowJo - February 27, 2007

Alright smart guys.

How about some OO? ;)

using System;

internal class Program
{
private static void Main()
{
for( int i = 1; i

87. Tim Balbekov - February 27, 2007

#include “stdio.h”

int main() {
int i;
for(i = 1; i LESS_THAN_GETS_FILTERED 100; i++) {
if( (i % 3 == 0) && (i % 5 == 0))
printf(”FizzBuzz\n”);
else if(i % 3 == 0)
printf(”Fizz\n”);
else if(i % 5 == 0)
printf(”Buzz\n”);
else printf(”%i\n”, i);
}

return 1;
}

88. Eric - February 27, 2007

my perl version (59 chars, borrowing some ideas from previous posts):
print+($_%3?”":”Fizz”).($_%5?”":”Buzz”)||$_,”\n”for 1..100;

89. Andy LeClair - February 27, 2007

Freshman at Northeastern University. 5 minutes in Pretty Big Scheme (PLT’s MzScheme (which includes R5RS) and the HTDP.org teaching languages)

;; fizzbizz: Number -> String
;; prints out the numbers from 0 to [sub1 n], fizz on a mult. of 3,
;; bizz on 5, and fizzbizz on both
(define (fizzbizz n)
(build-list n (lambda (x)
(cond
[(and (= 0 (modulo x 3))
(= 0 (modulo x 5))) "fizzbizz"]
[(= (modulo x 3) 0) "fizz"]
[(= (modulo x 5) 0) "bizz"]
[else x]))))

90. Andy LeClair - February 27, 2007

follow that up with:

(fizzbizz 100) or up to whatever you please.

91. otli4nitsa and keda - February 27, 2007

in perl… 67b :) paul, how’d you do 50b? plz post the code.

for(1..100){print ” “,$_%3?$_%5?$_:’fizz’:$_%5?’buzz’:'fizzbuzz’;}

92. Ayende @ Rahien - February 27, 2007

If Program I Can’t, Programmer Am I?

If Program I Can’t, Programmer Am I?

93. Steve - February 27, 2007

>To Shabaka:
>These programming tests aren’t real world situations anyway.

You’re right. These programming tests are far simpler than real world situations. If you can’t hack a simple contrived problem like FizzBuzz, it’s very likely you can solve a real world problem.

94. Dan - February 27, 2007

Javascript…

function FizzBuzz()
{
var strAlert=”";
for(var i=1; i

95. Dan - February 27, 2007

Javascript:
(I cant believe they don’t escape your input…)

function FizzBuzz()
{
var strAlert="";
for(var i=1; i<=100; i++)
{
strAlert += (i%3==0)?"Fizz":"";
strAlert += (i%5==0)?"Buzz":"";
strAlert += (i%3!=0 && i%5!=0)?i:"";
strAlert += ", "
}
alert(strAlert.substring(0, strAlert.length-2));
}

96. JimGee - February 27, 2007

I would happy if they can do this.

/* Comments about program go here*/
#include “stdio.h”
int main(int argc, char* argv[])
{
int i;
for(i=1; i

97. Don Beal - February 27, 2007

What a great post…

I have been writing code for about 20 years now, it’s amazing how in the past 10 nobody wants anything but VB for in-house stuff (mostly for TTM reasons) but this is a tremendous example of how there are a million ways to accomplish the same thing in 100 different languages.

I recently went out to dinner with a fellow consultant and we had this discussion about how new programmers know the latest .NET and nothing else and it is just pitiful how they have no foundation whatsoever. If it weren’t for Randy Birch I doubt they would ever pass a mid-term. I think that is the byproduct of making languages easier to read/write.

A lot of people can paint a house, but I wouldn’t call them all house painters.

Again, awesome thread.

98. JimGee - February 27, 2007

‘/* Comments about program go here*/
#include “stdafx.h”
int main(int argc, char* argv[])
{
int i;
for(i=1; i

99. JimGee - February 27, 2007

I would happy if they can do this.

/* Comments about program go here*/
#include “stdafx.h”
int main(int argc, char* argv[])
{
int i;
for(i=1; i&lt 101; i++)
{
printf(”%d “,i);
if(i % 3 ==0)
printf(”Fizz”);
if(i % 5 == 0)
printf(”Buzz”);
printf(”\n”);
}
return 0;
}

100. VOID - February 27, 2007

def FizzBuzz():
for i in range(1,101):
n=i%15
if(n==0): print “FizzBuzz”
elif(n in [3,6,9,12]):print “Fizz”
elif(n in [5,10]):print “Buzz”
else: print i

FizzBuzz()

Had to be original.

101. dumb - February 27, 2007

/

102. rob - February 27, 2007

the real test should be to leave a coding example and have the < and > show up ;)

103. imdumb - February 27, 2007

help me I’m dumb
/

104. Pat - February 27, 2007

I’ve done FizzBuzz in C language in 7 minutes and swap variables in 20 seconds. I think this is not bad and also not impressive. But these questions are too much hazardous. Too much synthetic to bring some guy down at the interview. Personally I’ve been two times a victim of that smart questions that I have not passed. However I consider myself as above average skilled programmer.

105. Steve - February 27, 2007

To Pat:
>Personally I’ve been two times a victim of that smart questions
>that I have not passed. However I consider myself as above
>average skilled programmer.

How do you know you’re an above average skilled programmer? There literally billions of dumb people roaming this earth who are deluded they are very intelligent though reality tells a different story. What makes you think you’re immune to being deluded about your own skills and intelligence?

Ask yourself this: Why did it take you 7 minutes to write FizzBizz? Do you type really slow? Did you have to fix bugs? Did you have to fix compiler errors? Did you know right away to use the mod operator?

Don’t you think if a person has trouble with a problem like this, they might do a poor job at solving real world problems with which the complexity of the problems are orders of magnitude more complicated?

106. robby - February 27, 2007

LoL its funny cause all these guys are like bragging that they can code this program by putting the code in their comment..

BUT ITS ALL WRONG

if i = 15 it would print, FIZZBUZZ
FIZZ
BUZZ
resulting it occurring twice..
cause you have no ‘continue’ after your if statement that checks for 3 and 5

it would also make more sense to check if it is divisible by 15 rather than 3 && 5

Hopeless.. im telling your boss to outsource your job to India.

107. Ray - February 27, 2007

in IL:

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 120 (0×78)
.maxstack 2
.locals init ([0] int32 i,
[1] string ‘value’,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: br.s IL_0065
IL_0005: nop
IL_0006: ldloca.s i
IL_0008: call instance string [mscorlib]System.Int32::ToString()
IL_000d: stloc.1
IL_000e: ldloc.0
IL_000f: ldc.i4.3
IL_0010: rem
IL_0011: brtrue.s IL_001e
IL_0013: ldloc.0
IL_0014: ldc.i4.5
IL_0015: rem
IL_0016: ldc.i4.0
IL_0017: ceq
IL_0019: ldc.i4.0
IL_001a: ceq
IL_001c: br.s IL_001f
IL_001e: ldc.i4.1
IL_001f: stloc.2
IL_0020: ldloc.2
IL_0021: brtrue.s IL_002d
IL_0023: nop
IL_0024: ldstr “FizzBuzz”
IL_0029: stloc.1
IL_002a: nop
IL_002b: br.s IL_0059
IL_002d: ldloc.0
IL_002e: ldc.i4.3
IL_002f: rem
IL_0030: ldc.i4.0
IL_0031: ceq
IL_0033: ldc.i4.0
IL_0034: ceq
IL_0036: stloc.2
IL_0037: ldloc.2
IL_0038: brtrue.s IL_0044
IL_003a: nop
IL_003b: ldstr “Fizz”
IL_0040: stloc.1
IL_0041: nop
IL_0042: br.s IL_0059
IL_0044: ldloc.0
IL_0045: ldc.i4.5
IL_0046: rem
IL_0047: ldc.i4.0
IL_0048: ceq
IL_004a: ldc.i4.0
IL_004b: ceq
IL_004d: stloc.2
IL_004e: ldloc.2
IL_004f: brtrue.s IL_0059
IL_0051: nop
IL_0052: ldstr “Buzz”
IL_0057: stloc.1
IL_0058: nop
IL_0059: ldloc.1
IL_005a: call void [mscorlib]System.Console::WriteLine(string)
IL_005f: nop
IL_0060: nop
IL_0061: ldloc.0
IL_0062: ldc.i4.1
IL_0063: add
IL_0064: stloc.0
IL_0065: ldloc.0
IL_0066: ldc.i4.s 100
IL_0068: cgt
IL_006a: ldc.i4.0
IL_006b: ceq
IL_006d: stloc.2
IL_006e: ldloc.2
IL_006f: brtrue.s IL_0005
IL_0071: call string [mscorlib]System.Console::ReadLine()
IL_0076: pop
IL_0077: ret
} // end of method Program::Main

108. Daruku - February 27, 2007

// c#

class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i

109. C Is A Write-Only Language - February 27, 2007

You suckas. In APL I can solve either problem in one character: a

I’ll take my job offer now, thanks.

110. DeathToSpam - February 27, 2007

ASP/VBScript, complete with purdy output formatting:

” & vbNewLine
Next %>

111. DeathToSpam - February 27, 2007

ASP/VBScript, complete with purdy output formatting:

Dim iLoop, bIsMultipleOfThree, bIsMultipleOfFive, sOutput

For i = 1 to 100
bIsMultipleOfThree = (i MOD 3 = 0)
bIsMultipleOfFive = (i MOD 5 = 0)

If (i ” & vbNewLine
Next

112. DeathToSpam - February 27, 2007

Tried to post, but characters got all truncated after submitting. :( Oh well.

113. Lumpio- - February 27, 2007

I can’t believe nobody thought of

for i in range(1, 101): print [i, "Fizz", "Buzz", "FizzBuzz"][int(i % 3 == 0) + int(i % 5 == 0) * 2]

114. Leonardo Herrera - February 28, 2007

Bah, couldn’t beat the 49 bytes mark (mine is 54 bytes).
print$_%3?$_%5?$_:”":Fizz,$_%5?”":Buzz,”\n”for 1..100;

115. Leonardo Herrera - February 28, 2007

(And I keep marveled at our capacity to intentionally miss the point.)

116. Marek Pułczyński - February 28, 2007

Just for completness - BASH oneliner (dumbly assuming modulo is as expensive as comparison)

for x in `seq 100` ; do if [ `expr $x % 15` -eq 0 ] ; then echo FizzBuzz; elif [ `expr $x % 3` -eq 0 ]; then echo Fizz; elif [ `expr $x % 5` -eq 0 ]; then echo Buzz; else echo $x; fi; done

117. Jason - February 28, 2007

A little more convoluted than the examples above in Python, but still (fairly) followable:

print ‘\n’.join([{(False, False): y[-1], (True, False): “Fizz”, (False, True): “Buzz”, (True, True): “FizzBuzz”}[y[0]] for y in [((x%3==0, x%5==0), str(x)) for x in range(1, 101)]])

118. mobmash blog » Blog Archive » links for 2007-02-28 - February 28, 2007

[...] Using FizzBuzz to Find Developers who Grok Coding « Imran On Tech (tags: programming interview) [...]

119. bumSki - February 28, 2007

// in C++
int main()
{
for (int count = 1; count

120. Matt - February 28, 2007

This is the shortest way I could think of doing it in C++. Of course, the layout is absolutely horrid (no whitespace, etc) but it’s short and it works:

#include
int main()
{
for(int i=1;i

121. MrMajestyk - February 28, 2007

For i As Integer = 1 To 100
If i Mod 3 = 0 Then
lblSolution.Text &= “Fizz” & vbCrLf
ElseIf i Mod 5 = 0 Then
lblSolution.Text &= “Buzz” & vbCrLf
ElseIf i Mod 3 = 0 And i Mod 5 = 0 Then
lblSolution.Text &= “FizzBuzz” & vbCrLf
Else
lblSolution.Text &= i & vbCrLf
End If

Next

122. Thomas Moitie - February 28, 2007

“Want to know something scary ? - the majority of comp sci graduates can’t.”

I’m 17, self-taught at php. A language in which I came up with a solution within 1 minute. I am utterly astounded if this statement is true

123. someone tired of writing code but loving of doing it also - February 28, 2007

after 15 years of c++ experience.
i wonder why you check three conditions for FizzBuzz thing!
one for fizz, one for buzz, and another for fizzbuzz..
wouldnt it be reasonable to use a boolean to control not to write the number. but only checking if it is a modul of 5 or 3. and after the each loop, feeding the line.

one thing is favorable, you should write a code to be extended easily in the future.

124. Thomas Messier - February 28, 2007

One test we used to administer for web developers was ask them to code a simple calendar. We gave them plenty of time, along with all the reference manuals. Lots of people couldn’t do it in 30 minutes.

125. Carl - February 28, 2007

What? No one’s tried to use C++ templates to compute FIZBUZ at compile time instead of at runtime.

126. Syed Muhammad - February 28, 2007

programming requires a little bit of talent and some passion of PROBLEM SOLVING. probably that’s why they can’t write the code. no problem solving skills.

127. Procyon - February 28, 2007

windows batch file:

@echo off
set count=1
:loop
set div3=%count%
set div5=%count%
:loop3
set /a div3 -= 3
if not %div3% leq 0 goto :loop3
:loop5
set /a div5 -= 5
if not %div5% leq 0 goto :loop5
if %div3%==0 (
if %div5%==0 (
echo fizzbuzz
) else (
echo fizz
)
) else (
if %div5%==0 (
echo buzz
) else (
echo %count%
)
)
set /a count += 1
if %count% lss 100 goto :loop

128. Unmaintainable Code - February 28, 2007

#include
int main(){int i;for(i=1;101>i;++i)printf(i%15?i%3?i%5?”%d\n”:
“Buzz\n”:”Fizz\n”:”FizzBuzz\n”,i);}

129. Anonymous - February 28, 2007

main(i){for(i=0;i

130. Matt - February 28, 2007

Here’s the main part of my code since my post was clipped for some reason:

std::cout

131. GS - February 28, 2007

Since everyone else is posting their solution:

#FIZZBUZZ.PY
for i in range(1,101):
flag = True
num = []
if i % 3 == 0:
num.append(”Fizz”)
flag = False
if i % 5 == 0:
num.append(”Buzz”)
flag = False
num = ”.join(num)
if flag:
num = str(i)
print “%s\n” % (num),

132. Trey Marcus - February 28, 2007

I’m not at all surprised at this because the interviewer is apparently asking a person to use a piece of paper or whiteboard to write a computer program. This is a completely foreign environment for most programmers I know and under the stress of an interview I am not at all surprised that most programmers would have trouble with such a task.

If you want to know if a person can architect a solution to a FizzBuzz style problem then I think its fine to ask them how they might go about solving it. Asking someone to write a computer program on a piece of paper seems a bit assinine and I think such interview tactics say far more about the interviewer than the interviewee.

If you want to test a person’s programming abilities… great. Give him a computer and a problem and leave him to work as he normally would to find a problem even if its just a few minutes.

133. Bob On Development » The Crisis of Programmer Competence - February 28, 2007

[...] In fact some interviewers have taken to screening all applicants early with some trivial programming problem like the FizzBuzz Question. [...]

134. shoebappa - February 28, 2007

Jacob, I’d seen the opposite question, to swap two variables without using a third…

$x = 5;
$y = 11;

$x = $x + $y; // 5+11 = 16
$y = $x - $y; // 16-11 = 5
$x = $x - $y; // 16-5 = 11

guess it only works with numbers, but using a third variable is the obvious solution, it’s unfortunate that you could weed anyone out with that question…

135. Jason - February 28, 2007

Common Lisp, format-style:
(loop for i from 1 to 100
do (format t “~A ~:[~;Fizz~]~:[~;Buzz~]~%” i (zerop (mod i 3)) (zerop mod i 5))))

136. test - February 28, 2007

137. test - February 28, 2007

< >

138. Exec - February 28, 2007

XSL version
=======

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:fo=”http://www.w3.org/1999/XSL/Format”>

<xsl:template match=”/”>
<xsl:call-template name=”fizzLogic”>
<xsl:with-param name=”iCount” select=”‘1′”/>
</xsl:call-template>
</xsl:template>

<xsl:template name=”fizzLogic”>
<xsl:param name=”iCount”/>

<xsl:if test=”$iCount &lt; 101″>
<xsl:choose>
<xsl:when test=”$iCount mod 3=0 and $iCount mod 5 = 0″>FizzBuzz</xsl:when>
<xsl:when test=”$iCount mod 3=0″>Fizz</xsl:when>
<xsl:when test=”$iCount mod 5=0″>Buzz</xsl:when>
<xsl:otherwise><xsl:value-of select=”$iCount”/></xsl:otherwise>
</xsl:choose>

<xsl:call-template name=”fizzLogic”>
<xsl:with-param name=”iCount” select=”$iCount+1″/>
</xsl:call-template>

</xsl:if>
</xsl:template>

</xsl:stylesheet>

139. The Mu » Blog Archive » Programmers that can’t program - February 28, 2007

[...] an aside, I gave the FizzBuzz problem a shot too.  I included my result below (it took me almost twenty minutes because I [...]

140. Sougent - February 28, 2007

I’m waiting for the solution in COBOL…….

141. Vincent McNabb - February 28, 2007

In JavaScript:

for(i = 1; i < 101; i++) {
  if(i%3==0) document.write("Fizz");
  if(i%5==0) document.write("Buzz");
  if(!((i%3==0)|(i%5==0))) document.write(i);
  document.write("<br>");
}

Basic

for i = 1 to 100
  if i mod 3 = 0 then print "Fizz"
  if i mod 5 = 0 then print "Buzz"
  if i mod 3 > 0 and i mod 5 > 0 then print str$(i)
next

142. Thomas Chapin - February 28, 2007

I wonder if you replace the lesser than symbol with the html escaped version, it will work? Testing now: <

143. Thomas Chapin - February 28, 2007

Ok. Cool. It looks like that worked.

Here’s my version done in javascript:

<script>

for(i=1; i<101; i++){

   if(i%15==0){
      document.write(”FizzBuzz<BR>”);
   }else if(i%5==0){
      document.write(”Buzz<BR>”);
   //
   }else if(i%3==0){
      document.write(”Fizz<BR>”);
   }else{
      document.write(i+”<BR>”);
   }

}

</script>

144. Thomas Chapin - February 28, 2007

whoops I left two slashes in there on accident (I was going to put comments in there but decided to remove them)

145. Ian - February 28, 2007

“I’m waiting for the solution in COBOL…”

something like (I don’t have a compiler to hand and it’s been a while):

PERFORM VARYING Count FROM 0 BY 1 UNTIL Count > 99
COMPUTE Three-Mod = FUNCTION MOD (Count, 3).
COMPUTE Five-Mod = FUNCTION MOD (Count, 5).
IF Three-Mod = 0 AND Five-Mod = 0 THEN
DISPLAY “FizzBuzz”
ELSE
IF Three-Mod = 0 THEN
DISPLAY “Fizz”
ELSE
IF Five-Mod = 0 THEN
DISPLAY “Buzz”
ELSE
DISPLAY Count
END-IF
END-IF
END-PERFORM

But as the nested IF’s suck, with an EVALUATE something like this:

PERFORM VARYING Count FROM 0 BY 1 UNTIL Count > 99
COMPUTE Three-Mod = FUNCTION MOD (Count, 3).
COMPUTE Five-Mod = FUNCTION MOD (Count, 5).
EVALUATE Three-Mod ALSO Five-Mod
WHEN 0 ALSO 0 DISPLAY “FizzBuzz”
WHEN 0 ALSO >0 DISPLAY “Fizz”
WHEN >0 ALSO 0 DISPLAY “Buzz”
WHEN >0 ALSO >0 DISPLAY Count
END-EVALUATE
END-PERFORM

146. flickrhoneys - February 28, 2007

Grok coding is one of my many hobbys, but i need someone to explain it to me :(

147. Fahd Mirza - February 28, 2007

Reading this thread was a splendid education

148. eric - February 28, 2007

indeed scary

that FizzBuzz problem should take 1-2 minutes max for an experienced developer.

Any more directly presents hesitance and a lack of skill. Unable for someone to answer it should mean that they chose a wrong path and should never have become developers.

149. eric - February 28, 2007

I cannot help noticing how simple this program is, and how many people have posted their solutions here, (trying of course to give a quick and clever minimalistic solution)

I haven’t read all the replies above me, and I am not surprised to found at least 3 solutions which do not work.

Not to name any but some solutions above print “Fizz Buzz FizzBuzz” when they find multiples of 3 and 5.

Being quick and rushing are not the same thing.

150. Alex - February 28, 2007

Can’t forget C# with a little LINQ…

foreach (string s in Sequence.Range(1,100).Select<int,object>(i => ((i%15)==0) ? “FizzBuzz” : ((i%3)==0) ? “Fizz” : ((i%5)==0) ? “Buzz” : i.ToString())) Console.WriteLine(s);

151. Ross Hawkins - February 28, 2007

IBM Lotus Notes @Function FizzBuzz

@For(n := 1; n <= 100; n := n + 1; output := output + @If(@Modulo( n ; 3 ) = 0 & @Modulo( n ; 5 ) = 0; “FizzBuzz”;@Modulo( n ; 3 ) = 0; “Fizz”; @Modulo( n ; 5 ) = 0; “Buzz” ; @Text(n) ));output;

or

@For(n := 1; n <= 100; n := n + 1;tmp := @If(@Modulo( n ; 3 ) = 0; “Fizz”; “”);tmp := tmp + @If(@Modulo( n ; 5 ) = 0; “Buzz”; “”);output := output + @If(@Length(tmp) =0; @Text(n); tmp ));output

Add a @Newline in there for readability if you wish. The great thing about the IBM/Lotus solution is that there’s hardly anyone out there who’s going to bother to test it.

152. Yuri - February 28, 2007

Someone wrote:

def FizzBuzz():
for i in range(1,101):
n=i%15
if(n==0): print “FizzBuzz”
elif(n in [3,6,9,12]):print “Fizz”
elif(n in [5,10]):print “Buzz”
else: print i

FizzBuzz()

Had to be original.

Hehe, nice!

153. Reality Me » And they call themselves programmers… - February 28, 2007

[...] he made a very good post asking "Why can’t programmers.. Program?" He referenced Imran on Tech who uses a simple problem to figure out if his prospective candidates can code. Imran also has good [...]

154. News Flash: Tech Interviews Don’t Work at mark++ - February 28, 2007

[...] Imram [...]

155. The Mu » Blog Archive » Blogosphere abuzz over FizzBuzz - February 28, 2007

[...] of traffic was created by Imran’s original post, Reg Braithwaite’s comment, and Jeff Atwood’s followup. All over the blogosphere, eager [...]

156. silentflute » Programmers can’t program? - February 28, 2007

[...] Using FizzBuzz to Find Developers who Grok Coding [...]

157. slimCODE, aka Martin Plante : Comment reconnaître un bon développeur - February 28, 2007

[...] à un poste de développeur est recommencé depuis un billet de Imran qui se sent obligé de mettre la barre très basse lors d’entrevues. Il propose l’implémentation d’un jeu d’enfant appelé FizzBuzz (tout bon [...]

158. Dilbert - February 28, 2007

There is some major flaws in this testing approach with any corporate programmer. First of all, you didn’t give him any time to discuss the requirements with analysts and end users, nor did you present him with use cases, workflows, or activity diagrams. You didn’t bother to explain unit test plans or QA bug tracking, nor did you even have the courtesy of scheduling five meetings to review specs, user interfaces, budgets, or schedules! You are also going to need to provide 45 minutes of time for the programmer to check his daily e-mail from his 5 supervisors who want to know what he is doing. AND YOU WONDER WHY THEY FAIL THE TEST???!!!

159. anon - March 1, 2007

Didn’t know about the MOD operator, so had to try a different method:

Sub FizzBuzz()

t3 = 0
t5 = 0

For i = 1 To 100

t3 = t3 + 1
t5 = t5 + 1
Number = False

If t3 = 3 And t5 5 Then
Cells(i, 5) = “Fizz”
t3 = 0
Number = True
End If

If t5 = 5 And t3 3 Then
Cells(i, 5) = “Buzz”
t5 = 0
Number = True
End If

If t3 = 3 And t5 = 5 Then
t3 = 0
t5 = 0
Cells(i, 5) = “FizzBuzz”
Number = True
End If

If Number = False Then
Cells(i, 5).Value = i
End If
Next

End Sub

160. Nick - March 1, 2007

In Prolog…

start :- fizz_buzz(1,100).

fizz_buzz(Lo,Hi) :-
Lo <= Hi ,
output(Lo) ,
Next is Lo+1 ,
FizzBuzz(Next,Hi).
FizzBuzz.

output( N ) :-
R3 is N mod 3 ,
R5 is N mod 5 ,
text( R3 , R5 , N , T ) ,
write(T) ,
nl.

text( 0 , 0 , N , N ).
text( _ , 0 , _ , ‘Fizz’ ).
text( 0 , _ , _ , ‘Buzz’ ).
text( _ , _ , _ , ‘FizzBuzz’ ).

161. The FizzBuzz Fizz And Weak Programmers on iface thoughts - March 1, 2007

[...] rightly points out that a lot of programmers, who talk well about programming, cannot actually code. Jeff Atwood follows up with his views on it. My experience has been a little different, when I got [...]

162. Solving FizzBuzz Problem | FuCoder.com - March 1, 2007

[...] quoted Irman’s FizzBuzz question, a trivial programming problem but surprisingly has stumbled many job applicants. Here is the [...]

163. Pete - March 1, 2007

I’m not really much of a programmer, but here’s my Fizzbuzz in C++:

#include
using namespace std;

int main(){
int x=0;
do {
x++;
cout

164. Pete - March 1, 2007

^that broke….

#include
using namespace std;

int main(){
int x=0;
do {
x++;
cout

165. Pete - March 1, 2007

……

166. Scorpyosis - March 1, 2007

You will never hire me, in spite of I can swap two variables (a,b)without creating a third variable :
a = a + b
b = a - b
a = a - b

>>> In respond to this comment :
3. Jacob - January 24, 2007
Want another good filter? This surprised me, but turns out to be a good indication of whether someone has actually ever programmed before.

Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.

If they don’t *start* with creating a third variable, you can pretty much write that person off. I’ve found that I can cut a third to half my (admittedly at that point unscreened) applicants with that question alone.

167. Don - March 1, 2007

“Dilbert (Feb 28)”, funny point, don’t forget to mention the one guy that is neither engineer nor developer somehow getting involved and insisting on changing the scope of the project every third minute.

;-?

168. A ver si no sabemos programar… « Para bellum - March 1, 2007

[...] autor al que se refiere es Imran, que está rechazando a montañas de programadores que no saben escribir un simple programa. Después de unas cuantas pruebas y errores descubrí que a las personas que se pelean con el [...]

169. Mike Nolan - March 1, 2007

81 characters in PHP but I’m sure it can be reduced a little:

for($i=1;$i

170. Dave - March 1, 2007

– in oracle plsql
begin
dbms_output.enable;
for i in 1..100
loop
if mod(i,3)=0
then dbms_output.put_line(’Fizz’);
elsif mod(i,5) =0
then dbms_output.put_line(’Bizz’);
elsif mod(i,3)=0 and mod(i,5)=0
then dbms_output.put_line(’FizzBizz’);
else
dbms_output.put_line(i);
end if;
end loop;
end;

171. Dave - March 1, 2007

– correction…plsql
begin
dbms_output.enable;
for i in 1..100
loop
if mod(i,3)=0 and mod(i,5)=0
then dbms_output.put_line(’FizzBizz’);
elsif mod(i,3)=0
then dbms_output.put_line(’Fizz’);
elsif mod(i,5) =0
then dbms_output.put_line(’Bizz’);
else
dbms_output.put_line(i);
end if;
end loop;
end;

172. Kisara - March 1, 2007

The sad part about this is that it took me about 3 minutes to write.

And I’m 12.

173. C.F. - March 2, 2007

What about this?

public class FizzBuzz {

static final int MAX_NUM = 100;

public static void main(String[] args) {

String str;

for(int i = 1; i

174. steve - March 2, 2007

My first python program. I have been wanting to learn Python so I thought I would install it and try FizzBuzz on for size. I see some other Python examples but this one is a little different. How bad is it?

for x in range(1, 101): # needs to be 101 to include 100 in the loop
out = “” # set reset output
if x%3 == 0: # x evenly divisible by 3
out = ‘Fizz’ # yes
if x%5 == 0: # x evenly divisible by 5
out = out + ‘Buzz’ # yes
if out == “”: # is out null
print x # yes print x
else:
print out # no print out