4 ms·
The question was "Write the worst – but working – implementation of Fizz Buzz that you can think of." It is designed to 'bring up some good laughs, and takes so
by dalke 11y ago
The question was "Write the worst – but working – implementation of Fizz Buzz that you can think of." It is designed to 'bring up some good laughs, and takes some of the pressure off of the applicant' with the assumption that the candidate will respond "Write bad code? I can do that!".
My question is, what if the interviewee is unable to produce bad code? Or even better, if the interviewee's worst code is better than the interviewer's best code. Does that inability to produce bad code on demand count as a negative?
- collyw 11y agoWell that's up to the interviewer. I would say it doesn't count as negative, but that's me. I am assuming the conversation around it is what is important. I recently got asked to do fizzbuzz in an interview. Then "fizzbuzz without the modulo operator". It's such an obvious way to approach the problem, that coming up with an alternative was surprisingly tricky. Caused a bit of a mental block for me.
- dalke 11y agoIf it's up to the interviewer then it isn't really a test of one's skills, is it? ("The worse the code is, the greater the understanding of the language in these cases") Instead, the interviewee needs to intuit what it is that the interviewer is looking for. Doesn't that restriction reduce to "how do you implement modulo?" def modulo(a, b): return a - a // b * b def modulo(a, b): while a > b: a -= b return a or pulling a hack from http://graphics.stanford.edu/~seander/bithacks.html#ModulusDivision http://graphics.stanford.edu/~seander/bithacks.html#ModulusD... , which is equivalent to casting-out-9s but in base 16: def mod15(a, b): while a > 15: b = 0 while a: b += a & 15 a >>= 4 a = b if a == 15: return 0 return a If it doesn't reduce to that, then it's another example of trying to guess what the interviewer is really asking, rather than trying to demonstrate one's expertise.
- collyw 11y agoAs I pointed out, I don't think the objective of the question is to test skills, but to start a discussion about what one values in "good code". It's an area that is partially subjective.
- dalke 11y agoYes, I understand your explanation. But you also said "The worse the code is, the greater the understanding of the language in these cases." My question is, what if the interviewee is unable to give an example of bad code? I don't see how failure to come up with bad code implies a worse understanding of the language. I can also easily see how people with poor understanding of the language can still produce horrible code. For example, at the end of this comment I write a simple assembly language interpreter in Python. That's a horrible solution to the problem. Explain to me how that shows I understand Python. Or for that matter, how it's a useful segue into a discussion of the factors that go into good code. If the point is to discuss what "good code" means, then why not a question which is better targeted towards that specific goal? For example, give four examples of FizzBuzz, ask the interviewee to discuss the pros and cons of each one, and ask for a preferred ranking. That's surely more like what happens in real life, doesn't have a failure mode where someone can't come up with really bad code, and actually leads to the discussion you want. import sys def execute(memory, code): lineno = 0 while 1: try: line = code[lineno] except IndexError: break if line[0] == "jeq": if memory[line[1]] == memory[line[2]]: lineno = line[3] continue elif line[0] == "inc": memory[line[1]] += 1 elif line[0] == "mod": memory[line[3]] = memory[line[1]] % line[2] elif line[0] == "print": sys.stdout.write(str(memory[line[1]])) elif line[0] == "load": memory[line[1]] = line[2] elif line[0] == "jmp": lineno = line[1] continue elif line[0] == "jz": if memory[line[1]] == 0: lineno = line[2] continue else: raise AssertionError(line[0]) lineno += 1 def fizzbuzz(n): memory = {"a": n+1, "b": 0, "c": 0, "d": "\n", "e": " "} code = ( ("inc", "b"), ("jeq", "a", "b", 1000), ("print", "b"), ("mod", "b", 15, "c"), ("jz", "c", 14), ("mod", "b", 5, "c"), ("jz", "c", 12), ("mod", "b", 3, "c"), ("jz", "c", 10), ("jmp", 17), ("load", "c", "Fizz"), ("jmp", 15), ("load", "c", "Buzz"), ("jmp", 15), ("load", "c", "FizzBuzz"), ("print", "e"), ("print", "c"), ("print", "d"), ("jmp", 0), ) execute(memory, code) fizzbuzz(100)
- bugstomper 11y agoThere are much simpler ways without using modulo than defining your own modulo and using it. For example (in Java) class FizzBuzz { public static void main(String[] argc) { int next5 = 5; int next3 = 3; for (int i=1; i<=100 ; i++ ) { String output = ""; if (i == next3) { output = "Fizz"; next3 += 3; } if (i == next5) { output += "Buzz"; next5 += 5; } if (output.isEmpty()) { output = Integer.toString(i); } System.out.println(output); } return; } }
- dalke 11y agoYes, that works. But I don't understand the goal of these variant question. The original goal was to weed out obviously incompetent programmers. I accept that for some positions that's reasonable. But if I were given that question, and end up implementing my own modulo function to replace the missing operator, is that a mark against the interviewee, compared to someone with otherwise identical qualifications who implements your solution? Or someone who does this sieve solution: def fizzbuzz(n): n += 1 values = ["%d"]*(n+1) for i in range(0, n, 3): values[i] = "%d Fizz" for i in range(0, n, 5): values[i] = "%d Buzz" for i in range(0, n, 15): values[i] = "%d FizzBuzz" for i in range(1, n): print values[i]%i All of these have different performance/memory/maintenance characteristics, but the full constraints are unspecified. At some point it feels like the interviewer just wants to see the monkey dance, by throwing in non-realistic constraints.