4 ms·
>> The famous fizzbuzz test simply asks "are you aware of the modulo operator?" > Wait, what? No, it asks "can you write a for loop without breaking a sweat?".
by dfabulich 10y ago
>> The famous fizzbuzz test simply asks "are you aware of the modulo operator?"
> Wait, what? No, it asks "can you write a for loop without breaking a sweat?".
There are notorious cases where senior engineers with years of actual honest coding experience "fail" fizzbuzz. IMO, That happens in two ways:
1) Not knowing (forgotten?) what a modulus operator is, and trying to code one up from scratch with a for loop. This can totally happen when you spend a few years working on line-of-business CRUD apps that never turn out to need modulus arithmetic.
Coding modulus from scratch with just + - / * is a fun little challenge in its own right, but it'll probably take you longer than 10 minutes to write it correctly and solve fizzbuzz with it.
That means you just took more than 10 minutes on fizzbuzz, which means you failed.
2) Trying to "clean up" the solution. http://c2.com/cgi/wiki?FizzBuzzTest http://c2.com/cgi/wiki?FizzBuzzTest has a good discussion on 'Why Fizz-Buzz is "hard:"', which points out that many of the most obvious solutions have code duplication, which some people then try to eliminate, tying themselves in knots trying to make it perfect.
The third possibility, that a candidate with years of experience shipping code nonetheless struggles to write a for loop, is IMO just not as common as the above two failure cases. (Much more likely is that the candidate has lied/exaggerated on their resume.)
- corecoder 10y ago#2 happened to me: I wrote the straightforward implementation in a few seconds, and then struggled and tied myself up trying to eliminate the duplication, making it worse and worse, until I started panicking. Luckily the interviewer understood what was happening and changed subject.
- imtringued 10y ago>Coding modulus from scratch with just + - / * is a fun little challenge in its own right, but it'll probably take you longer than 10 minutes to write it correctly and solve fizzbuzz with it. It's surprisingly easy actually. var remainder = x - Math.floor(x/y)*y (Math.floor because javascript)
- benhoyt 10y agoIf you don't know about modulus, you don't have to re-implement it. For Fizzbuzz you're looping anyway, so you can just maintain two counters, one that counts to 3 and resets, and one that counts to 5 and resets. When one of the counters resets, you know the main loop counter is a multiple of 3 or 5. Like so: def fizzbuzz_no_modulo(): counter_three = 1 counter_five = 1 for i in range(1, 101): output = [] if counter_three == 3: output.append('Fizz') counter_three = 0 if counter_five == 5: output.append('Buzz') counter_five = 0 if not output: output.append(str(i)) print(''.join(output)) counter_three += 1 counter_five += 1
- squeaky-clean 10y agoThere's a lot of ways you can do it. I think it's fun to see how people solve it when they're not allowed to use the obvious answers (note: I don't interview people and probably would not choose no-modulo-fizzbuzz as an interview question). Similar to your solution, instead of two counters, you can have two iterators that cycle every 3 / 5 elements, and add them. Then fill in any blank elements with their index+1. from itertools import cycle, zip_longest, islice fizz_list = ['', '', 'fizz'] buzz_list = ['', '', '', '', 'buzz'] output = [ fizz + buzz for fizz, buzz in islice(zip_longest(cycle(fizz_list), cycle(buzz_list)), 0, 100) ] for i in range(len(output)): print(output[i] or i+1)
- lmm 10y ago> Much more likely is that the candidate has lied/exaggerated on their resume. If you don't do fizzbuzz or equivalent, how else do you catch that?
- SilasX 10y agoOn 1), I disagree -- if a genuinely senior engineer doesn't know of modulus (totally possible btw), then what they'll do is: "Okay, let's assume I have some function that tells me whether something is divisible by p. [writes it while using that function] Now, how would I implement that function? Well, I'd search for some existing library function to do that, but if I can't, then here's how I would check divisibility ..." That's because the senior dev knows how to break the problem into solvable pieces, even and especially where they don't already know the solution to each piece.
- morgante 10y ago> Coding modulus from scratch with just + - / * is a fun little challenge in its own right, but it'll probably take you longer than 10 minutes to write it correctly and solve fizzbuzz with it. I've seen numerous people fail on FizzBuzz despite supposedly being senior engineers. None of them failed because they didn't know the modulo operator. For one thing, it's actually pretty easy to solve even without modulo. If someone doesn't know modulo, I'll happily explain it. More commonly, people struggle with even basic things like setting up a loop at all.