52

FizzBuzz is a common whiteboard question for programming job interviews:

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

The standard answer has high readability and uses a loop. How would the interviewers take it if I had fun with this common question? For example, I write it all in one line, or obfuscate it with some crazy syntax? Would they think "That's hilarious! You're hired!" Or would they kick me to the curb for not showing I know how to write a loop?

This question was inspired by https://codereview.stackexchange.com/questions/49058/single-line-fizzbuzz-solution-in-linq and https://codegolf.stackexchange.com/questions/88/obfuscated-fizzbuzz-golf.

2

12 Answers 12

79

Would they think "That's hilarious! You're hired!" Or would they kick me to the curb for not showing I know how to write a loop?

Here's the problem with this thought - you cannot know how your interviewer will take such hilarity ahead of time.

Some employers will think you are amusing, witty, and will likely be fun to work with.

Others will think you are immature, unable to follow simple instructions, and not worth considering.

And there's no way to know what they will think until you try it.

We often think we are funnier and far more clever than we actually appear to others. So unless you are feeling very lucky, or unless you don't really want the job, why bother making such a bet when you don't have to?

Play it straight during interviews, and save the tricks and fun for after you are hired.

7
  • 18
    Some think you are amusing, and witty, and the employees of some other company deserve the fun of working with you :-)
    – gnasher729
    Apr 22, 2016 at 8:32
  • 6
    Not only these points, but obfuscated code is hard to read. If I had to review obfuscated code, I would throw it away. There is no way to evaluate it. However, using some tricks (like, for example, an operation that can reduce the code size, time and complexity) can be good and show that you dominate the language. Apr 22, 2016 at 9:00
  • 1
    @IsmaelMiguel In most languages used in production systems, such tricks rarely have a real-world use, are hard to read and a pain to maintain. Clean and minimalistic code is fine. XOR swaps and other shenanigans are not.
    – Lilienthal
    Apr 22, 2016 at 12:29
  • 2
    This answer is exactly correct. Their reaction will let you know everything you need to know about the company and whether it is a waste of your time to pursue employment with them.
    – rw-nandemo
    Apr 22, 2016 at 18:46
  • 1
    @IsmaelMiguel: to generalise what (I think) you're saying: the questioner mentions that it's easily done using a loop, and this is true. But some languages offer alternatives to explict loops that are not mere "tricks", they are strong alternative approaches. Using such approaches shouldn't fail the interview, provided the resulting code is "good", because if you're used to LINQ, or to functional programming, then a loop is a clumsy hack :-) There's still a difference between using a legit approach with no explicit loops, which might be fun, vs "having fun" by doing something mischeivous. Oct 20, 2016 at 10:00
24

I would ask the interviewer something like: "Do you want the kind of code I would write at work, or for fun?" and let them decide. Perhaps they ask to see both versions.

3
  • I think this is the best answer. In this way if they ask for the fun way and do not like it they can still ask for how you would do it at work. Apr 22, 2016 at 15:03
  • 1
    This would be my answer too. "Do you want clean and concise or fun and unreadable?" and I would take their response into account when thinking of joining. Was the response fun or did they seem strict? Apr 22, 2016 at 18:15
  • 2
    @Chad why would I want to employ somebody who thinks "working here is going to be boring, compared with the other stuff in my life"?
    – alephzero
    Apr 22, 2016 at 18:38
19

This reminds me of The Fizz Buzz from Outer Space. While this prospective employee thought he would be applauded for his out of the box thinking it actually highlighted worse traits. Aside from appearing cocky and pretentious it was also hard to understand and it left great debate to if he actually comprehended the task at hand. It quickly became apparent he wouldn't be a good fit for the job. Additionally a programming applicant should already know that the employer only expects to see a non-complicated solution that shows they are a practical programmer. Don't stray from the objective and expected result unless you are purposely looking to tank your interview.

9
  • You wouldn't even give an applicant a chance to explain their code and possibly give them a hint to do it in the "normal" way? This is why good programmers are hard to find.
    – user8365
    Apr 22, 2016 at 8:29
  • 14
    @JeffO: If programmers deliberately camouflage their skills, they should expect to be hard to find.
    – TMN
    Apr 22, 2016 at 14:26
  • 3
    @JeffO a coder who codes in the non "normal" way, generating code that is not robust, lacks in maintainability and flexibility because he's being overly "clever", is NOT a good coder to have in a production team. Normally, these coders are better as the "lone wolves", doing whole, smaller projects by themselves. Clever code is nice, but has its place, and a good coder will know when to revert to less fancy code. Someone shooting clever code for Fizzbuzz will generate some horribly hard (to maintain, read, etc) code when asked "hey, mind coding a quicksort here, we'll need it?".
    – Patrice
    Apr 22, 2016 at 14:56
  • 1
    @JeffO Ask yourself, would you want his code or strategies in your codebase? You go in for an interview you dress for the job you want. This is no different. Code for the job you want.
    – Bacon Brad
    Apr 22, 2016 at 17:38
  • 2
    @JeffO: There are different ways of being incompetent. Not understanding the purpose of a simple programming problem is one.
    – TMN
    May 2, 2016 at 11:16
13

There may be an option to show a clever solution to FizzBuzz or a variant. First show a really clean simple solution.

If the interviewers just nod and go on to the next thing, that's the end of the matter.

If they want to discuss your solution, you might be able to introduce a clever alternative, making it clear that it is just for fun and not something you would ever do in real code.

8

I was given fizzbuzz when interviewing for a linux systems job, because that's what their interviews for coders have. (This company employs a lot of coders)

So I answered it with a bash shell script rather than bluffing my way through some pseudo code. This meant my answer was relevant and useful to the position.

Answer: An interview is a chance to show that you are a good fit for the position, not a chance to show off. Prove that you can do the job properly, that's your task in the interview.

7

I'm afraid @paparazzi has it right. Nobody likes a smart-ass.

Your goal here is to show that you can write the prettiest, most maintainable, most efficient code possible; that's what they want to hire.

Express your annoyance with the task on your own time.

3
  • 4
    I'm not sure how this is an answer, as opposed to a comment on another answer.
    – Nic
    Apr 22, 2016 at 12:24
  • 1
    At the time, it was an answer based on a comment, which is legit. If you don't find it adds value, don't up-vote.
    – keshlam
    Apr 22, 2016 at 12:32
  • Oh, I see. I assumed that the post referenced was an answer, not a comment.
    – Nic
    Apr 22, 2016 at 16:04
5

Over the course of tens of screens using fizz buzz I longed for a candidate to solve it creatively. Programming is a creative profession, and a creative, non-bs solution to a common programming question can help you stand out. The bar is pretty low for fizz buzz, so doing something like golfing it is probably a waste, but there are solutions that strike a balance between the raw, hard-coded for loop and an arcane incantation.

Consider the ways fizz buzz is similar to other, more practical programming problems. Provide a means for the interviewer to alter the parameters of the question. Offer to write an alternative solution that fits with the job. What are the interfaces fizz buzz should have suitable for different programming paradigms?

Have fun being creative within the constraints of demonstrating you can do the job well. That's the kind of creativity we need in our industry.

7
  • 2
    Yeah, you may want to ask more creative questions, then. The only value in FizzBuzz is to weed out the ones that definitely won't cut it.
    – xxbbcc
    Apr 22, 2016 at 20:41
  • @xxbbcc Fizz buzz is useful for that, but I disagree that's its only value. Everyday we see blog posts complaining that challenging interview questions are equally stupid. Because many programmers don't face difficult programming problems, "reverse this tree!", "implement the Sieve of E!". Instead, they do CRUD operations. The good ones design good interfaces for stupid simple operations, because they know their idea of "obvious" isn't the same as the customer's, even if that customer is an API user. Fizz buzz lets me know if a good coder will take the "stupid" parts of the job seriously.
    – kojiro
    Apr 22, 2016 at 20:50
  • 1
    That's not my experience at all regarding FizzBuzz. It's a question that needs to be asked as bad as it is but it doesn't tell you much about what the candidate knows and how he/she works. I find problem solving, pointing out problem in code samples work better.
    – xxbbcc
    Apr 22, 2016 at 20:54
  • 1
    I'd say no, don't be creative with a question like FizzBuzz - no one is looking for creativity. Everyone knows what a bad question it is. If you want to see if a candidate is creative, ask about problems and multiple alternate solutions to see what variations the candidate can come up with. You don't need code at all for creativity. I personally prefer creative thinkers to creative coders.
    – xxbbcc
    Apr 22, 2016 at 20:59
  • 1
    I have a funny feeling we could carry on this conversation for a while and eventually discover we don't even agree on what "creative" actually means. Let's agree to disagree.
    – kojiro
    Apr 22, 2016 at 21:01
5

1) Say that you have seen FizzBuzz before.

2) Do the most straight forward solution.

3) Mention that you know about other witty solutions.

Some interviewers might be interested in them and some not (I would not be). The problem with going for a witty solution is that it almost guarantees that FizzBuzz will take more time than it should, and you might not get to or have less time to get other must get problems.

1
  • +1 for being honest and telling them you've seen FizzBuzz before. If an applicant is honest with me, I'm 10x more likely to recommend them for hire. Plus, if I knew someone knows it, I'll be more interested to see the interesting variants of the solution that they may also know. Apr 22, 2016 at 13:40
4

FizzBuzz is a very simple exercise that I hope isn't really that common.

If you look at the original blog post that described it, you see that it was invented for one purpose: check that people can program at all. There apparently is or was a problem with people coming to interviews for programming jobs who cannot write even a very simple routine from scratch, and it weeds them out.

Other than that, it is not interesting, so just get it over with and move on to the meat of the interview.

6
  • That is not the original post that described it. Apparently it is here (linked from the article you linked to): imranontech.com/2007/01/24/…
    – Brandin
    Apr 22, 2016 at 10:47
  • Tell me about it. We had a kid out of college who didn't know the difference between a sub and a function. Apr 22, 2016 at 14:45
  • 1
    @RichardU That's easy. One of them can launch nuclear missiles and destory North Korea. The other's a submarine.
    – Joe
    Apr 22, 2016 at 19:26
  • @RichardU: In what language? In Perl, for example, there is no difference. Apr 22, 2016 at 23:31
  • @KeithThompson in VB, VBA, a Function returns a value, a sub does not Apr 26, 2016 at 12:25
4

Interviews are for two purposes. First is to sell them on the idea of hiring you and second is for them to sell you on the idea of deciding to go for them if they give an offer. So think about anything you want to do in those terms, does this help sell me or does this help me weed out places I don't want to work?

Are you only willing to work in a place where they would think your creative answer is funny? It will take you much longer to find a job, but it might be one more suited to your personality.

Is this going to impress them? Well that depends, a good, solid, correct, boring answer is more likely to impress than a poorly thought out funny one. A well thought out but creative answer that is perfectly executed might make you stand out from the pack, but...

FizzBuzz is a weed-out question. It isn't to show off your high level skills or sense of humor, it is used by the interviewers to determine if this person has any skill at all. You would be amazed, if you have never done any interviewing, how many people can't actually solve it all. It's one of those questions that rarely helps your cause, only damages it. Sort of like the where do you see yourself in five years question or why did you you want to leave your current position question.

Because of this, personally I would find another way to show off that I have the creative chops or high level skills to stand out from the crowd. I would do it through showing my actual accomplishments and through being able to handle the real technical portion of the interview (not the weed out question) at a higher level than most people. These are the things that are the meat of the interview and where you should spend your time. Get FizzBuzz over and done with and then concentrate on the stuff that really shows who you are and what you can accomplish.

1
  • "show off your high level skills" I would draw an analogy: you're asked to put a piece of paper in the (recycling) bin. You can do it the normal way, or you can scrunch it up, aim your shot from the other side of the room, put on a blindfold and sink it. Supposing it comes off, you've demonstrated a skill you weren't asked to demonstrate, which could be good or neutral or occasionally bad. Supposing it doesn't come off, you've failed the simplest task imaginable, so you've failed the interview. "Clever" code is "clever" because that's what's needed to avoid mistakes when attempting it. Oct 20, 2016 at 10:11
1

I'd rather see you do something like write your own modulus function along with doing it the typical way. You would need to express the intent of your solution was to be able to come up with an alternative along with the notion you wouldn't attempt something like this with production code unless you could make a strong case for an obscure alternative.

Unfortunately, some people will read too much into this approach. Finding good programmers is difficult, but they insist on looking for any hint of a personality trait that would make you unfit for their corporate culture. That's a shame. If part of the culture is to take yourself too seriously, you may want to do this just to weed-out prospective jobs where they have no sense of humor.

0

I think it's inappropriate to use fizzbuzz to show that you are funny or clever.

I do think it can be appropriate to use fizzbuzz to show off more than your ability to write a loop.

  • You could write a fizzbuzz that is object oriented and extendable.

  • You could write a fizzbuzz that is functional.

  • You could write a fizzbuzz with optimization in mind.

  • You could write a fizzbuzz that includes tests.

  • You could write a fizzbuzz api.

  • You could write a git clone of an existing fizzbuzz (but afterwards write it yourself just to assure them you can code)

A code golf or obfuscated fizzbuzz would be pointless and perhaps disrespectful of the task. An interview is for demonstrating knowledge. Keep this in mind with any question or task given to you.

Not the answer you're looking for? Browse other questions tagged .