FizzBuzz: the Programmer's Stairway to Heaven

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.

Eddie Van Halen's Patent

I'm invoking the Wayne's World rule here: Please, No Stairway to Heaven.

Wayne's World: Stop, No StairwayWayne's World: No Stairway Sign

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
146 Comments

I think you just managed to gracefully hit that button in each of us, that makes us want to prove ourselves.

Nicely done :)

Juha on February 28, 2007 1:06 AM

I'm not going to lie... I didn't do it as a chalange to your post, but I was afraid I couldn't do it :-/... Let's just say I was disappointed in myself. I couldn't remember how to format strings for printf() nor could I remember how to use cout... and I program everyday, except sunday :).

Jeremy on February 28, 2007 1:07 AM

Comments were down yesterday when I went there. But the answer is: very few (java) folks code in the language's syntax. Almost all code in some framework, and haven't addressed FizzBuzz low level problems in years, if not decades. And won't in any User Application. Doesn't make them lousy programmers.

BuggyFunBunny on February 28, 2007 1:07 AM

In the guitar shop where I'm mostly buying stuff nobody is allowed to play "Seven nation army". (or a Nirvana song I've forgot)
All the clerks roll their eyes if anybody starts playing that.

Bernhard on February 28, 2007 1:08 AM

First solution:

http://www.themathlab.com/music/stairway.mid

I win!

James on February 28, 2007 1:15 AM

using Codinghorror.Blog;

BlogPost post = Blog.LoadBlogPostByID("00781");

foreach (Comment comment in post.GetAllComments())
{
if (comment.Index % 2 == 0)
{
comment.Tag = "Rockstar developer";
}
}


sorry :)

Peter Palludan on February 28, 2007 1:36 AM

This reminds me the temptation of trying so when people read "90% of people can't touch one's elbow with one's tongue."

Kevin Lee on February 28, 2007 1:42 AM

Jeff after this post i really respect you.. really! :D
buuuut i would like to read some articles about java sometime..
i know that properly is not ur field but surely with all your experience, you know alot about it.. at least i think so.
Regards

ps. this post is a little off topic argh

Donnie from Rome on February 28, 2007 1:57 AM

Donnie, here's a good article on Java.

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

Siegfried on February 28, 2007 2:22 AM

(Arguing with Pete over hardcoded constants and cargo cult programming)

You got me exactly backwards. I don't think readability is unimportant; I think it's critically important. That was the whole point! Separating out definitions for 3 and 5 does nothing good for the readability or maintainability of the solution.

I know that avoiding hardcoded constants is a good guideline, for just the sort of reason you give. But when you turn down that candidate for not following it, is it because they're a lousy programmer who doesn't even know the guideline, or a really good programmer who knows it and also knows when it doesn't apply?

For a really simple example of why it doesn't apply here: how do you print "Hello World" in your favorite language? is it something like this (using Python 'cause it's short):

print "Hello World"

or something like this:

import sys
destination = sys.stdout
string_to_print = "Hello World"
print destination, string_to_print

Would you really turn down a candidate who came up with the former instead of the latter?

Jeff on February 28, 2007 2:30 AM

Writing article about hard drives: -2
Having Wayne's World reference: +10

You have been redeemed, Jeff.

foobar on February 28, 2007 2:49 AM

"so...why not use this to the advantage of codinghorror. Wright a weekly challenge or something. One, it could be fun and two. It might be a good way to learn from other people. There is never just one way of doing things. It would be interesting to see how other people solve problems.

Bryan"

I wouldn't mind seeing a weekly (perhaps monthly) small challenge like Bryan has suggested, it would be a good way to get responses from your readers. I have this blog on my google homepage so whenever I notice a new post I will read it if I have time (which I usually do between classes).

As a side note...did anybody solve it for their TI-83 plus?
It took me a while due to my lack of syntax knowledge as well as not knowing if there is a remainder function, but it runs.

GO TI-83 plus!

-Dana

Dana on February 28, 2007 2:56 AM

I stumbled on the FizzBuzz discussion by accident and enjoyed reading all the way to the bottom. I'm at the other end of the employment train (leaving rather than joining). Programming is just a hobby interest for me and I was delighted to see I had got the FizzBuzz sort-of right quite quickly after failing miserably with a series of non-programming trick questions yesterday.

My experience is that the problem identified by Jeff applies in all fields. Most people can do, by rote, what they have been trained to do but have no idea how to do stuff when a broader perspective is required. At an interviewing course years ago the teacher was adamant about asking interviewees "what do you think of that [experience], looking back?". In other words, trying to see if they learned anything, or if they could stand outside their experience and look at the big picture. He also said "hire a learner". [Of course most interviewers haven't a clue :)]

On thing that was not mentioned was the issue of debugging. Maybe the various posters take that skill as a given. In my experience the ability to diagnose problems efficiently is the most elusive of human skills. It is a skill that is required in many walks of life - auto-mechanic, physician as well as programmer. However the ability to debug seems to have a much higher importance in computer programming.

There are lots of clever ways to solve problems in programming. But the choice of solution depends on the context and the specification - neither of which can be adequately explained in a short question. The solutions that simply printed out the answers line by line work, provided there will never be a change in the requirement. The need to swap to things seems to me to be an undesirable level of cleverness that would lead to problems - and some people identified that.

If I was hiring someone to jump in and write code tomorrow it might be more effective to give them a page of code (ideally on laptop) and ask them to fix it - together with explaining their thought process as they were going about the task.

If I was hiring someone who could grow into the task I would be content with a generic answer to the fizzbuzz problem - for example "loop through the numbers and check whether each number is divisible by 3 or 5. Then print the answer depending on that test" That could lead to another question "how would you know if it was divisible by 3?". What I would like to know is whether the person was capable of conceiving a solution to a problem - it is easy to look up the details once you have a suitable plan. Indeed, with Google, programming is probably the "science" in which it is easiest to look up the details.

Another characteristic that is sadly lacking among computer geeks is the ability (or willingness ???) to understand the non-computer client's needs, or to express back to the client his/her need in ways that enable the non-computer person to express the requirement better. I had a sharp experience of this recently where I was never once asked "what are you trying to achieve - maybe there is another solution?". Personally I regularly find myself saying to someone "I don't think I understand your question" - and I find it is better to let them struggle to explain themselves rather than to prompt them in what may be the wrong direction. If I was hiring someone who would interface with clients I would be looking for this skill as well as the ability to program. Getting the specification right solves 80% of the problem.

Finally, an interesting aspect of all the responses was the various languages that people use. I think it is 100% safe to say that they are all less comprehensible to the reader than the initial VBscript example. By comprehensible I mean how quickly could you figure out what the program snippet does after you have forgotten how you did it, or could you figure out what someone else wrote.

Robin2 on February 28, 2007 3:16 AM

Well of course.

I suspect that while there was a rapid digression into solution-space, there was also a perceived need to demonstrate that the responder was, through their ability to code a solution, competent to comment.

Guilty as charged, your honour.

Mike Woodhouse on February 28, 2007 4:09 AM

Thank you! Very reasonable.
And thank you again for sharing this, as you say it's depressing but important.

Sugggan on February 28, 2007 4:09 AM

I suppose the article could be interpreted as a challenge; your recent posts about "some people just don't/won't get it" would no doubt strike fear into the hearts of aspiring developers who worry they might not "get it". Perhaps the solutions are merely a self assurance that they are in the exclusive developer club, only for people who "get it".

Greg Poole on February 28, 2007 4:13 AM

As far as I'm concerned, the BrainF**k, Perl and Java solutions are the best. It's all about the Factory Pattern being reapplied.

When I applied to a certain large software company, there was a timed test for programming certain things in their framework that I would have taken had I gotten to go up there and interview. I'm overall glad I didn't get the job, but it was definitely interesting to see what they expected.

When I sent them in a really nice set of answers six hours later with quite a nice little GUI, I was pretty insulted to not get a full interview.

I think there's a line to how many questions like this you should be able to ask. Honestly, it's really expensive for me to spend four hours staring at a stupid problem. My first solution to this problem would be picked from the best I could find with Google or Experts Exchange with a freeware license. Any time I have a truly hard problem in the real world I get to go ask someone who knows or find an answer that doesn't suck, so why would it be any different during a test?

"Build me a Wikipedia-style intranet in the next eight months." Tends to make me think, "Why not just install the Wikimedia and spend a few weeks modding it?" Any other answer I consider silly beyond belief, and encouraging these answers is a cruddy way to do business.

Dylan Brams on February 28, 2007 4:18 AM

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

I'm sorry, but I disagree. I read about half the comments on the last post and didn't see one genuinely good solution to the problem. None contained a single comment, all utilised hard coded constants and most were just plain obfuscated. I'd expect more from students, let alone graduates and professionals.

I think in an interview I'd hire a programmer who couldn't quite get the problem right (okay maybe not for a problem this simple), but did so in a clear and readable way rather than someone who gets it right but all crammed into a few short unreadable lines.

Pete on February 28, 2007 4:23 AM

Looking at the comments to Phil's post, it turns out some people can't read at all....

Adi on February 28, 2007 4:31 AM

Compulsive puzzle solving is right. Both my wife and I are coders, after reading this I related the results and basically both of us started figuring out other questions of similar weight [after answering the question as quickly as possible first]. Yet another astute observation, Jeff.. kudos.

phreakre on February 28, 2007 4:32 AM

I think it struck a nerve -- a well coded algorithm to solve a "problem" than can be easily grasped gives one the chance to "create", solve, whatever, and some people got that buzz to try it out.

Used to be that we had less to "care about" -- there were no frameworks, browser issues, dev tools with a zillion options and windows, and event programming was still future except for catching keyboard interupts.

I was a math major at Cal and couldn't get into the CS department. Even though I did lots of engineering and numerical analysis coding, the concepts of registers, assembly code, and other vital nuts and bolts that you may never use but help one grasp the bigger picture were missed.

I was in "high" school when STH came out, but truthfully LZ's first album is still better. We made fun of Black Sabbath's "Iron Man" because it was only 3 chords, but listening to it today, the guitar solo is better than most of what I hear. So I have mellowed or today's guitar playing is not so good.

"Officially", the rock era of the late '60s/early 70s ended with Bad Company.

steve burch on February 28, 2007 4:43 AM

I like the question 'why we have to ask people to write FizzBuzz'. I think it is because the notion of what programming IS has changed (eroded?) over time. For example, in the late 60s if you claimed to be a programmer, you likely had the skills for FizzBuzz. But now I often hear of 'HTML programmers'. AFAIK, HTML doesn't have much in the way of flow control. Also, I rarely write many complicated algorithms any more, I just use existing stuff from the C++ STL or the JDK - but it is important that I know how to use them (compose with them, etc.), rather than just look for a magic method that does exactly what I want in a single line. 'There's no keyword or library function to do that' isn't an excuse to me.

Now my rant turns personal. I work for a government agency, and I find I get paid on the same scale as an 'HTML programmer'. I work on distributed simulation systems, and do a fair amount of system level coding. I've done a couple of web projects before I joined the gov't, using servlets, SQL, etc. In my experience I rarely meet people who can't write FizzBuzz in my recent work, but I encounter people doing web development who probably would have a tough time with FizzBuzz - and those same people (who haven't walked in my shoes) will remark how challenging their work is. My private thought is, if you think that's difficult, try my job for a day. And I have walked in their shoes. OK, my rant is done now.

Brad on February 28, 2007 4:49 AM

Brandon:
There are good programmers who can solve problems with resources (again not specifically thinking of FizzBuzz here) and a lack of pressure that will undoubtedly make mistakes in an interview. I'd say their approach is far more important than the actual results in this situation.

Knowledge and experience can be acquired if a little lacking, but your approach to programming (I mean the way you think as a programmer when solving problems) is difficult to change once you have it.

Pete on February 28, 2007 5:01 AM

"Is there a term for people who compulsively answer rhetorical questions?"

Yes, programmers. Apparently...

Telos on February 28, 2007 5:13 AM

if(hedgerow.Contains(bustle))
{
alarmState = AlarmState.NotAlarmed;
bustle = SpringCleanForTheMayQueen();
}

Daniel on February 28, 2007 5:20 AM

if(hedgerow.bustle) {
alarmed = false;
}

mayQueen.springClean();

natron on February 28, 2007 5:20 AM

Maybe you should give us something tougher than FizzBuzz.

philihp on February 28, 2007 5:20 AM

That's spooky. Stop it.

natron on February 28, 2007 5:23 AM

We ask questions like FizzBuzz because interviews are short, because programming involves a great many skills and concepts, and we might as well screen for the basics immediately, and because the question probably has high predictive value.

I wouldn't give a candidate more than 60 seconds of my time to demonstrate that he/she knew how to handle FizzBuzz before moving on. "You don't need to write code, just explain how you'd do it."

I interview a lot of people, these days mostly hardcore quants, with degrees in the hard sciences, who almost certainly had to sling code at some point, but who may not be very good at it. For me, the interesting side topic here is:

What is a good set of basic questions -- like FizzBuzz -- that one can use to quickly cover the bases of programming skills and concepts? To quickly determine if you're talking to the real McCoy?

There was some discussion and criticism of "puzzles" and "trick questions" on yesterday's thread. I'm not into tricks. I posted a couple of my standards yesterday, the "four cards" problem and the hash table question. In my experience, these questions have high predictive value. They're not remotely reliable on their own (not getting the four cards problem, all by itself, means very little), but combined with a substantial number of other problems, they draw a clear picture in a short period of time.

I'm looking for "Bladerunner"'s voice-comp test for programmers: "I'm going to ask you a series of simple questions..." Because for me, the hard part is separating the wheat from the chaff.

John Pirie on February 28, 2007 5:32 AM

When I was reading the non-solution comments with other more advanced questions, I thought that often the first idea I have is not the best. Quite often I figure out an improvement over night. I don't know how I would do in an interview expecting "instant" best answers. For example, on the "are two strings anagrams of each other" question, I thought:
sort and compare
count number of each char and compare counts
what if it is unicode? count with hash map?

BTW I have been writing assembly and embedded programs for 20 years and cannot remember ever swapping to variables "in place" by hand. (There areinstructions to do it on many processors that I have used.)

expr on February 28, 2007 5:34 AM

I tried to ask a question on this blog yesterday amid the torrent of code, but I'd like to ask it again since it was impossible to get a response. It basically went like this:
I'm by no means an excellent programmer, I've only studied C and C++ as a hobby for a few years and written a few thousand lines of code and some silly and incomplete 2d games. The alleged inability of CS majors to even solve the Fizzbuzz or equivalent, plus many poster's inability to have the social skills to not flood the blog with illegible, or "clever", code makes me think that I could be competitive for an entry level programming job, at least in C. I had never considered applying for a programming job before yesterday.

Is that a bad attitude? I really like coding but would I be simply adding to the wave of noobs in professional positions? Should we leave programming jobs to those who grok it in the highest degree?

blkmage on February 28, 2007 5:48 AM

so...why not use this to the advantage of codinghorror. Wright a weekly challenge or something. One, it could be fun and two. It might be a good way to learn from other people. There is never just one way of doing things. It would be interesting to see how other people solve problems.

Bryan

Bryan on February 28, 2007 5:49 AM

You're right, I don't play "Stairway to Heaven."

But I rock the Hell out of "Foggy Mountain Breakdown."

mrprogguy on February 28, 2007 5:54 AM

As a student programmer, after reading this, my first instinct was to create a 'solution'; Like the rest, I'm a compulsive problem solver. I've only been in classes for 4 months, yet feel like a task this simple should be redundant. During nightly practicals, I'll crank out a oversimplified templated singly linked list, just because I think that this may be what I would be requested to do in a professional environment. Things like loops and conditionals are such second nature to me, and even after only 4 months of program, I would assume that any 'senior programmer' would be able to turn out a little under one hundred bytes of code in the blink of an eye. I suppose that which is constantly reinforced at my school holds very true:

Learn the hardest things, and remember them. Don't 'learn and remember' the simple things, know them so well that they're the fiber of your very being.

Nitz on February 28, 2007 6:01 AM

In a professional environment it usually boils down to something related to an excel spreadsheet.

natron on February 28, 2007 6:06 AM

oops, sorry, I thought it was a job interview.


wait, does this mean i won't get the job?

;)

David H. on February 28, 2007 6:12 AM

No FizzBuzz?! DENIED!

Jae on February 28, 2007 6:20 AM

Maybe we compulsively answer FizzBuzz questions because many of us have Autistic Spectrum Disorders and saw the answer before completly reading the problem statement? Or it could be one upmanship.

Brian on February 28, 2007 6:27 AM

So, let me get this straight: you asserted that many programmers aren't worthy of the title, proposed a simple problem to establish some basic credibility, and were surprised when your readers commented?

Also just in: three seconds after you finish reading this sentences, you will be thinking about an elephant in a tutu.

Sam Leibowitz on February 28, 2007 6:35 AM

Daniel natron: thanks for the laugh. :)

Charles on February 28, 2007 6:45 AM

Had I read the FizzBuzz topic right after the original post, I would not have felt 'compelled' to write a solution.

After seeing many other solutions in various languages, it became a fun thing to add. :)

Now, off to my hacked Guitar Hero II game, and Stairway! :)

Jadawin on February 28, 2007 6:49 AM

Or more precisely, it's the programmer's /Smoke on the Water/...

Leo Comerford on February 28, 2007 6:54 AM

I'm looking for "Bladerunner"'s voice-comp test for programmers

It's a "Voight-Kampf" test.

OK, that was needlessly nitpicky, but I just couldn't stop myself.

Dave C. on February 28, 2007 6:55 AM

@blkmage: How are you ever going to gain experience if you don't try for a job? There's a lot of code to be written out there, and not all of it needs someone who groks programming really deeply. To quote The Magic Schoolbus: "...get out there, take chances, get messy and make mistakes!" If you're anything like me, the way to grok programming more fully is to do it, and if you can get paid for it, so much the better.

@Pete: I think a number of programmers (several on my LJ, at any rate) see obfuscated/ultracompact code as a challenge, and a bit of a status symbol: "I can solve this problem in N statements!" "Oh, yeah? I can solve it in N-1 statements!" To some degree, it's a pissing contest. Looked at from another perspective, though, there's a certain joy to be had in being able to solve a problem in as few characters as possible. It likely won't make any difference in execution speed, but it may save a bit of time and tedium on the part of the coder. And readability really is in the eye of the beholder.

Full Disclosure: I'm one of those compulsive FizzBuzz solvers. :)

Kit Peters on February 28, 2007 6:57 AM

To Jeff's astonishment people were actually posting solutions to the simple code test. But really Jeff, when will a programmer not try to flex his might and show off his skills a bit. However, some people where trying to one up each other and the started to post code and forgot to do parts of the test, like print the number everywhere else or print FizzBuzz every 15 lines.

The author stated that a decent programmer should be able to write the code in under two minutes. But it's one of those tests that is so simply you have a chance not to follow the directions. As a lot of the people who were posting solutions didn't do.

So to me the test is a two-fold question. 1) Can you write a simple program and 2) Can you follow directions?

Tim on February 28, 2007 6:59 AM

One more thing - the sort of programmer that reads this blog *shouldn't* be above implementing FizzBuzz. Far from it - it's the sort of thing that one should do periodically to make sure one remembers how. :)

Kit Peters on February 28, 2007 7:00 AM

Actually, I thought that there was one interesting point about fizzbuzz itself.

The best solution obviously involves using modular arithmetic. If, by some strange twist of fate, the candidate did not know modular arithmetic, would that be an automatic no-hire?

By "not know" I mean that they don't know that most (all?) computer languages have an operator that gives you the remainder; not that they are completely "math illiterate".

What if they solved the problem using some other method (say, counters or repeated subtraction)?

Bill on February 28, 2007 7:28 AM

I found yesterday's Fizzbuzz post very encouraging. I am not currently employed as a programmer, but would very much like to be. I could write the Fizzbuzz program without a problem (albeit in BASIC…) and it is encouraging that even at my neophyte level, my genuine fascination with code puts me ahead of such a greater percentage of applicants!

The Egger on February 28, 2007 7:28 AM

Pete on February 28, 2007 04:23 AM
I'm sorry, but I disagree. I read about half the comments on the last post and didn't see one genuinely good solution to the problem. None contained a single comment, all utilised hard coded constants and most were just plain obfuscated.

That whooshing sound was the joke flying over your head. The main point of fizzbuzz was that it was incredibly easy a caveman could do it and people in here were just having fun.

As for writing comments: this was an interview question. Did you also scream that they put their code in the default package!!1!!

BlogReader on February 28, 2007 7:30 AM

I have a different take on the programmers taking up the challenges of the FizzBuzz problem (at least on tickletux's site).

Good programmers are creative, like a challenge and enjoy solving old problems in new, more elegant, ways. The way I'm reading those posts are thoughts on how to make it more elegant and efficient. And a way to show off different programming paradigms and language strengths.

Big, complex, monster problems are solved by breaking them down into smaller, more manageable problems. Seeing how programmers tackle problems in new ways is what I want to see from my candidates.

Love the Wayne's World reference, but this is different. Guitarists that play Stairway are trying to play it verbatim and often are not very good at it. However, if you played well enough to put your personality into it and make it yours then that's another thing altogether.

Ben Craigo on February 28, 2007 7:36 AM

This is how you respond when you get a passionate, creative reaction from programmers in response to your post? That's really lame. And I'm glad no one listened to you.

Seeing *idiomatic* Python, Ruby, Scheme, Common Lisp, Haskell, SmallTalk etc. solutions is edifying if you don't program in that particular language everyday. Lots of the solutions showed examples of some of the great features in those languages. Look at Raganwald's compose carbonation functions if you don't believe me. And I'm not ashamed that I learned something from reading that.

Joe W. on February 28, 2007 7:39 AM

The truly frightening thing is the large proportion of 'solutions' in the comments that are wrong. Bounds errors, logic errors, failure to read the spec errors, holy cow.

Definitely proof that the "average" developer should be doing something else for a living.

Todd Blanchard on February 28, 2007 7:55 AM

[quote] The best solution obviously involves using modular arithmetic. If, by some strange twist of fate, the candidate did not know modular arithmetic, would that be an automatic no-hire? [/quote]

I'd say no - there's plenty of other (admittedly sub-optimal) ways to do it. I think the point is that they can come up with *something* that works. You can always optimize later. :)

Ugly way to do it (gotta prove I can, y'know) - VBA, since that's what I'm using at work
Dim number As integer, Dim count3 As integer, Dim count5 As integer
count3 = 0
count5 = 0
For number = 1 To 100
count3 = count3 + 1
count5 = count5 + 1
If (count3 = 3 And count5 = 5) Then
Debug.Print "FizzBuzz"
count3 = 0
count5 = 0
ElseIf (count3 = 3) Then
Debug.Print "Fizz"
count3 = 0
ElseIf (count5 = 5) Then
Debug.Print "Buzz"
count5 = 0
Else
Debug.Print number
End If
Next number

Allen on February 28, 2007 8:07 AM

It might have been lost in the noise from yesterday, Jeff, but there are people trying to make Software Engineering a real trade.

For example, The Association of Professional Engineers of British Columbia has a "Software" designation. That's right - it's being treated the same as Civil, Mechanical, and Electrical Engineering. You get a degree, you work under other P.Engs for 4 years, then you take a professional practice exam.

http://www.apeg.bc.ca/reg/ProfessionalEngineer.html

It's a start.

There's also a planned change to make members of all disciplines take continuing education courses.

themagni on February 28, 2007 8:12 AM

Wait a sec. Does that mean you're lousy if you CAN solve it? Or does it mean your lousy if your not depressed about how lousy all the comp sci grads are? But wait, if I read this blog then all my lousiness is canceled right? I'm gonna put that on my resume.

andy on February 28, 2007 8:14 AM

The problem I saw was that everyone posted solutions in VBA, Windows SCripting Host, VB etc. How is that any solution? I think maybe the FizzBuzz should also analyze what language coders choose. WSH? VBA? your scaring me...

ryan on February 28, 2007 8:25 AM

If the people who spent all that time writing code snippets here yesterday had, instead, written to their local universities/colleges/community colleges and asked them how this could happen, maybe something productive could have come from it.

Programmer, police thyself!

Also, I thought Brad's comment on the rise of HTML was on the mark. After reading through the a good chunk of those comments yesterday, I was talking with the wife about, essentially, the same thing. When we were taking programming classes back in the 70s, there really wasn't any such thing as a GUI (and certainly no HTML). Your I/O options consisted of "do you want a card punch machine with or without a working ribbon?"

What it works out to is that we spent almost no time programming UI stuff because the UI was primitive and unimportant: algorithms and data structures were important, UI wasn't.

My son is currently in his 3rd year of a CS degree. His first 2 years were "wasted" with all the stuff necessary to satisfy the "well-rounded person" requirements of the university. The first term of the actual CS degree stuff started with basic programming (FizzBuzz stuff). But, instead of moving on into linked lists and data structures, they spent the next term working on the GUI aspects of programming. Only this term are they back into what we used to consider to be actual programming.

It's too late to stop the flood of vitriol that will result from that previous paragraph. But, I do want to say that I understand why the school does that: it's a GUI world now and that stuff needs to be covered. But, what's the cost of that coverage? FizzBuzz failure?

David A. Lessnau on February 28, 2007 8:52 AM

My thoughts on the problem 'sorting the sheep from the goats'

http://jimliveshere.spaces.live.com/blog/

Jim on February 28, 2007 9:04 AM

ryan on February 28, 2007 08:25 AM
[snip]
The problem I saw was that everyone posted solutions in VBA, Windows SCripting Host, VB etc. How is that any solution? I think maybe the FizzBuzz should also analyze what language coders choose. WSH? VBA? your scaring me...
[/snip]

Real programmers adapt to the workplace to a certain extent, and are comfortable working with languages that aren't necessarily their ultimate favorite, in order to Get The Job Done (tm).

You cannot always just parade into a job and say "right, toss that lot and lets start over with xyz language".

Tim Gunter on February 28, 2007 9:14 AM

"Real programmers adapt to the workplace to a certain extent, and are comfortable working with languages that aren't necessarily their ultimate favorite, in order to Get The Job Done (tm)."

I agree, 11 years here, and have had to work on many VB, MTS COM+, perl, ATG Dynamo migrations. Not fun stuff. My problem was that this was their first shot off the hip. If it was C#, C++, C, Java even Python or Ruby but they chose WSH and VBA?!?

Do "Real Programmers(tm)" when they choose to write something choose the most hideous language of choice?

Play some golf! a href="http://golf.shinh.org/p.rb?FizzBuzz"http://golf.shinh.org/p.rb?FizzBuzz/a

ryan on February 28, 2007 9:19 AM

"Guitarpocalypse" Ha! Classic.

Hey, I noticed you prepended "www." to my domain. Touche my friend. Touche.

Haacked on February 28, 2007 9:19 AM

On a side topic

At least two posts yesterday mentioned that the swap using arithmetic operations a=a+b; b=a-b; a=a-b; fails in case of overflow.

This is incorrect, it is a property of 2's complement arithmetic that if a sequence of additions (that include subtractions) has a final result that ends up in the representation range, then the result will be correct irrespective of the presence of intermediary underflow or overflow. As most processors use 2's complement representation, the swap using arithmetic operations mostly works.

Strange but true, you can use the program below to convince yourself that it is indeed working in the case of a swap, irrespective of overflow or underflow (done on short so that the loops finish in a reasonable time).

#include "limits.h"

int main() {

int i ;
int j ;
short int a, aswapped ;
short int b, bswapped ;

for (i=SHRT_MIN; i = SHRT_MAX ; i++) {
for (j=SHRT_MIN; j= SHRT_MAX ; j++) {
a = i ; b = j ; aswapped = b ; bswapped = a ;
a = a+b ; b = a-b ; a = a-b ;
if ((aswapped != a) || (bswapped != b)) {
printf ("Swap failed for %d %d\n", aswapped, bswapped) ;
}
}
}
return 1 ;
}

Marc Delvaux on February 28, 2007 9:24 AM

Speaking for myself, I went off and implemented FizzBuzz after I read your previous post (but before I'd read the comments), and was satisfied that it worked correctly (I didn't post my implementation as a comment). That's partly because it looked like a fun little challenge that wouldn't take long, and partly because I think that this would be a good litmus test when I'm recruiting, and I want to be fair about it. Basically, I won't ask other people to do things that I'm not willing to do myself, so if I'm going to say to someone "You took 15 minutes to work it out? You suck!" then I want to be sure that it's a fair assessment by trying it myself first. In the same way, I've been doing some certifications that are way below my current skill level (e.g. ECDL) just so that I know what they measure, and how much attention I should pay to them if I see them on a CV.

John C. Kirk on February 28, 2007 9:24 AM

[quote] [quote] The best solution obviously involves using modular arithmetic. If, by some strange twist of fate, the candidate did not know modular arithmetic, would that be an automatic no-hire? [/quote]

I'd say no - there's plenty of other (admittedly sub-optimal) ways to do it. I think the point is that they can come up with *something* that works. You can always optimize later. :)[/quote]

Whatcha talking about! Modular arithmetic is slow slow slow! Your counting method is much much better!

I'd put some serious thought into improving it though, and I reckon I've come up with the required improvements to optimize further...


StringBuilder line = new StringBuilder();
StringBuilder lines = new StringBuilder();
for (uint i = 1, fizz = 1, buzz = 3; i = 100; ++i, --fizz, --buzz)
{
if (fizz 2)
line.Append("Fizz");
fizz = Math.Min(fizz, 2);

if (buzz 4)
line.Append("Buzz");
buzz = Math.Min(buzz, 4);

if (line.Length == 0)
line.Append(i);

lines.AppendLine(line.ToString());
line.Remove(0, line.Length);
}
Console.WriteLine(lines);


(and come on, when you say you can't play Stairway, what's the first thing someone's gonna do...)

{grin}

Bonehead Barry on February 28, 2007 9:25 AM

Did anybody write this in Assembler? :)

Martin Marconcini on February 28, 2007 9:37 AM

Dana: Forget the TI-83! Your post has inspired me to write the HP48 version.

1 100 FOR i i 5 MOD i 3 MOD IF DUP2 AND THEN DROP2 i -STR ELSE "" "Fizz" IFTE SWAP "" "Buzz" IFTE + END NEXT

One of these days I should just write an RPL compiler for x86...

C-Mike on February 28, 2007 9:43 AM

I too wrote a solution, though i didn't post it. I think John C. Kirk hit it on the head -- I wasn't trying to see if I could solve the problem, it was obvious. I wanted to see how quickly I could do it myself. Out of the people who posted solutions, I bet most of them timed themselves. It's not the solution that mattered, it was the race.

And then following the race, it's a matter of elegance -- how short can I make it? How fast can I make it? Does my favorite pet language beat the others?

Ethan on February 28, 2007 9:44 AM

Pete, regarding hardcoded constants:

That's funny--I saw some similar complaints in the original posting, and my thought was that I'd be more wary of these silly buggers complaining about hardcoded constants.

That's cargo cult programming: following rules that originally had a good purpopose--"avoid hardcoded constants"--without understanding why they are good rules, what problems they are intended to avoid, or when they do or don't apply. Taken to extremes, it's responsible for a good chunk of thedailywtf's horrors.

In this case, the problem description has the 3 and 5 right in it. Doing otherwise in the code that solves the problem is just obfuscation. It doesn't help with maintenance, because you don't yet know what direction the problem is going to go. Will the numbers change? Will a third case get added? Will we need to add support for output in French ("Le Fizzbuzz") and UK English ("Fizzbouzz")? Keep the code as simple as possible and maintenance is always easier.

Jeff on February 28, 2007 9:49 AM

If you use fizzbuzz at a litmus test for PHP programmers, look for something like

while($a101){ echo (($a%3)!=0) ? $a : "Fizz"; $a++;}

vrs the 1,000 responses of looped if/when/while/for stuff that was posted.

This will actually play a part when I am looking at future coders, though we generally ask for site examples and extensive code samples as well as references.

:)

joeldg on February 28, 2007 9:49 AM

sry your Fizz has no Buzz

ryan on February 28, 2007 10:14 AM

"That's cargo cult programming: following rules that originally had a good purpopose--"avoid hardcoded constants"--without understanding why they are good rules"

Avoiding hard coded constants is almost always a good idea, as it makes your code more self-documenting and allows easier changes when the constant is used in multiple places (as many FizzBuzz solutions do with three special cases and two constants). If you think things like this rule and making code as readable as possible are bad ideas then you probably haven't had the same nightmare experiences working with other people's code that I have.

I work with embedded systems where people frequently litter the code with (hex) constants to set certain in registers (which can do anything in hardware -- no way to tell from a number). Even with comments (which may no longer be in sync with the code) it's virtually impossible to tell what they're trying to accomplish just by looking at it -- I spend literally hours each week pouring over data sheets for different devices trying to work out what each arcane constant is for.

Following such a rule is a (very) minor inconvenience to you, but can be a HUGE assistance to your colleagues (even the ones you will never meet).

Pete on February 28, 2007 10:32 AM

Based on the number of incorrect programs posted to the original article, I'd have to say the FizzBuzz test works. It's a shame such a test is needed.

Kuerwen on February 28, 2007 11:03 AM

Not surprised about the comments in the last one. I myself had put it all together in C++ in my head moments after reading the problem.


Didn't read most comments on the other pages, but was I the only one to question the wording of the question in that output should perhaps be(separate lines for readability, although the problem never mentioned newlines:

1
2
Fizz
4
5Buzz
Fizz
...
14
FizzFizzBuzz
16


Oddly, English is harder to understand and interpret than the code it asks for. Personally, I'm more satisfied with my answer than the most common one. But who wouldn't like their own answer more?


Is a test like FizzBuzz necessary? I'm a Junior in college working for dual majors in CompSci and Information Systems, and I'm disappointed with the quality of the students I see in my classes, especially my programming classes. Problems that "just make sense" to me seem like they're hard to grasp by other CS majors at my school. Am I tooting my own horn? I hope not, but it seems that where computers were once an area that only enthusiasts pursued there is now a lot of people going into CS/IS that are just looking for what they're told is a solid career path.

In a class of 30 of my peers with a CS major at my school, I would expect maybe 4 could solve this problem in under 5 minutes. I'm sure most of you put it together in your heads in well under a minute, with maybe a minute to write it out. When will "teaching students to be proficient programmers" actually mean a programmer that can, as Jeff so elegantly put it, "program".


/Warning "Shameless Self Plug located below"
I'm finishing my Junior year of college at Clarion University of Pennsylvania in mid-May. I am majoring in both Computer Science and Information Systems, with programming being my hobby and focus for career choice. If anyone's company (or one that they know of) is looking for any summer interns or temporary summertime entry-level programmers for any reason, please contact me. I'm in PA, but have no problem relocating for the summer, especially if the job would pay enough to afford a place to live and some food. Anyone with any opportunities or interested in a resume or any information please feel free to contact me at walwal31(at)hotmail(dot)com.

Shameless? Yes. But where else will I find a better group of programmers and locations that may actually be part of a company looking for summer work?

Keven Kalkbrenner on February 28, 2007 11:14 AM

You either have it or you don't.

If you have it you will make a great programmer if you stick with it.

If you don't you had better find something else to do besides making nightmares for people like me...

Rabid Wolverine on February 28, 2007 11:42 AM

Real men write it in brainf**k anyway...

(Not quite to spec, does 00-99, but that's a documentation issue).

++++++++++++++
+++[++++++++++-]+++++++[+++++++
++++++++-]++[+++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++-]++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++[++-]+++++++++++++++++++++++++
++++++++[++-]++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++
+++++++++++++++++
+++++++++[++++++++-]
++++++[[-]++++++++++
[-]++++++[++++++++-][
[-]++
+[-][-][++-][+-][-]++
+[--][-]+[[-][-]][[-]
[-]....[-]]
[-][-][++-][+-][-]++++
+[--][-]+[[-][-]][[-][-
]....[-]]
[.
.[-]]
+.-]+-]

Therac-25 on February 28, 2007 12:09 PM

It appears that my logic has failed. I was just in the shower when it occurred to me that I faulted.

Multiples of 15, under my above interpretation of the original problem, should read "FizzBuzzFizzBuzz".


I guess I failed :(

Keven Kalkbrenner on February 28, 2007 12:10 PM

I don't believe any of this. If this is true why am I working so hard. You mean I can spend the day cutting out paper dolls of code and I'd be as good as the guy next to me? (I always suspected I was better than you, Carl.)

fxp on February 28, 2007 12:22 PM

Is there a term for people who compulsively answer rhetorical questions?

Kenny on February 28, 2007 12:26 PM

FizzBuzz was easy, the real sad truth is most programmers are not tech savvy enough to send me $10 via PayPal.

Dave on February 28, 2007 12:32 PM

Yes there is, isn't there?

Manu on February 28, 2007 12:39 PM

That be as it may, putting forth the idea is much like saying "Nobody can lick their own elbow": Whether it's true or false, everyone who reads it is going to try it.

Dante on February 28, 2007 12:43 PM

Nobody commented their code! Okay, so we all understand this simple task but one of the biggest headaches is working with other people's code when they leave no clues to what they're trying to do. The benefits of using a top programmer to quickly write brilliant code are negated if the maintenance consequently takes forever.

Gary on March 1, 2007 2:23 AM

I too tried the solution, and I too timed myself. I think if you gave any small problem in any context you will get a lot of people trying to solve it for the hell of it.
I would agree that it is a slightly biased test in that to do it in a reasonable time it requires knowladge of the modulo operator. I guess I would expect decent programers to be aware of it, but still...

[ICR] on March 1, 2007 4:01 AM

Well, the length to which these comments have gone, and off the mark at that, is something to think about.

I've interviewed 100's of programmers over the years, and many of them (20%? I don't know) couldn't write fizzbuzz, and I would never recommend those for any programming job. In fact, if no one else were available, I would recommend hiring no one - adding these people would probably have a negative impact.

However, there's been a conversation around my office about the concept of a 'computer trade school', say, for non-programming computer roles. For example, desktop support, server or LAN admin, etc. Like the 'computer operator' of days past.

I know there are existing programs, some with more merit, some with less. And like the fizzbuzz solution I expect people will answer this a debate over the relative merit of existing education applied to a 'computer operator' role.

But I'm interested in if it is even possible to come up with a proper program. Everyone in my office is quite impressed at the quality of local tradesmen from the local community college. I think it would be valueable to everyone if that quality control could be applied to a non-programming computer trade. And, would it be necessary for these graduates to write fizzbuzz? I would hope even these graduates could write fizzbuzz, but they might never have to.

IMHO, all real programmers understand flow control, and those who don't aren't programmers (yet, until they do). I'm also surprised when people don't understand at least the concept of a stack, but I have met some reasonably productive programmers that don't - and they pick it up right away when it is explained. I think any fizzbuzz test that requires flow control is a good first line. For more advanced jobs further lines will be needed.

Last time I hired a student, I did 10 interviews, and one of the interview questions was "discuss the difference between assignment in C/C++ and Java". Everybody except 2 said they were the same, even when I tried to prod them a little. Only 2 of 10 understood the difference between objects and object references. Is this because Java is taught and pointers are neglected? Perhaps, but I don't think so, because the wrong 80% seemed to think Java assignment copied by value like C/C++.

I think some of the 80% might have done an OK job writing a program for me, but the task for the student was actually to use AspectJ to retrofit some instrumentation into some legacy code. One of the 2 that got this question was hired, and he did a fine job, and I had a great time acting as a reference for his job search on graduation.

But I don't think you'd need a guy like that to configure and maintain an Exchange e-mail server. Nor would you want that (he'd get bored), nor would he want that (he'd be bored) - overqualified.

Any thoughts on what a non-programming fizzbuzz test would be for hiring a desktop support/server/LAN admin?

Brad on March 1, 2007 4:02 AM

TristanK has some ideas on non-programming FizzBuzz:

http://blogs.technet.com/tristank/archive/2007/02/28/it-s-not-just-a-programming-problem.aspx

Feel free to email him directly if you need to, he's a great guy.

Jeff Atwood on March 1, 2007 4:47 AM

ms tsql
sorry it's procedural

declare @bzr varchar(4);set nocount on;declare @onehundredniiiiinetynine int;set @onehundredniiiiinetynine=1;declare @rdir varchar(8);declare @200 int;set @200=1000*@onehundredniiiiinetynine/10;declare @onehundredniiiinetynine int;set @onehundredniiiiinetynine=@onehundredniiiiinetynine;declare @not int;set @onehundredniiiinetynine=1;declare @exists int;set @bzr = 'fizz';set @200=201-@onehundredniiiinetynine-(@onehundredniiiinetynine*(@200%201));set @not=0;while @onehundredniiiinetynine=@200 begin set nocount on;declare @buzr varchar(200);set @exists=floor(21*@200/31415/*9*/);set @buzr=NULL;select @rdir=coalesce(fizzbuzz+nullif(fizz,@bzr)+nullif(fizz,'buzz')+nullif(fizz,@bzr+'buzz'),fizz) from (select convert(varchar(3),@onehundredniiiinetynine) as fizzbuzz, case @onehundredniiiinetynine%((7^4)%9) when @not then @bzr else '' end+case @onehundredniiiinetynine%(12(7754)^1) when @exists then 'buzz' else '' end as fizz) bitwise_nor;print @rdir+coalesce(@buzr+'fizz','') set nocount off;set @exists=NULL;set @onehundredniiiinetynine=@onehundredniiiinetynine+1;end;

nightsql on March 1, 2007 5:54 AM

// C/C++ extensible version.
// If you can do easily extensible in C, you can do it extensible in any language.
// You can copy/paste this code and compile it.

#include stdio.h
int main(void)
{
nbsp;nbsp;nbsp;nbsp;int max = 100;
nbsp;nbsp;nbsp;nbsp;int i,j;
nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp;// just change this three lines to change the program
nbsp;nbsp;nbsp;nbsp;int size = 2;
nbsp;nbsp;nbsp;nbsp;int numbers[] = {3,5};
nbsp;nbsp;nbsp;nbsp;char* words[] = {"Fizz", "Buzz"};
nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp;bool printnumber;
nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp;for (i=1; imax; i++)
nbsp;nbsp;nbsp;nbsp;{
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;printnumber = true;
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;for (j=0; jsize; j++)
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;{
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;if (!(i% numbers[j]))
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;{
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;printnumber = false;
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;printf("%s", words[j]);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;}
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;}
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;if (printnumber)
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;printf("%d#92;n", i);
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;else
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;printf("#92;n");
nbsp;nbsp;nbsp;nbsp;}
nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;nbsp;nbsp;return 0;
}

/* now my comments:
Design patterns are for people that don't know lisp. They are all versions of the "universal pattern": the human compiler at work.
This is a good problem "because" it is short and can drive out bad programmers. More complex stuff takes more time you have in an interview.
And I coded this because people kept saying that there were no good answers. So at least one good answer should be posted.
*/

Nicolay77 on March 1, 2007 9:29 AM

# //alternating perl and java
# stairwaytoheaven.pl.java

$flag = exists ($ladywhoknows)
allThatGlitters.setColor(Color.gold)
she.buy("Stairway to Heaven");
if( she.getsThere() ) { she.knows(); }
foreach $c @store { @store[$c] = "closed"; }
if( she.oneword() ) { she.setCanGetWhatSheCameFor(true); }
# oh ohhohh
and she-buying("a stairway to heaven");
if( !she.wantsToBeSure() ) { boolean signOnTheWall = true; }
int sometimes = words.numOfMeanings();
assert sometimes = 2;

perljam on March 1, 2007 11:58 AM

Scott, you're so right. Those kick ass. I updated this article with one of the pics you selected:

http://www.codinghorror.com/blog/archives/000552.html

Jeff Atwood on March 1, 2007 12:55 PM

Hmm - Think I have an idea for a new killer video game here: "Programmer Hero". Start off with FizzBuzz, and progress up to The Traveling Salesman. Venues could be interview rooms at Google, MicroSoft, ZoomInfo (shameless plug).

Martin on March 2, 2007 2:03 AM

Code should be readable. If a programmer can't read, how can you expect that programmer to read what they just wrote in code? Good code is readable. Just like you want to be able to read the resume.

What surprises me is that even if I can write a FizzBuzz solution, it still hasn't guaranteed me a job with any employer that wants a programmer.

#include iostream

main()
{
for( int i = 1 ; i = 100 ; i ++ )
{
if( ( i % 3 ) ( i % 5 ) )
std::cout i std::endl ;
else
std::cout
( i % 3 ? "" : "Fizz" )
( i % 5 ? "" : "Buzz" )
std::endl ;
}
}

dzonatas on March 2, 2007 3:51 AM

Hmm... I found a feature in your blog in the above message of mine, as it left out some text in my insertion. Do you see where?

dzonatas on March 2, 2007 4:04 AM

I'm not even a programmer, and when I read the FizzBuzz thing, the code popped up in my head, in BASIC. Literally five seconds.

Peril on March 2, 2007 6:39 AM

Why, oh why, I seem to like the spider guitar ?

Nicolay77 on March 2, 2007 11:40 AM

After reading this, the thing that popped into my head was "people like solving little programming problems". So I made a blog for a daily dose of little programming projects that should be completable in just about any language (most require the ability to output text and manipulate numbers, so it does limit it to a certain level) and in a relatively short amount of time. Today is day #3 for me, and I have 3 projects up already that are about the size and scope of FizzBuzz. If you have any interest, click my name and check it out.

Jae on March 2, 2007 12:30 PM

I feel slightly dirty having just posted a blatant plug like that...

Jae on March 2, 2007 12:35 PM

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 6:10 AM

using System;
using System.Collections.Generic;
using System.Text;

namespace FizzBUzz
{
class Program
{
static void Main(string[] args)
{
for (int index = 1; index = 100; index++)
{
if (index % 3 == 0 index % 5 == 0)
{
// Print it out
Console.WriteLine("FizzBuzz");
}
else
{
if (index % 3 == 0)
{
// Print Fizz
Console.WriteLine("Fizz");
}
else
if (index % 5 == 0)
{
// Print Buzz
Console.WriteLine("Buzz");
}
}

if (index % 3 != 0 index % 5 != 0)
{
// Print it out
Console.WriteLine(index.ToString());
}
}
}
}
}

Jehanzeb on March 6, 2007 1:35 AM

In fact, I did not solve FissBuzz within 2 minutes. I immediately had a question: "Will the special outputs change often or are they constant forever?".
If they change often, I think, most solutions are not very good, because adding another special output - like "Qux" if number % {7, 23, 58} == 0 - will be a bit more difficult.

I'd rather create something like this:

function FizzBuzz(int Start, int End, Hashtable Specials) {
for(int Counter = Start; Counter End; Counter++) {
foreach(Specials as Constraints = Output) {
if(sum(map(fun(X) = Counter % X end)) == 0) print(Output);
} // foreach
} // for
} //function

(Pseudocode. Influenced by a lot of Stuff ;) )
Btw, this assumes A % B 0. Who sees why? :P

Of course, the sum(map(...)) can be replaced by foldl for more obscurity and efficience :)

Hk on March 6, 2007 5:01 AM

More comments»

The comments to this entry are closed.