4 ms·
Another way to freak interviewers out is to calculate mod5 implement a nice circular linked list with 5 nodes, one node containing "Buzz" and the other four nod
by VLM 13y ago
Another way to freak interviewers out is to calculate mod5 implement a nice circular linked list with 5 nodes, one node containing "Buzz" and the other four nodes containing null and call mod5(86) means you dance around the circular linked list 86 times and whatever string you stop on you output either Buzz 1/5 the time or null string/nothing 4/5 the time. This is hilarious if you run fizzbuzz from 0 to 100 but even funnier if you try 1000000 to 1000100
Also there's a pretty obvious conversion to recursion.
Either implementation can be even funnier if you memoize results in an array cache (so if you've already figured out the hard way that 999999mod3=0 then next time around when you check 1000000 you need not cycle all the way down to 0 for 1000000 because once you hit 999999 you already know from last time around how 999999 turned out. Bonus points for making your "cache" smaller, like implementing a LRU (or simpler) algorithm for the last several lookups rather than memoizing all one million results.
There's also a devilish way of (ab)using IEEE float arithmetic rounding errors which I forget at this moment.
If your language or a library attached to it handles arbitrary bases, there's a certain pattern to the last digit WRT multiples of 5 when expressing the number in base 5... so just to be a jerk you do the math for 3-mults in base 3, 5-mults in base 5, and your 0 to 100decimal counter in base 7 for the pure hell of it. OR amuse the interviewer because the base wasn't spec'd by running up to 100base7 (which is probably going to be a new ethernet standard soon enough).
Another fun form of rebellion is to implement an emulator for any machine language machine you can remember running simple BCD math. So here's a lame little Z80 emulator and a Z80 assembly routine to solve this using simple BCD math.
Deviant forms of trig obfuscation can be funny, essentially you rely on the trig libraries overflow behavior to dance 1/5 of the way around a unit circle each time, and when you're all done, if your angle is close enough to zero, I guess its divisible by 5. This is much funnier if you implement essentially a pentagon and then rotate it using quaternions
I think programmer brain puzzles are kind of dumb, when I was younger I would have just walked out of the room, but as I age the likelihood of a total smartass answer like above increases over time.
- adamtj 13y agoFizzBuzz is not a brain puzzle. It's a pass/fail weedout question. It's purpose is to determine if you are worth spending more time interviewing. If you don't know enough math to understand multiples or you don't know enough programming to write a "for" loop then that's the end of the interview. If you pass, you should then get the better and more relevant questions. Writing a Z80 emulator at that stage of the interview is a waste of time, and may itself constitute a fail depending on the interviewer.
- smrtinsert 13y agoIf FizzBuzz is ever considered that humorless, I'd just walk out. Pointless questions deserve pointless but correct answers.
- VLM 13y agoNot entirely pointless, assuming you don't completely screw up FizzBuzz 2.0 or whatever you want to call it, the next question was probably going to be along the lines of "tell me about a problem using recursion other than boring old factorial" or "can you do much of anything with an array" or "show me a data structure more interesting than Int X, like, say, a linked list?" or "Tell me about your time in the 3-d graphics rendering world" or "tell me about memoization and/or caching strategies" or "tell me how to use a library and its API in language XYZ where it could be an arbitrary numerical base library". (edited to add a hilarious one: Using a unit testing framework only permit proper fizzbuzz, then feed with with 10e3 monkeys on typewriters trying to generate Shakespeare technology and trust the testing framework...) I suppose for a low level CRUD app job knowing what modular math is and how to run a loop, aka fizzbuzz, is probably massive skill overkill but there are higher level jobs out there... I'm racking my brain right now trying to think of a "funny" way to do fizzbuzz for a high freq trading interview as in another story on HN today. So to test mod3 you'd submit a buy order followed by two half sized sell orders repetitively all in a couple milliseconds and let the market maker routine clear the market, and orders left over, if any, would tell you if you submitted a multiple of 3 orders so you could print "fizz"?
- ProblemFactory 13y agoI don't think FizzBuzz leads into interesting interview questions. That would be too dependent on the hope that the candidate does implement an overkill solution for fizzbuzz. Much more interesting questions can be derived from the problems the company is actually working on. FizzBuzz is a simple "can you code at all" filter. If you write the obvious solution, you move on to the actual interview. If you write an elegant and amusing solution, you move on to the actual interview. Surprisingly most applicants for programming positions cannot write any code that uses a for loop or an if statement at all, and fizzbuzz is a 3-minute test to prevent everyone from wasting their time.
- im3w1l 13y agoI had to try the linked list idea http://pastebin.com/3rUk6yS1 http://pastebin.com/3rUk6yS1
- euank 13y agoNice! I cleaned it up a little, shortened it down, got it down from 7 to 4 for loops. It is inspired by yours of course! https://gist.github.com/euank/7206859 https://gist.github.com/euank/7206859
- Sir_Cmpwn 13y agoI've also seen people memorize a random seed that produces FizzBuzz procedurally, then use it as the basis for their program. Also, I don't think I could write a z80 emulator on a whiteboard ;)
- gamegoblin 13y agoCompletely unrelated, but are you the same Sir Cmpwn who posts on cemetech.net?
- Sir_Cmpwn 13y agoI used to, yeah.
- gamegoblin 13y agoAh. I thought I recognized your handle. I'm Pseudoprogrammer on there. I was pretty active back in middle school many years ago. Mainly lurk, now.
- Sir_Cmpwn 13y agoAh, fun stuff. I still work on TI stuff [1], but I don't really participate in the community anymore. [1] https://github.com/KnightSoft/KnightOS https://github.com/KnightSoft/KnightOS