r/ProgrammerHumor •

[deleted by user]

[deleted] — view deleted post

0 Upvotes

446 comments sorted by

128

u/zebishop Jan 16 '14

What is really brilliant here, aside of the naive (yet perfect) answer, is the ordering of the results in columns that highlights the pattern of this algorithm

→ More replies (102)

149

u/novagenesis Jan 16 '14

What does an employer expect when they ask the FizzBuzz question in a way only completely unambiguous to someone who knows the FizzBuzz question?

Why not just say "hey, can you do fizzbuzz?"

132

u/Innominate8 Jan 16 '14

For someone who has no idea what fizzbuzz is, what this person did is not an unreasonable interpretation of the instructions. Technical interviews are often full of arbitrary seemingly unrelated questions brought on by interviewers who think they're being clever so it's hard to figure out what they actually want out of it.

It only looks stupid when you know what fizzbuzz is and fill the unwritten instructions in mentally. So in the end it's pretty much asking "Do you know what fizzbuzz is well enough to fill in the missing instructions?"

54

u/TheRingshifter Jan 16 '14

To be completely fair, this interpretation is the only reasonable interpretation of what the question asks... if you take it literally. If you write out some code on a piece of paper that code is never going to actually "print" anything. But writing the numbers out is printing them... the question should really just be worded:

"Write pseudocode (or java or whatever) which will ... [etc]"

It took me a good minute or two to figure out what he did wrong and I'm pretty sure I've heard of fizzbuzz at least in passing...

24

u/Xaquseg Jan 16 '14

Technically what's written there is already a form of pseudocode, I think the person writing the answer interpreted the question as "evaluate this pseudocode".

38

u/[deleted] Jan 16 '14

Yeah, honestly I had to come in to the comments to see why this was funny...I mean, I'm just learning to program and have obviously never had an interview, but were I presented with that piece of paper I wouldn't know what to do other than what the person in the picture did.

20

u/danillonunes Jan 17 '14

FizzBuzz is a common question asked by interviewers. The task is really easy and can be solved by good programmers in minutes, the purpose is only to filter out people who don’t have any clue of what they’re doing.

Generally, the task is: Write a program that prints the result shown in the image, but the instructions didn’t mention the "program" part, so OP read it literally and just wrote the expected result, as asked.

→ More replies (15)
→ More replies (21)
→ More replies (4)

17

u/JBlitzen Jan 16 '14

Even knowing it, I see those instructions as deliberate, as if they want what he wrote.

That or they outsourced the instructions to some $1/hr elance dude in India.

If I were that company's owner, I'd be way more disappointed by the interviewer than the candidate.

2

u/ZarinaShenanigans Jan 17 '14

Or more accurately, "do you know how to apply modular arithmetic to a simple programming problem?"

→ More replies (7)

48

u/matterball Jan 16 '14

None of these answers are going to make it in the Real World. Here's a reusable enterprise quality solution for this problem: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

10

u/robotmayo Jan 17 '14

Oh god the packages just never end.

7

u/jonnywoh Jan 17 '14

Now port the code to PHP line by line

→ More replies (1)

204

u/paranoid_twitch Jan 16 '14

This would be a whole lot funnier to me if we hadn't had like 30 people come though interviews like this. The number of people who apply for development jobs with no programming knowledge blows me away.

15

u/TheRingshifter Jan 16 '14

If it really bothers you so much why not just re-write the question so it's not ambiguous? To me, this shows nothing of a lack of programming knowledge. If it were not ambiguously worded, someone wouldn't even have to know what "fizzbuzz" is to work this out. It's really easy... just put "write code such that" in front of it...

→ More replies (8)

4

u/[deleted] Jan 16 '14

How do they get interviews though? Don't most employers want a Github link, experience, a relevant degree, or anything indicating that the applicant is familiar with what they're applying for?

4

u/paranoid_twitch Jan 16 '14

Donno, HR just hands me a stack and asks me to interview. I wish I had more insight to give you because that's probably the problem.

→ More replies (6)

4

u/jrk- Jan 16 '14

It would be fun if you could write that thing in asm. Maybe I should refresh my memory a bit (not enough memory in brain, only 640kb or so..)

6

u/Blecki Jan 16 '14

Yeah? You get tons of these people in interviews, and I, who have an extensive github history proving how capable I am, don't even get the interview...

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

96

u/didzisk Jan 16 '14

So, apparently the FizzBuzz programming interview task isn't common knowledge yet... After like what, 10 years?

OK, 7 years.

19

u/sandsmark Jan 16 '14

yes, sounds like a great idea to rely on people having heard about the interview questions before!

8

u/Excrubulent Jan 17 '14

The problem with common knowledge is that it's not as common as you'd think. Everyone has a first time learning something. Of course you've seen the relevant XKCD comic? Of course you have.

Also if it really were universally known, it would be entirely possible to get around it without knowing what you're doing by memorising a piece of code.

9

u/Charlemagne_III Jan 16 '14

I have never heard of this in the industry and we have a lot of tech companies here so I don't think it is that surprising.

→ More replies (20)

9

u/paranoid_twitch Jan 16 '14

That's understandable, I'm sure the interviewer had a good chuckle too. Hope it went well after that!

4

u/metamorphosis Jan 17 '14

in this case the prompt didn't say to write code,

Was it a development job?? Even if he is not familiar with FuzBuzz and even if the task didn't specifically said "write a code", the '"print" the numbers" is a give away of , mostly common, print functions and hence it assumes, print - using code. It didn't say "write numbers".

I understand how he got confused, but through half of the task he should've realized that something is wrong.

→ More replies (3)
→ More replies (3)

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

98

u/Lord_Naikon Jan 16 '14

Nope, you need to learn about % first :)

87

u/[deleted] Jan 16 '14

if(i == 1) print(i); else if(i == 2) print(i); else if (i == 3) print(Fizz);....

/s

29

u/[deleted] Jan 17 '14 edited Jan 17 '14

var _0xdd13=["\x6C\x6F\x67","\x46\x69\x7A\x7A","\x42\x75\x7A\x7A","\x46\x69\x7A\x7A\x42\x75\x7A\x7A"];for(i=0;i<=100;i++){if(i==1){console_0xdd13[0];} ;if(i==1){console_0xdd13[0];} ;if(i==2){console_0xdd13[0];} ;if(i==3){console_0xdd13[0];} ;if(i==4){console_0xdd13[0];} ;if(i==5){console_0xdd13[0];} ;if(i==6){console_0xdd13[0];} ;if(i==7){console_0xdd13[0];} ;if(i==8){console_0xdd13[0];} ;if(i==9){console_0xdd13[0];} ;if(i==10){console_0xdd13[0];} ;if(i==11){console_0xdd13[0];} ;if(i==12){console_0xdd13[0];} ;if(i==13){console_0xdd13[0];} ;if(i==14){console_0xdd13[0];} ;if(i==15){console_0xdd13[0];} ;if(i==16){console_0xdd13[0];} ;if(i==17){console_0xdd13[0];} ;if(i==18){console_0xdd13[0];} ;if(i==19){console_0xdd13[0];} ;if(i==20){console_0xdd13[0];} ;if(i==21){console_0xdd13[0];} ;if(i==22){console_0xdd13[0];} ;if(i==23){console_0xdd13[0];} ;if(i==24){console_0xdd13[0];} ;if(i==25){console_0xdd13[0];} ;if(i==26){console_0xdd13[0];} ;if(i==27){console_0xdd13[0];} ;if(i==28){console_0xdd13[0];} ;if(i==29){console_0xdd13[0];} ;if(i==30){console_0xdd13[0];} ;if(i==31){console_0xdd13[0];} ;if(i==32){console_0xdd13[0];} ;if(i==33){console_0xdd13[0];} ;if(i==34){console_0xdd13[0];} ;if(i==35){console_0xdd13[0];} ;if(i==36){console_0xdd13[0];} ;if(i==37){console_0xdd13[0];} ;if(i==38){console_0xdd13[0];} ;if(i==39){console_0xdd13[0];} ;if(i==40){console_0xdd13[0];} ;if(i==41){console_0xdd13[0];} ;if(i==42){console_0xdd13[0];} ;if(i==43){console_0xdd13[0];} ;if(i==44){console_0xdd13[0];} ;if(i==45){console_0xdd13[0];} ;if(i==46){console_0xdd13[0];} ;if(i==47){console_0xdd13[0];} ;if(i==48){console_0xdd13[0];} ;if(i==49){console_0xdd13[0];} ;if(i==50){console_0xdd13[0];} ;if(i==51){console_0xdd13[0];} ;if(i==52){console_0xdd13[0];} ;if(i==53){console_0xdd13[0];} ;if(i==54){console_0xdd13[0];} ;if(i==55){console_0xdd13[0];} ;if(i==56){console_0xdd13[0];} ;if(i==57){console_0xdd13[0];} ;if(i==58){console_0xdd13[0];} ;if(i==59){console_0xdd13[0];} ;if(i==60){console_0xdd13[0];} ;if(i==61){console_0xdd13[0];} ;if(i==62){console_0xdd13[0];} ;if(i==63){console_0xdd13[0];} ;if(i==64){console_0xdd13[0];} ;if(i==65){console_0xdd13[0];} ;if(i==66){console_0xdd13[0];} ;if(i==67){console_0xdd13[0];} ;if(i==68){console_0xdd13[0];} ;if(i==69){console_0xdd13[0];} ;if(i==70){console_0xdd13[0];} ;if(i==71){console_0xdd13[0];} ;if(i==72){console_0xdd13[0];} ;if(i==73){console_0xdd13[0];} ;if(i==74){console_0xdd13[0];} ;if(i==75){console_0xdd13[0];} ;if(i==76){console_0xdd13[0];} ;if(i==77){console_0xdd13[0];} ;if(i==78){console_0xdd13[0];} ;if(i==79){console_0xdd13[0];} ;if(i==80){console_0xdd13[0];} ;if(i==81){console_0xdd13[0];} ;if(i==82){console_0xdd13[0];} ;if(i==83){console_0xdd13[0];} ;if(i==84){console_0xdd13[0];} ;if(i==85){console_0xdd13[0];} ;if(i==86){console_0xdd13[0];} ;if(i==87){console_0xdd13[0];} ;if(i==88){console_0xdd13[0];} ;if(i==89){console_0xdd13[0];} ;if(i==90){console_0xdd13[0];} ;if(i==91){console_0xdd13[0];} ;if(i==92){console_0xdd13[0];} ;if(i==93){console_0xdd13[0];} ;if(i==94){console_0xdd13[0];} ;if(i==95){console_0xdd13[0];} ;if(i==96){console_0xdd13[0];} ;if(i==97){console_0xdd13[0];} ;if(i==98){console_0xdd13[0];} ;if(i==99){console_0xdd13[0];} ;if(i==100){console_0xdd13[0];} ;} ;

→ More replies (1)

11

u/Gbyrd99 Jan 16 '14

You know what they say the more lines of code, the more impressive you look.

→ More replies (2)
→ More replies (8)

25

u/decerian Jan 16 '14

Ha. You can't fool me with those percent signs.

41

u/billy_tables Jan 16 '14

Modern programmers don't use % because jQuery can do that for you

31

u/berkes Jan 16 '14

Just search the web for a FizzBuzz jQuery plugin, then spend the rest of the day trial-and-erroring your way through all the settings for that plugin, so it matches the 3 and 5 requirements... good enough.

→ More replies (5)

12

u/atrain728 Jan 16 '14

As a modern programmer, I still like to modulo.

→ More replies (1)
→ More replies (2)

11

u/8lbIceBag Jan 17 '14 edited Jan 18 '14

Can I haz Job???

http://i.imgur.com/2XwMeX1.png

EDIT: For the hell of it, I went ahead and did a low level C implementation for an arduino uno. Except I didn't use their libraries and it's mostly from scratch.

http://i.imgur.com/eqhxXYV.png

→ More replies (6)

7

u/jambonilton Jan 16 '14
if (i/3*3 == i)
    print "fizz";
→ More replies (2)
→ More replies (9)
→ More replies (1)

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

29

u/paranoid_twitch Jan 16 '14

If they aren't in programming jobs that's fine. I know some really good network engineers that couldn't write a line code to save their lives. That's what we are here for.

25

u/G01denW01f11 Jan 16 '14

To save network engineers' lives?

→ More replies (7)
→ More replies (5)

103

u/tohryu Jan 16 '14 edited Jan 17 '14

I had to come to the comments to understand why this was wrong and what it had to do with computing.

Nowhere on the page that I can see does it say to write an algorithm to do it, it literally says"do this". I would have done the same thing and thought it was the most pointless exercise ever.

26

u/digitalpencil Jan 16 '14

In all fairness, if it was a programming job and had I never heard of fizzbuzz, I would have assumed that they wanted pseudo-code simply from context and the use of the term 'print'.

Would have been better if they actually requested pseudo-code though as this is a literal interpretation and not technically incorrect.

13

u/Massless Jan 16 '14

Understandable, to a point, but you should probably read a hiring blog.

25

u/Talran Jan 16 '14

Oh? Good source for similarly vaguely worded interview questions?

I rather prefer the more direct approach.

11

u/Massless Jan 16 '14

Nah, not interview questions. Instead, a peek inside the heads of hiring managers so your job search can be more efficient. Fizz Buzz comes from the coding horror blog which tends to be informative and entertaining. I've found that there's rarely a direct approach when looking for a new position.

→ More replies (18)

2

u/Kinglink Jan 16 '14

If you're getting hired for a programming job I'd assume you (and I ) would write an algorithm...

But also this answer should get some points because it is valid... stupid but valid.

→ More replies (1)

17

u/ehmpsy_laffs Jan 16 '14

A project manager is born.

8

u/katyne Jan 16 '14

guise... you guise... monoids

no srsly

→ More replies (2)

7

u/thomplinds Jan 16 '14

had to answer this question for my first ever professional job, this is extremely entertaining

7

u/rohanivey Jan 17 '14

My friend looked over my shoulder at this, "What language did they want it in?"

"I'm guessing not English."

6

u/[deleted] Jan 21 '14

Is this seriously an interview question? I'm a 16 year old with little/no Java experience and this looks way too easy. For loop, if/else if/else statement, modulo operator, done.

8

u/tworing Jan 21 '14

This sort of question is routinely used during interviews as a first question because when you have an open programmer position you get a staggering number of applicants who do not actually know how to program. So by asking a question like this and immediately disqualifying anyone who can't write a reasonable solution to it within ~5 minutes, you save a ton of time by not having to actually interview people who had no business applying in the first place.

2

u/[deleted] Jan 21 '14

OK, makes sense! Thanks!

So what you're telling me is, I'm ahead of the majority of programmer applicants simply by knowing basic Java?

→ More replies (4)

24

u/djimbob Jan 16 '14

While their answer perfectly follows the vague instructions, it shows the candidate failed this part of the interview.

Why? Because before blindly doing the task, they didn't think about the larger context (in a programming interview, no one cares about your ability to act out non-code instructions), what was more likely to have been meant (even if it wasn't specifically specified), and didn't ask any clarifying questions. In the real world, instructions will be vague more often then not.

(Granted, you can fail one part of an interview and potentially still get a job offer, especially if you do very well on other aspects).

1

u/kqr Jan 16 '14

I don't think anyone disputes they failed this part of the interview.

15

u/[deleted] Jan 16 '14

I do. The question was a train wreck. Being able to answer that would be indicative of how many interviews someone has had, nothing more.

→ More replies (3)
→ More replies (10)
→ More replies (1)

4

u/Kazinsal Jan 16 '14

My favourite way to do FizzBuzz involves nesting ternary operators.

A lot of people don't even think it's real code.

→ More replies (2)

4

u/martinmine Jan 17 '14
for (int i = 1; i <= 100; i++)
{
	if (i % 3 == 0 && i % 5 == 0) print("FizzBuzz");
	else if (i % 3 == 0)          print("Fizz");
	else if (i % 5 == 0)          print("Buzz");
	else print(i);
}
→ More replies (1)

7

u/[deleted] Jan 16 '14

Programming Student early in academic career who has never seen this test or heard of it. Am I being stupid to think just written else if in between the lines of instructions isn't sufficient enough? I assume because he didn't write code the tester gave him free will to write what he thought answer was and wouldn't it of just been easier or ok to show pseudo?

2

u/ActionScripter9109 my old code = timeless gems, theirs = legacy trash Jan 16 '14

Writing "else if" between the lines would not work... in this order. Consider the case where the number is 45. The first test would evaluate to true and it would print "Fizz", yet it should have printed "FizzBuzz".

The proper else-if based approach would be to test the 3+5 case first, then hit 3 and 5 separately with else-ifs.

Another strategy is to check 3 then 5 with if statements and not worry about 3+5. If a number is divisible by both, "FizzBuzz" gets printed since they both triggered. The issue there, of course, is that you have to put the newline at the end of the loop block, not in the print statements.

I had to complete this question a couple times in my interviews fresh out of university, so I've given it a fair bit of thought.

4

u/[deleted] Jan 16 '14

After reading your explanation then rereading problem I completely see where I went wrong. Thank you for explanation and even showing two separate ways.

→ More replies (1)

10

u/KBKarma Jan 16 '14

I decided to see whether I could still do this. After a slight issue with having the mod 3 catch before the mod 3 && mod 5, I made this in Python really quickly. I've never done FizzBuzz in Python.

    for elem in range(1, 101):
            if elem % 3 == 0:
                    if elem % 5 == 0:
                            print "FizzBuzz"
                    else:
                            print "Fizz"
            elif elem % 5 == 0:
                    print "Buzz"
            else:
                    print elem

33

u/Kautiontape Jan 16 '14

You could make it a little more streamlined, by taking advantage of "FizzBuzz" being the same as "Fizz" + "Buzz"

for num in range(1, 101):
    p = ''
    if num % 3 == 0:
        p += "Fizz"
    if num % 5 == 0:
        p += "Buzz"
    print p or num

It's a little simpler, and more Pythonic

15

u/mcvoid1 Jan 16 '14 edited Jan 16 '14

Here's my $0.019999999...

(->> (range 1 101)  ; start at 1? Bunch of savages in this town...
     (map (fn [x]
       (cond (zero? (mod x 15)) "FizzBuzz"
             (zero? (mod x 3)) "Fizz"
             (zero? (mod x 5)) "Buzz"
             :else x)))
     (map println))

7

u/katyne Jan 16 '14

i might be getting old but in my day kids got smacked for being smartasses.

...unless you're even older and are being serious. In that case, with all due respect sir.

→ More replies (1)
→ More replies (2)

4

u/kqr Jan 16 '14
print p or num

This clever trick doesn't look very Pythonic to me. Do you have links to precedents or something?

3

u/Kautiontape Jan 16 '14

I tried finding you a good authoritative source using it (such as PEP 8), but haven't been able to find one. Otherwise, The Zen of Python encourages flat coding over nested coding, and in general, it is good Python tradition to avoid being needlessly superfluous if it makes sense to condense code. This obviously fits the bill, since it saves you an if/else statement in favor of a simple Boolean statement. I could dig around and see if there's any trusted Python code that does this if I had time.

At the very least, it's existed on [StackOverflow for years] (http://stackoverflow.com/a/1338532/586933) and a book tutorial.

5

u/kqr Jan 16 '14 edited Jan 16 '14

The more I look at it, the more I feel like accepting it as okay style, but it still looks weird to me. It makes so many assumptions about truthish values and short-circuiting behaviour and the return value of boolean operators so on. I mean, you need the empty string to be falseish, you need or to short-circuit and you need or to return the value of the first truthish argument – otherwise the last one. Maybe I'm just not used to it.

Edit: I just asked a bunch of Python programming friends and they all instinctively said, "Go for the or expression!" but when they looked at it closer they started to question whether they even gave the same result. While they do give the same result, I think their doubt speaks for avoiding it in production code.

→ More replies (4)
→ More replies (3)

3

u/seiyria Jan 16 '14

This inspired me to make a horribly contrived version in JS:

function isFizz(i)      { return i%divisors.FIZZ === 0; }
function isBuzz(i)      { return i%divisors.BUZZ === 0; }
function isFizzBuzz(i)  { return isFizz(i) && isBuzz(i); }

var divisors = {
  FIZZ: 3,
  BUZZ: 5
};

var functionsAndResults = [
  { function: isFizzBuzz, msg: "FizzBuzz" },
  { function: isFizz, msg: "Fizz" },
  { function: isBuzz, msg: "Buzz" },
];

for(var i=1; i<=100; i++) {
  
  var calledFunction = false;
  
  for(var func=0; func<functionsAndResults.length; func++) {
    if(functionsAndResults[func].function(i)) {
      console.log(functionsAndResults[func].msg);
      calledFunction = true;
      break;
    }
  }
  
  if(!calledFunction)
    console.log(i);
}

Had I been doing more CoffeeScript at the time, this could be a lot more elegant. C'est la vie.

(See also: Enterprise Fizz Buzz)

1

u/thomplinds Jan 16 '14

the only time i've done it i did it in python, but that was almost 4 years ago and i can read what you've written, but unsure if i could have written it myself anymore :/

1

u/didzisk Jan 16 '14 edited Jan 16 '14

I misunderstood the question and tried doing this without division, but it grew convoluted really quickly (save all states and keep track of them)... and then I got bored. Happened several years ago, so I have never in my life completed FizzBuzz.

Edit: Forget it, now I have. And why?

1

u/glaslong Jan 16 '14

In case you haven't seen it before, you should check out /r/dailyprogrammer. They post a problem of roughly this difficulty every Monday. It's pretty fun and good practice.

→ More replies (1)

3

u/takatori Jan 17 '14

It's a trick question: they were checking to see if he would use cursive instead of printing.

8

u/Chemical_Scum Jan 16 '14

dick stuck in FizzBuzz?

2

u/[deleted] Jan 16 '14

They didn't specify a language, was i supposed to write it in pseudo code?

1

u/Sargeron Jan 16 '14

I assume they'd let you do it in any language you choose. The intent behind this (I assume) is to test your logical problem solving using code, rather than proficiency in any given language.

2

u/[deleted] Jan 16 '14

Ah, good to know. I'm in computer science in college so that will be important to know when me getting a job is relevent.

→ More replies (3)

2

u/[deleted] Jan 16 '14

I am so lost. What is this? It looks like they did it right to me.

2

u/Sargeron Jan 16 '14

While FizzBuzz originally was intended to be done this way to teach children about division, it has been widely adopted by interviewers to test an applicant's programming skills. The asssumption made by the interviewer in OP's friend's situation is that the friend is aware of this and thus wouldn't need to be told that he needs to solve it using code.

Source

3

u/autowikibot Jan 17 '14

Here's the linked section Other uses from Wikipedia article Fizz buzz :


Fizz buzz has been used as an interview screening device for computer programmers. Creating a list of the first 100 Fizz buzz numbers is a trivial problem for any would-be computer programmer, so interviewers can easily sort out those with insufficient programming ability.

Adults may play Fizz buzz as a drinking game, where making a mistake leads to the player having to make a drinking-related forfeit.


about | /u/Sargeron can reply with 'delete'. Will also delete if comment's score is -1 or less. | To summon: wikibot, what is something?

→ More replies (2)

2

u/Xiver1972 Jan 17 '14

There are so many ways to answer this question...

for(int apple = 1; apple < 101; ++apple){
	switch(apple % 15){
		case 0:
			std::cout << "FizzBuzz";
			break;
		case 3:
		case 6:
		case 9:
		case 12:
			std::cout << "Fizz";
			break;
		case 5:
			std::cout << "Buzz";
			break;
		default:
			std::cout << apple;
			break;
	}
	std::cout << std::endl;
}

1

u/Xiver1972 Jan 17 '14
bool bMod3;
for(int apple = 1; apple < 101; ++apple){
	bMod3 = (apple % 3) == 0;
	if(bMod3){
		std::cout << "Fizz";
	}
	if((apple % 5) == 0){
		std::cout << "Buzz";
	}else if(!bMod3){
		std::cout << apple;
	}
	std::cout << std::endl;		
}	

4

u/thereddarren Jan 16 '14
		for (int i = 1; i < 101; i++)
		{
			bool divisibleBy3 = i % 3 == 0;
			bool divisibleBy5 = i % 5 == 0;

			if (divisibleBy3)
			{
				Console.Write("Fizz");
			}
			if (divisibleBy5)
			{
				Console.Write("Buzz");
			}
			else if (!divisibleBy3 && !divisibleBy5)
			{
				Console.Write(i);
			}
			Console.WriteLine("");//New Line
		}

And now I wait for smarter people to tell me what's wrong with it.

3

u/hbdgas Jan 16 '14 edited Jan 16 '14

Since the else if is following if (divisibleBy5), you don't really need to check !divisibleBy5 in it.

Edit: Also, since after making that change you'd then only be using divisibleBy5 once, you might as well just put the calculation directly into the if statement to free up that variable. But yours is really fine like it is for readability.

2

u/peclo Jan 16 '14

I like that version better. Easy to read, easy to extend.

1

u/[deleted] Jan 16 '14

braces

1

u/[deleted] Jan 16 '14

[removed] — view removed comment

20

u/thespacebaronmonkey Jan 16 '14

or not to verbose... that is the question

→ More replies (2)

3

u/pirate_platypus Jan 16 '14

Since other people have tossed in their version, here's my submission for the silliest way to do it (Python 3)

[("FizzBuzz" if x % 15 == 0 else ("Buzz" if x % 5 == 0 else ("Fizz" if x % 3 == 0 else x))) for x in range(1, 101)]

3

u/VanFailin Jan 16 '14

There are a few python features that make it sillier. Try:

print('\n'.join((i % 3 == 0) * 'Fizz' + (i % 5 == 0) * 'Buzz' + (i % 3 != 0) * (i % 5 != 0) * str(i) for i in range(1, 101)))

2

u/hbwales Jan 17 '14

how about:

print('\n'.join((i % 3 == 0) * 'Fizz' + (i % 5 == 0) * 'Buzz' or str(i) for i in range(1, 101)))

2

u/thirdegree Violet security clearance Jan 17 '14

Things like this make me with it was valid python to use ! as a not operator :(

→ More replies (2)

2

u/DemiReticent Jan 16 '14

I've never seen this problem and I could immediately tell that the intent was to have the person write code to do it. I mean, I would write a program anyway. Also, the use of the word 'print'. Am I the only one?

4

u/FeepingCreature Jan 17 '14

You're not in an interview situation. You're on Reddit.

2

u/cypr Jan 16 '14

You know those times during an interview when you get a problem that you can easily solve and therefor get relaxed and decide to initially break a related joke, try getting a smile from the interviewer, show some social skills and that you grasp not only the direct problem but also several different solutions and possible pitfalls.

Yeah, I don't think this is such a time.

1

u/Sakuya_Lv9 Jan 16 '14

I got this:

12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz3132Fizz34BuzzFizz3738FizzBuzz41Fizz4344FizzBuzz4647Fizz49BuzzFizz5253FizzBuzz56Fizz5859FizzBuzz6162Fizz64BuzzFizz6768FizzBuzz71Fizz7374FizzBuzz7677Fizz79BuzzFizz8283FizzBuzz86Fizz8889FizzBuzz9192Fizz94BuzzFizz9798FizzBuzz

(hint: not println)

1

u/[deleted] Jan 16 '14

Q

1

u/Kinglink Jan 16 '14

This is clever. Actually there was a problem where someone wanted to know if all the numbers from 1 to some large number was Prime. the large number was under 100,000

So I wrote the program, and then left a second answer. I used the first program to generate the answers into a file, used that output as a array, (depending the number they pushed, I would output X of the array up to that number) and just posted that as my answer. Explaining the answer (Just looking up the first 100,000 values is faster than the sieve of eratosthenes.)

The question emphasized speed over algorithm, but I forgot what happened with the interview so I have no idea if that was the better answer.

1

u/kqr Jan 17 '14

Actually there was a problem where someone wanted to know if all the numbers from 1 to some large number was Prime.

putStrLn (if largeNumber > 3 then "No" else "Yes")

...going by the literal interpretation again.

1

u/Kinglink Jan 17 '14

On the sheet it actually said the number I forget it ( I want to say 100,000) but that's a good answer.

1

u/acfman17 Jan 17 '14

Rosetta Code has how to do it in over 180 languages. Everybody should be able to do this.

1

u/Rikkety Jan 17 '14

To be fair, this pseudo-code is by far the fastest implementation. It doesn't have the overhead of a loop, nor the expensive modulo and comparison operations.

If the question was to optimize for speed, this is exactly how I would do it (except maybe a "printf" or something similar preceding each line.

1

u/mm94 Jan 17 '14

am I missing something?

1

u/balkonkind Jan 17 '14

needs more design patterns:

var textMap = [
    { division: 3, text: 'Fizz' },
    { division: 5, text: 'Buzz' },
];

for (var number = 1; number <= 100; number++) {
    
    var isDividable = false;
    for (var i = 0; i < textMap.length; i++) {
        if (number % textMap[i].division == 0) {
            isDividable = true;
            break;
        }    
    }
    
    if (isDividable) {
        var consoleText = '';
        for (var i = 0; i < textMap.length; i++) {
            if (number % textMap[i].division == 0) {
                consoleText += textMap[i].text;
            }
        }
        console.log(consoleText);
    } else {
        console.log(number);   
    }

}

1

u/mcdileo Jan 18 '14 edited Jan 18 '14

Just because:

public void main()
{
    FizzTheBuzz(["Fizz", "Buzz", "FizzBuzz"], [3, 5, 15], 100);
}

/// <summary>
/// Takes a collection of strings and ints.
/// If any number below count is divisible by any of the conditions, it outputs
/// the matching text in the output array. The length of output and conditions
/// must be the same.
/// </summary>
/// <param name="outputs">What to say if there's a match</param>
/// <param name="conditions">On what numbers do you want to say an output?</param>
/// <param name="count">How high to you want to count</param>
public void FizzTheBuzz(string[] outputs, int[]conditions, int count)
{
    if (output.Length != conditions.Length)
    {
	    Console.WriteLine("The lengths of outputs and conditions don't match.");
	    return;
    }

    for (int i = 0; i < count; i++)
	    for (int i1 = 0; i1 < conditions.Length; i1++)
		    Console.WriteLine((i % conditions[i1] == 0) ? outputs[i1] : i);	
}

EDIT: what is wrong with me? Maybe it's just because it's Saturday and I don't want to do work.

1

u/nicholas818 Jan 20 '14 edited May 02 '14

+/u/CompileBot python

n = 1
while n<=100:
	fizz = n % 3 == 0
	buzz = n % 5 == 0
	if fizz and buzz:
		print "FizzBuzz"
	elif fizz:
		print "Fizz"
	elif buzz:
		print "Buzz"
	else:
		print n
	n += 1

1

u/CompileBot Green security clearance May 02 '14

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
...

^source | ^info | ^git | ^report

1

u/Coopsmoss Jan 16 '14

I've never heard of it, is this a serious programming interview question? Why am I'm unemployed, I'm practically a programming god comparatively.

0

u/[deleted] Jan 17 '14

FizzBuzz Program:

+/u/CompileBot C#

for(int i = 1; i <= 100; i++)
{
     if (i % 3 == 0) System.Console.WriteLine("Fizz");
     if (i % 5 == 0) System.Console.WriteLine("Buzz");
     if (i % 3 == 0 && i % 5 == 0) System.Console.WriteLine("FizzBuzz");
 }
 Console.ReadLine();

2

u/[deleted] Jan 19 '14

if (i % 3 == 0 && i % 5 == 0) System.Console.WriteLine("FizzBuzz");

Wouldn't

if (i %15 == 0) System.Console.WriteLine("FizzBuzz");

be better for this ?

1

u/[deleted] Jan 20 '14

The read out is the same thing and unless you're running this calculation one hundred thousand times per millisecond, I'm not sure it would make big difference other than the few characters you saved typing. In which case, it might also be better not to use "System.Console.Writeline()" 3 times but instead assign a string variable string s; [...] s = "Fizz" etc, [...]WriteLine(s); etc etc. for a net savings in typed characters.

-11

u/[deleted] Jan 16 '14

This solution would be easier if it was written like this:

1 2 3 4 FIZZ 6 7 8 9 FIZZ 11 12 13 14 FIZZBUZZ 16 17 18 19 FIZZ

Then just repeat 5 times.

14

u/djimbob Jan 16 '14
1 2 3 4 FIZZ 6 7 8 9 FIZZ 11 12 13 14 FIZZBUZZ 16 17 18 19 FIZZ

Then just repeat 5 times.

Wow. Fail at cheating FizzBuzz.

  1. Fizz is for divisibility by 3, not 5.
  2. You didn't do anything for divisibility for 3.
  3. You need to repeat on a multiple of 15/30 to have the pattern line up nicely.

-7

u/[deleted] Jan 16 '14 edited Jan 16 '14

Sorry about that...

1 2 FIZZ 4 BUZZ FIZZ 7 8 FIZZ BUZZ 11 FIZZ 13 14 FIZZBUZZ 16 17 18 19 FIZZ BUZZ

5

u/djimbob Jan 16 '14

There's no 13 and 19?

7

u/G01denW01f11 Jan 16 '14

And this why we have computers.

→ More replies (2)

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

48

u/NakedJeff Jan 17 '14

The single use of printf(...) instead of System.out.println(...) for when i == 90 is really clutch. So many interview candidates miss this!

→ More replies (1)

91

u/CptPoo Jan 16 '14

Did you write a program to output this text? If so, you're hired!

180

u/danillonunes Jan 17 '14

Well...

<?php
$text = <<<FIZZBUZZ
		int i = 1;
		while (i <= 100) {
			if (i == 1) {
				System.out.println("1");
			} else if (i == 2) {
				System.out.println("2");
			} else if (i == 3) {
				System.out.println("FIZZ");
			} else if (i == 4) {
				System.out.println("4");
			} else if (i == 5) {
				System.out.println("BUZZ");
			} else if (i == 6) {
				System.out.println("FIZZ");
			} else if (i == 7) {
				System.out.println("7");
			} else if (i == 8) {
				System.out.println("8");
			} else if (i == 9) {
				System.out.println("FIZZ");
			} else if (i == 10) {
				System.out.println("BUZZ");
			} else if (i == 11) {
				System.out.println("11");
			} else if (i == 12) {
				System.out.println("FIZZ");
			} else if (i == 13) {
				System.out.println("13");
			} else if (i == 14) {
				System.out.println("14");
			} else if (i == 15) {
				System.out.println("FIZZBUZZ");
			} else if (i == 16) {
				System.out.println("16");
			} else if (i == 17) {
				System.out.println("17");
			} else if (i == 18) {
				System.out.println("FIZZ");
			} else if (i == 19) {
				System.out.println("19");
			} else if (i == 20) {
				System.out.println("BUZZ");
			} else if (i == 21) {
				System.out.println("FIZZ");
			} else if (i == 22) {
				System.out.println("22");
			} else if (i == 23) {
				System.out.println("23");
			} else if (i == 24) {
				System.out.println("FIZZ");
			} else if (i == 25) {
				System.out.println("BUZZ");
			} else if (i == 26) {
				System.out.println("26");
			} else if (i == 27) {
				System.out.println("FIZZ");
			} else if (i == 28) {
				System.out.println("28");
			} else if (i == 29) {
				System.out.println("29");
			} else if (i == 30) {
				System.out.println("FIZZBUZZ");
			} else if (i == 31) {
				System.out.println("31");
			} else if (i == 32) {
				System.out.println("32");
			} else if (i == 33) {
				System.out.println("FIZZ");
			} else if (i == 34) {
				System.out.println("34");
			} else if (i == 35) {
				System.out.println("BUZZ");
			} else if (i == 36) {
				System.out.println("FIZZ");
			} else if (i == 37) {
				System.out.println("37");
			} else if (i == 38) {
				System.out.println("38");
			} else if (i == 39) {
				System.out.println("FIZZ");
			} else if (i == 40) {
				System.out.println("BUZZ");
			} else if (i == 41) {
				System.out.println("41");
			} else if (i == 42) {
				System.out.println("FIZZ");
			} else if (i == 43) {
				System.out.println("43");
			} else if (i == 44) {
				System.out.println("44");
			} else if (i == 45) {
				System.out.println("FIZZBUZZ");
			} else if (i == 46) {
				System.out.println("46");
			} else if (i == 47) {
				System.out.println("47");
			} else if (i == 48) {
				System.out.println("FIZZ");
			} else if (i == 49) {
				System.out.println("49");
			} else if (i == 50) {
				System.out.println("BUZZ");
			} else if (i == 51) {
				System.out.println("FIZZ");
			} else if (i == 52) {
				System.out.println("52");
			} else if (i == 53) {
				System.out.println("53");
			} else if (i == 54) {
				System.out.println("FIZZ");
			} else if (i == 55) {
				System.out.println("BUZZ");
			} else if (i == 56) {
				System.out.println("56");
			} else if (i == 57) {
				System.out.println("FIZZ");
			} else if (i == 58) {
				System.out.println("58");
			} else if (i == 59) {
				System.out.println("59");
			} else if (i == 60) {
				System.out.println("FIZZBUZZ");
			} else if (i == 61) {
				System.out.println("61");
			} else if (i == 62) {
				System.out.println("62");
			} else if (i == 63) {
				System.err.println("FIZZ");
			} else if (i == 64) {
				System.out.println("64");
			} else if (i == 65) {
				System.out.println("BUZZ");
			} else if (i == 66) {
				System.out.println("FIZZ");
			} else if (i == 67) {
				System.out.println("67");
			} else if (i == 68) {
				System.out.println("68");
			} else if (i == 69) {
				System.out.println("FIZZ");
			} else if (i == 70) {
				System.out.println("BUZZ");
			} else if (i == 71) {
				System.out.println("71");
			} else if (i == 72) {
				System.out.println("FIZZ");
			} else if (i == 73) {
				System.out.println("73");
			} else if (i == 74) {
				System.out.println("74");
			} else if (i == 75) {
				System.out.println("FIZZBUZZ");
			} else if (i == 76) {
				System.out.println("76");
			} else if (i == 77) {
				System.out.println("77");
			} else if (i == 78) {
				System.out.println("FIZZ");
			} else if (i == 79) {
				System.out.println("79");
			} else if (i == 80) {
				System.out.println("BUZZ");
			} else if (i == 81) {
				System.out.println("FIZZ");
			} else if (i == 82) {
				System.out.println("82");
			} else if (i == 83) {
				System.out.println("83");
			} else if (i == 84) {
				System.out.println("FIZZ");
			} else if (i == 85) {
				System.out.println("BUZZ");
			} else if (i == 86) {
				System.out.println("86");
			} else if (i == 87) {
				System.out.println("FIZZ");
			} else if (i == 88) {
				System.out.println("88");
			} else if (i == 89) {
				System.out.println("89");
			} else if (i == 90) {
				printf("FIZZBUZZ");
			} else if (i == 91) {
				System.out.println("91");
			} else if (i == 92) {
				System.out.println("92");
			} else if (i == 93) {
				System.out.println("FIZZ");
			} else if (i == 94) {
				System.out.println("94");
			} else if (i == 95) {
				System.out.println("BUZZ");
			} else if (i == 96) {
				System.out.println("FIZZ");
			} else if (i == 97) {
				System.out.println("97");
			} else if (i == 98) {
				System.out.println("98");
			} else if (i == 99) {
				System.out.println("FIZZ");
			} else if (i == 100) {
				System.out.println("BUZZ");
			}
			i += 1;
		}
FIZZBUZZ;
echo $text;
?>

5

u/[deleted] Apr 02 '14

(vomits into hands)

1

u/CptPoo Jan 17 '14

I see what you did there. You're a sly one.

0

u/[deleted] Jan 17 '14

[deleted] — view deleted comment

64

u/peabnuts123 Jan 17 '14

And our circle is complete.

for i=1,26 do
	if i==1 then print("<?php") end
	if i==2 then print("printf(\"<?php\n\");") end
	if i==3 then print("printf(\"\$text = <<<FIZZBUZZ\n\");") end
	if i==4 then print("printf(\"\t\ti += 1;\n\");") end
	if i==5 then print("printf(\"\t\twhile (i <= 100) {\n\");") end
	if i==6 then print("for($i = 1; $i <= 100; $i++){") end
	if i==7 then print("\t$res = \"\";") end
	if i==8 then print("\tif(!($i % 3) || !($i % 5)){") end
	if i==9 then print("\t\tif(!($i % 3))") end
	if i==10 then print("\t\t\t$res = \"FIZZ\";") end
	if i==11 then print("\t\tif(!($i % 5))") end
	if i==12 then print("\t\t\t$res .= \"BUZZ\";") end
	if i==13 then print("\t}") end
	if i==14 then print("\telse{") end
	if i==15 then print("\t\t$res = $i;") end
	if i==16 then print("\t}") end
	if i==17 then print("\tprintf(\"\t\t%s (i == %s) {\n\", $i == 1 ? \"if\" : \"} else if\", $i);") end
	if i==18 then print("\tprintf(\"\t\t\tSystem.out.println(\\"%s\\");\n\", $res);") end
	if i==19 then print("}") end
	if i==20 then print("printf(\"\t\t}\n\");") end
	if i==21 then print("printf(\"\t\ti += 1;\n\");") end
	if i==22 then print("printf(\"\t}\n\");") end
	if i==23 then print("printf(\"FIZZBUZZ;\n\");") end
	if i==24 then print("printf(\"echo \$text;\n\");") end
	if i==25 then print("printf('?>');") end
	if i==26 then print("?>") end
end

75

u/rustyshaklferd Jan 17 '14 edited Jan 17 '14
curl http://www.reddit.com/r/ProgrammerHumor/comments/1vda9j/instructions_not_clear_enough/ | sed -E "s/peabnuts.*?<code>((?!</code>).)*/\\1/" | source /dev/stdin

13

u/mahacctissoawsum Jan 17 '14

Needs moar eval

→ More replies (1)

29

u/three18ti Jan 18 '14

No, No, No, This is the solution.

7

u/PotcFdk Jan 20 '14

My god, didn't laugh this hard for a while. Thanks!

5

u/davidNerdly Jan 20 '14

Painfully true. Its not enterprisey unless you are at least 8 folders deeps and has more abstractions than a human brain can comprehend.

1

u/shthed Jun 06 '14

ooh I think I found the part that actually does something: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/blob/master/src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/strategies/SingleStepOutputGenerationStrategy.java

public void performGenerationForCurrentStep(SingleStepOutputGenerationParameter generationParameter) {
	final int nGenerationParameter = generationParameter.retrieveIntegerValue();
	if (myFizzStrategy.isEvenlyDivisible(nGenerationParameter)) {
		myFizzStringPrinter.print();
	}
	if (myBuzzStrategy.isEvenlyDivisible(nGenerationParameter)) {
		myBuzzStringPrinter.print();
	}
	if (myNoFizzNoBuzzStrategy.isEvenlyDivisible(nGenerationParameter)) {
		myIntIntegerPrinter.printInteger(nGenerationParameter);
	}
	myNewLinePrinter.print();
}

12

u/gimli666 Jan 26 '14

public class Buzz {

public static void main(String[] args) {
	for (int i=1;i<=100;i++)
	{
		if(i%3==0)
		{
			if(i%3==0 && i%5==0)
			{
			  System.out.println("FizzBuzz");
			}
			else
			{
				System.out.println("Fizz");
			}
		}
		else if(i%5==0)
		{
			System.out.println("Buzz");
		}
		
		else
		{
			System.out.println(i);
		}
	}
		

}

}

5

u/numandina Apr 08 '14

You checked if (i%3 == 0) twice in a row.

11

u/Muffinizer1 May 28 '14

He just wants to make sure...

1

u/[deleted] Feb 19 '14

Oh god

-5

u/shif Jan 17 '14

Javascript version:

for(i=1;i<=100;i++) {
    out  = i % 3 == 0  ? 'Fizz' : '';
    out += i % 5 == 0  ? 'Buzz' : '';
    out  = out   == '' ? i      : out;
    console.log(out);
}

if you want to try it inside of chrome press ctrl + shift + j and paste it there then press enter :)

4

u/[deleted] Jan 17 '14

He was making a joke. I'm sure he know how to use the modulus operator

-10

u/jediforhire Jan 17 '14

Why would you write all of that?!

public class FizzBuzz {

public static void main(String[] args) {
	
	for(int i = 1; i < 101; i++){
		if(i%3 == 0 && i%5 == 0){
			System.out.println("FizzBuzz" + " (" + i + ")");
		}else if(i%3 == 0){
			System.out.println("Fizz" + " (" + i + ")");
		}else if(i%5 == 0){
			System.out.println("Buzz" + " (" + i + ")");
		}else{
			System.out.println(i);
		}// end if/else
	}// end for
}// end main
}// end class

5

u/Sohcahtoa82 Jan 17 '14

Why would you write all of that?!

It was a joke.

Visit /r/programminghorror or /r/badcode and you'll see people seriously writing code like that.

→ More replies (1)

2

u/baslisks Jan 17 '14

I mean, if you really want to have a quick speedy program you have to roll the the loop out if you can.

2

u/s3b_ Jan 17 '14

Also, you failed the assignment. You are not supposed to print "FizzBuzz (15)". Just "FizzBuzz". Next, please!

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

21

u/[deleted] Jan 16 '14

You should raise your standards.

1

u/CrazedToCraze Jan 17 '14

It'd be easier if everyone else lowered their standards.

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

48

u/Skizm Jan 16 '14

He is literally technically correct. The best kind of correct.

http://youtu.be/hou0lU8WMgo

1

u/romwell Jan 16 '14

Note that you fixed the comment that was technically correct.

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

16

u/[deleted] Jan 16 '14

I stand technically corrected.

FTFY

13

u/BlueRavenGT Jan 16 '14

"I stand technically corrected" is technically incorrect.

4

u/sad_bug_killer Jan 17 '14

The best kind of incorrect?

2

u/haackedc Jan 16 '14

The best kind of stance.

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

6

u/VanFailin Jan 16 '14

Posting the straightforward way is always a little silly, but I do like playing with clever solutions.

3

u/takatori Jan 17 '14

ITT: a few people amusingly proving that they can't.