February 27, 2007
Evidently writing about the FizzBuzz problem on a programming blog results in a nigh-irresistible urge to code up a solution. The comments here, on Digg, and on Reddit – nearly a thousand in total – are filled with hastily coded solutions to FizzBuzz. Developers are nothing if not compulsive problem solvers.
It certainly wasn't my intention, but a large portion of the audience interpreted FizzBuzz as a challenge. I suppose it's like walking into Guitar Center and yelling 'most guitarists can't play Stairway to Heaven!'* You might be shooting for a rational discussion of Stairway to Heaven as a way to measure minimum levels of guitar competence.
But what you'll get, instead, is a blazing guitarpocalypse.
I'm invoking the Wayne's World rule here: Please, No Stairway to Heaven.
FizzBuzz was presented as the lowest level of comprehension required to illustrate adequacy. There's no glory to be had in writing code that establishes a minimum level of competency. Even if you can write it in five different languages or in under 50 bytes of code.
The whole point of the original article was to think about why we have to ask people to write FizzBuzz. The mechanical part of writing and solving FizzBuzz, however cleverly, is irrelevant. Any programmer who cares enough to read programming blogs is already far beyond such a simple problem. FizzBuzz isn't meant for us. It's the ones we can't reach – the programmers who don't read anything – that we're forced to give the FizzBuzz test to.
Good software developers, even the ones who think they are Rockstars, don't play Stairway to Heaven. And instead of writing FizzBuzz code, they should be thinking about ways to prevent us from needing FizzBuzz code in the first place.
* via Jon Galloway and Steven Burch.
Posted by Jeff Atwood
I think the reason I was compelled to code FizzBuzz is because I had to prove to myself that I wasn't as incompetent as you described.
(for (i 1 100)
(= 0 (% i 15)) (set 'i "FizzBuzz")
(= 0 (% i 5)) (set 'i "Buzz")
(= 0 (% i 3)) (set 'i "Fizz"))
I'm shocked how many people actually posted theyr own solution. Many of them just wasn't giving the right result.. forgetting to print the numbers.
I did a test me too, I was curious to see how much time it wold take for me. I think the point of the discussion was to say that many "developers" are unable to solve a simple problem in an acceptable time.
For everybody posted a solution without saying how much time did they spent on it.. well I think they havn't understood what was the "test" purpose. It's belivable that everybody sooner or later find the way.
My 2 cents
The first thing that's happening here is people are thinking "Hmm, yes it sounds like a really simple problem anyone should be able to cope with, but I'd better check it myself to make sure there isn't a hidden twist".
The second thing is that once people find there's no twist, it strikes them as quite a neat little problem which could be used to compare languages or try out new forms of obfuscation.
So I can see why any of us who hears of this problem first tries it out in our favourite language, then our second favourite language, then the language we're currently trying to learn, then the language we hate, then the toy language we once invented, then in our favourite language in a silly way, ...
Here's my solution in Smalltalk. It may not be the most elegant one, but, certainly, Smalltalk is the most elegant programming language.
" FizzBuzz test "
1 to: 100 do: [ :n |
(n \\ 3 = 0) ifTrue: [Transcript show: 'Fizz'].
(n \\ 5 = 0) ifTrue: [Transcript show: 'Buzz'].
((n \\ 3 ~= 0) (n \\ 5 ~= 0)) ifTrue: [Transcript show: n printString].
Here is my solution. It is written in 8051 assembly. You will need a 11.0592 MHz quart to run this thing :p
;* Program: fizzbuzz
;* Author: Samir Ben Abid (email@example.com)
;* Description: Outputs the FizzBuzz test results to an rs232 port, 9600,8,0,n
;* Compiled with Keil C51 running on Wine (lame!)
;*** CONSTANTS ***
; the first divider's value
divider1 equ 3d
; the second devider's value
divider2 equ 5d
; the maximum value that the fizzbizz program can reach. Must be between 0 and 100
limit equ 100d
;*** CODE ***
; Code segment at 0
cseg at 0
ljmp start ; avoir interrupt vectors
org 0x100 ; and go to a safer place
start: ; a.k.a here.
mov scon,#0x52 ; set UART to 8,0,n
mov tmod,#0x20 ; Timer1: autoreload
mov th1,#0xfe ; speed: 9600 bps
setb tr1 ; start Timer1
mov dptr,#intro ; load the intro and...
call emit ; beem it!
mov r7,#0 ; set counter to 0
cjne r7,#limit,continue ; are we finished with counting to 100?
jmp rest_in_peace ; Yes... rest in peace.
call do_crlf ; go to next line
mov r4,#1 ; set the (r7%3)? flag
inc r7 ; counter++
mov b,#divider1 ;
mov a,r7 ; divide the counter by three
div ab ;
mov a,b ; and get the reminder
jz do_fizz ; if it is null, do a fizz :)
mov b,#divider2 ;
mov a,r7 ; divide the counter by five
div ab ;
mov a,b ; and get the reminder
jz do_buzz ; if it is null, do a buzz :D
mov a,r4 ; did we do a fizz?
jz compute ; if yes, reume the loop
call write_number ; else, write the number
jmp compute ; and resume the loop
clr tr1 ; Stop Timer1
jmp $ ; AM STONED!
; gets: nothing
; returns: 0 in r4
; Beems a "Fizz" through the UART
mov dptr,#fizz ; load the fizz
call emit ; then display it
mov r4,#0 ; and leave a message: "I was here"
jmp next ; then resume your normal activity
; gets: nothing
; returns: nothing
; Beems a "Bizz" through the UART
mov dptr,#buzz ; load the buzz
call emit ; then display it
jmp compute ; then resume the loop
; gets: nothing
; returns: nothing
; Beems the Carriage return/Line feed controle caracters through the UART.
mov dptr,#crlf ; load crlf
call emit ; then send it
ret ; and return
; gets: the adress of the message to display in dptr
; returns: nothing
; Beems an ASCIIZ message, stored in the code memory, through the UART.
mov r6,#0 ; initialize the index to 0
mov a,r6 ;
inc r6 ; load the pointed byte
movc a,@a+dptr ;
jz fin ; if zero then return
jnb ti,$ ; if the last transmission isn't over, stay in your place
mov sbuf,a ; and transmit!
clr ti ; and clear ti to get further notifications :p
jmp bc_1 ; end of the loop
ret ; return
; gets: the digit's value in A (must be between 0 and 9)
; returns: nothing
; Translates a one-digit-bcd value located in A into Ascii and the beems it through the
; gets: the number to display in r7
; returns: nothing
; Beems the Ascii representation of the number located in r7 through the UART. r7 must
; be between 0 and 99
mov a,r7 ;
mov b,#10d ; divide the number by 10
div ab ;
jz write_l ; if it si less than 10 then just write the modulo
call emit_id ; else, write the result (because r7100) :)
mov a,b ; prepare the parameters
call emit_id ; and send the digit
ret ; then return
; *** STATIC DATA ***
; type: Asciiz string
; containes the fizz message
fizz: db "Fizz",0
; type: Asciiz string
; containes the buzz message
buzz: db "Buzz",0
; type: Asciiz string
; contains the intro message.
; intro shares it's Asciiz 0 with crlf, 3 code bytes of economy :)
intro: db "The FizzBuzz test"
; type: Asciiz string
; containes the crlf byte couple
; shares itself and it's Asciiz 0 with intro
crlf: db 10,13,0
; Bye Bye :)
I've liked the BrainF**k solution.
Any solution in INTERCAL? :)
I love this example it is a prime way to weed out the candidates who don't understand the basic operators that come in very handy.
The debate about proving oneself aside, this whole thing is symptomatic of a downward spiral in hiring by so called Human Resources.
I was recently multiply interviewed by a well-known internet search company (who shall obviously remain nameless) for a post in image processing. Now, I think it's fair to say I'm an expert in this field with over 12 years research experience and a PhD to boot. However, in one of the many interviews I was asked question after question on programming databases - about which I know approximately zero - by someone who had a list of set answers. Some of these questions required "real time" programming, ie. writing programs on the fly, which is damn near impossible. Needless to say that because of that one interview I wasn't hired.
On another occasion some years previous I was asked to program a Euclidean distance estimate function, which I did perfectly, only to be told that it was no good since it wasn't how their programmer had done it. This was a tacit acknowledgement that the person asking the question didn't actually know how to do it but could compare the two and see they weren't the same. [Incidentally, my solution was the more elegant of the two].
My points being:
1) This kind of mindless task is far easier to ask than to answer, so is perfect for HR robots looking to eliminate candiates a.s.a.p. with minimall effort. As such it should be avoided not encouraged..
2) It proves little about programming ability - which is a problem solving skill rather than a regurgitation skill. We all have access to the resources on the 'net in our daily work and knowing how to use *that* effectively is more important.
It's so difficult:
for($i = 1; $i 101; $i++) echo $i % 15 == 0 ? 'Fizz-Buzz' : ($i % 3 == 0 ? 'Fizz' : ($i % 5 == 0 ? 'Buzz' : $i)) , "\n";
I just enjoyed seeing how this problem was expressed in all the different languages... It's slightly more complicated than hello world, so it shows a little about those language's syntactic and logical structures.
I wrote it in Postscript, which was fun. I wrote 3 versions of it... the "normal" version, the compact speed-optimized unreadable/obfuscated version, the completely generalized version.
The final version allowed you to specify any range of numbers (1-100), any strings for replacement (fizz, buzz), any numbers to compare against (3,5), and any comparison logic to use via a named functioned (multiples of), so that you now have a function that will iterate over a range replacing integer values with string via whatever arbitrary comparison you wish...
Why? Because it was fun, and relaxing compared to the programming tasks I have to do all day at work. I became a programmer because I enjoy it. It's hard to maintain your enjoyment of it when "business interests" are constant destroying any fun you might be able to eek out of your work. So, doing stuff like this is akin to doing the crossword puzzles in the newspaper... It's no great feat, but it exercises your mind just the tiniest bit, and it's completely frivolous, which is less boring, and less stressful than not doing it.
This Fizzbuzz issue really is a problem though. I remember in first year, there were a bunch of people who couldn't code worth a damn. I figured they'd be weeded out by 2nd year. Nope. 3rd year? Nope. 4th year? Nope, they're still there, right beside me. Somehow, they manage to basically skimp by all the classes, without ever actually coding. My supervisor for my thesis says he even has this problem with graduate students that can't code! I think the issue is that, in my program at least, there aren't that many hardcore coding classes. And, basically all the hard coding courses past 1st year put you in groups to do work... and large groups at that, since the classes are so big. So, if these people can manage to squeeze by first year, then it only gets easier to weasel by in 2nd, 3rd, and 4th year, since they simply let other group members handle the coding part of the project... or they may do nothing at all. The few courses I've taken that have hard individual coding assignments that are actually worth something are only required by certain streams. Many people also manage to graduate w/o the courses by getting substitutes.
I've been using FizzBuzz-type questions, but usually simpler than FizzBuzz, in interviews for 20 years now (I just dug out my first interview notes from 1987!). I've interviewed people for many types of programming job. What follows the FizzBuzz-type question will vary depending on the specific job, but I always start with the trivial one.
Why do I ask them to write Fizzbuzz? If they're lost when looking at that one, I can save everybody's time by wrapping up the interview quickly.
Jeff's question was "Why do we have to ask people to write FizzBuzz?"
1. Because a CS degree is not a guarantee of basic coding literacy. That's unfortunate, but true. Me, I think they should ask for their money back from the colleges that churned them out.
2. Because HR is now a game of tick-the-box. "Salary expectations within range? Check. Degree? Check. Two years of Java? Check. Able to program? Sorry, HR can't handle that one."
Last week, I interviewed 5 people, all with CS degrees. None of them answered the FizzBuzz-type question correctly, and only one of them came close. It was a bad week. :-(
I'm disappointed in the answers to the fizzbuzz problem that have been posted here. I never saw my favorite:
Kinda puts the world into perspective. I'm 12 years old, in 6th grade, and the fizzbuzz problem is ridiculously easy. Picked up C++ for dummies and within a few weeks, I can script (albeit not really well...).
I HAS A FizzBuzz in LOLCode (http://tinyurl.com/27eszt). Now I just need a compiler so I can test it...
CAN HAS STDIO?
I HAS A LOOPVAR
IM IN YR LOOP
IZ (LOOPVAR MOD 3 LIEK 0) AND (LOOPVAR MOD 5 LIEK 0)?
IZ (LOOPVAR MOD 3 LIEK 0)?
IZ (LOOPVAR MOD 5 LIEK 0)?
IZ LOOPVAR BIGR THAN 100? KTHXBYE
IM OUTTA YR LOOP
"And instead of writing FizzBuzz code, they should be thinking about ways to prevent us from needing FizzBuzz code in the first place."
Is the same as saying...
"And instead of taking tests, they should be thinking about ways to prevent us from needing to take tests in the first place."
That is the dumbest thing I've heard today. How do you get people to prove they know what they know? It's called testing. Thanks for being an egotistical prick about it though.
I'll play stairway to heaven whenever I want!
Good point. I read the FizzBuzz replies, and have to say, I actually was asked to write a FizzBuzz once for a job interview. They told me they were impressed, which only depressed me. If knowing how to make use of modulus arithmetic and a for loop is impressive, then what is the world coming to?
Still, I got an interview out of it. But no job - I think that was because I couldn't encapsulate the differences between C and Java during the interview. And/or, I wasn't a C# guru, though not exactly a C# moron.
I'm also depressed by the paucity of languages most FizzBuzz respondents used. I mean, what, no COBOL? No FORTRAN? No Algol68? No PL/I? No IBM S/390 HLASM? No Ada? No Bliss?
And only one APL!
"The interviewer who is expecting, and eventually selecting, a programmer to developing the FizzBuzz logic in less than couple of minutes, is actually trying to build a team for a project which is more than likely to lead to failure and waste millions of dollars and leading the project stakeholders to suicidal death. A programming skill is not something that may spontaneously be predicted and come up with the best logic suitable for a given scenario.
Akbar on March 5, 2007 06:10 AM "
Looks like someone failed the test. I guess we do need a test to see if a person can think... even if an 8 year old could pass it
I'm just amazed how many of the replies that have 'solutions' are actually wrong and have various errors!
So there is more than one point to the FizzBuzz test:
1) Can the application understand programming languages at all?
2) Does the application think carefully about their answer, and check it?
I haven't been subjected to tests of my skill yet, and inasmuch as I think it would be "oh so much fun" I have to say that I find it a little bit ludicrous to put people down. A lot of people say they are programmers when really they are not and couldn't tell you what was wrong with a page of code if it was written entirely in English and not in code at all. But lots of people do that...and not just in coding and programming. I run into people every day who tell me that they are Media Buyers and I after two questions about their job I know that they don't even know the title means. But at least they're trying...and I think that's a little more important. This sounds more like an argument I would have with a standardista that thinks people shouldn't be allowed to post sites on the web unless they have received training for it. What's the point of asking the question if you already know the answer?
When I applied for a Software Engineering job, I was actually given the FizzBuzz problem from this blog as a "test." After getting the job, I was told that credentials don't always mean a person is qualified, and that they used FizzBuzz to weed out the poor applicants.
Very interesting blog, and I think you’ve pointed out one of the problems that’s going to appear in any profession, whether it be guitar players or programmers. There will always be those people out there who don’t feel the need to read anything or learn anything new. They already think they know it all and that they can’t learn anything from the programming community. As you said, it’s these people who need the FizzBuzz test, but it’s also these people who are never going to understand WHY they need it. It’s a rather sad thought, but it’s true. Of course, I don’t think all programmers who don’t read blogs are like this – some, I think, just have no desire to interact with the community (some are loners by nature), and some probably just don’t have time. I know there are weeks where I go without reading all the blogs I normally like to just because there are only so many hours in the day….
You might be shooting for a rational discussion of Stairway to Heaven as a way to measure minimum levels of guitar competence.
Well, you got us off the guitarpocalypse, but I think rational was overoptimistic.
Well, here I was thinking that I was about to be sooo clever:
"So I guess that this isn't the time to write my solution?"
Then I saw that a few already had written their solution. No, not as a joke. We live in a world of dumbasses, folks.
(No offense to those who DID write their scripts here, but.. C'mon!)
Obligatory xkcd: a href="http://xkcd.com/356/"Nerd Sniping/a
This is really Stupid!
There are some freaks out there who just want to see the code they pick up from this blog. I recently went for interview and the only thing he ask me is problem(fizz buzz), from 1...100
first i was thingking it may be a really big problem or there should be some catch but then i just wrote it
I wrote the code in Java and then he told me there is bug because you I didn't put less than equal(both), i just used 100 , what a asshole ?
He said that he got disappointed ! that made me nervous
then he gave me the problem of reversing the numbers in a Char Array and by then i was king of nervous and used the method to copy to another array and reverse the order. He told me that take to much memory not a good solution then bye.....bye
I am ready to bit that he is not smarter than me in any way and i face him in a normal way not the interview i will kick his ass in technology and programing
I am not against test or testing with basic problems but at least try to understand the logic people use to solve the Problem instead of pin pointing that u didn't write both less than or equal sign.
I came home and within couple of minutes i wrote the test case run the both problems successfully.
In my opinion the person who is interviewing should have a understanding of his fields and positive attitude to hire the people and not just this FIZZ BUZZ problem alone.
One thing more he was just taking of reading blogs , C'mon
there are freaks who spend all their time on blogs, who the hill in this world has that much time to read all this bull and write all the time.
get a life people......go out do something ....i mean it!
People has work, kids, wife and parents to take care
Its gr8 to learn the new tech but not from BLOGS.
The Excel Solution:
Paste the following formula into a cell on row 1, then select the bottom corner of the cell and drag down to row 100:
(define (fizzbuzz n)
(let ((test (cond ((= 0 (modulo n 15)) "Fizzbuzz")
((= 0 (modulo n 5)) "Fizz")
((= 0 (modulo n 3)) "Buzz")
(if (= n 1) (list n)
(cons test (fizzbuzz (- n 1))))))
(map print (fizzbuzz 100))
Concerning the variable swapping problem form the previous thread:
A simple solution in python ;-)
a, b = 3, 5
a, b = b, a
And yet many of the comments yesterday fell along the lines of "anyone can do that" (no, they can't...and that is the point), "a puzzle like this does not prove anything" (a puzzle??? FizzBuzz is a quick and effective competency test to weed out the losers early on). Cwazy sheep.
A new entry for the Diagnostic and Statistical Manual of Mental Disorders: FBOCD (FizzBuzz Obsessive-Compulsive Disorder)!
Sorry Jeff, my opinion of the average intelligence of the readers of you blog has dived. :( But just a little bit #61514;
In theory I have to agree, though it depends on how far you push readable+clear. Smart, gets the job done, follows instructions - that would be my first set of requirements.
It really is embarassing that there's anyone out there who proffers themself as a professional developer that can't figure out how a modulus works.
It's not a bad idea to have applicants prove that they're not helpless without The Internets to copy and paste from, but what about the flipside of the coin? Why not put FizzBuzz in front of them and walk you through it, having them explain what it does? At the end of the day, I want to work with people who not only can write good code that works, I want them to be able to read so-so code that's mostly working and know that since they can read code, their first instinct to fix what's wrong won't be to deploy tactical nukes from geosynchronous orbit and start from scratch.
@Bill: I think the only wrong answers would be either dead silence or sequential print statements with no loop or branches. It is hard to believe, yes, but plenty of people would just stare blankly at you or mutter strange excuses ("If I had my IDE" or "If I could research it on the net"). Freaky, but true. It’s a great first question.
I was surprised at the number of solutions to the FizzBuzz problem as well, Fizz Buzz is a total waste of effort in my opinion. Also many of the solutions are very obfuscated.
But to Jeff's point, why the need for FizzBuzz:
It comes down to validation. If I am looking for a C# programmer, I am looking for some level of skill with that langauge. People fluff resumes, exaggerate, etc. So, we give the fizzbuzz question(s) to make sure they can do it.
Professional licensing would help here, but how are you going to license someone when the technology and toolset changes so rapidly?
Maybe a basic programming license in addition to certification in different langauges.
I am not talking about Microsoft here or any large company that hands out certificates, I think FizzBuzz shows the need for a governing body of programming/computer science that is nonprofit and independent.
If someone shows me a programming license, then there is no need for fizzbuzz at all, otherwise, they have to demostrate something about thier skills because all programmers need to produce code at some point.
I think it is important to reiterate the point that programming is not about syntax and quite often not about math; it's about solving problems. There are varying degrees of ability in solving problems, but I think that creativity, an immense drive to learn and an innate desire to improve something are core traits of a good programmer.
CS programs are designed to give you a broad overview of the Computer Science field. I personally think that this is valuable to persons who may not live and breath the IT industry and are not sure what areas that they are going to be interested in. Every field has it's specialists, I think that the "General Practitioner" in Computer Science is far less common than a specialist and should be the goal of anyone that is aspiring to be a great programmer. If your only tool is a hammer, then every problem becomes a nail.
At the same time, we as humans only have so much that we can focus on at once. In the book "Domain Driven Design" by Eric Evans, Mr. Evans talks about the need for a Ubiquitous Language to be used on Software projects so that there is no need for translation between programmers and domain experts. This reduces the amount of effort required to comprehend the overall system. A Senior Developer should be able to see the project in terms of the overall picture. Otherwise they can put their considerable talents to waste on sins such as premature optimization.
Finally, criticizing a programmer because they do not know the exact parameters or return value of a method is counter productive. Especially in the light of tools such as Google that can help to find hundreds of examples or docs on how to use that function. Einstein was asked for his phone number once and he looked it up in a phone book. The person who had asked was surprised that Einstein didn't know his own number and made a remark to Einstein about it. Einstein's response was "Why should I memorize something I can so easily get from a book?" Einstein followed by saying that he never memorized anything which could be looked up in less than two minutes.
YouTube made an impact because of the concept, not because some coder found the most elegant way possible of paginating the videos.
Going back to the original issue, here are my 2c.
This "FizzBuzz" is a good problem for an interview question, because you could complicate the problem after the interviewee solves it.
So first solution would probably have:
1. a statement where they REDO the divide by 3 and divide by 5.
2. Console.Write and Console.WriteLine (in the C# world, or printf or System.out....) in three places.
First performance optimization would be to remove the additional divides by a flag.
Now the interviewer could complicate the problem to writing to a different IO stream - say a slower one -say a file across the network. So we are writing 100 lines right now. Would it not be better to write the entire output into a StringBuilder(in C#,Java) and then push it out the output channel? probably yes.
Now of course if we were doing the same exercise on 1000 lines instead of 100, maybe we should flush the buffer every now and then, say after every hundred lines.
So on and so forth...
You are absolutely right, I deal with lot of people and I sometimes I feel how can a programmer call himself a programmer when he doesnt even know Queue, Stack etc, I tell someone that I think you have to use queue or stack here to solve this issue, then they go blank... if in .NET, what class should I use, fine there are classes for Stack and Queues, but the problem is, they dont know the difference between pop/push and enqueue/dequeue and how it affects the logic.
Being an almost graduated computer programmer coming across this article made me feel a lot better about my programming capabilities. Since i was able to figure out the solution to the fizzbuzz problem fairly easy i feel like i have a solid shot at making it into the pro's(programmer's get it lol). One thing i guess interviewees dont take into to consideration is the fact that programmers since they are always on a computer have easy access to google. To be able to search google when a simple syntax error is popping up or when a solution to a problem you are trying to solve has already been solved why not use it. No use to invent the wheel all over again. So to test someone using a basic program like fizzbuzz dosent really contend with the decision process that should be applied when hiring new candidates. To me its the programs people made that consider them a good programmer or not. Yes, not many people made programs that are on the market but any student who has taking computer science as their major has tons of programs to show off to potential employers. Also, if they are a computer science student who really wants to show their skills to the world they would be programming their own projects on the side just for knowledge of knowing how to program.
I can bang out fizzbuzz in 2-3 minutes in an interview situation, in multiple languages. I've done so multiple times. So why can't I get past the interviews and actually get a job offer?
In Python, slightly optimized-
for i in range(1,100):
if (i%3==0 and i%5==0): p="fizzbuzz"
if (i%3==0): p="fizz"
if (i%5==0): p="buzz"