6 ms·
A way to torture an interviewer (C++, FizzBuzz)
#include <stdio.h>
static struct X {
void* operator new[](size_t) { return (char*)&1[""]; }
void operator delete[](void*) { }
X() : X((char*)this - "") { }
X(char i) {
printf(i%3?i%5?"%s%i\n":"%s\n":"Fizz%s\n",i%5?"":"Buzz",i);
}
} *x = new X[100]();
int main(){}
- danuker 4y agoIf a candidate were suggesting this as their serious FizzBuzz implementation fit for production, I'd have a hard talk with them about readability.
- yrgulation 4y agoIf an interviewer had a "hard talk" about anything during an interview, i'd have an immediate hard walk.
- deleted 4y ago[deleted]
- rgoulter 4y agoWhat implementation of FizzBuzz do you use in your production environment?
- ketzu 4y agoObviously outsourced to a 'competent' FBaaS provider! https://fizzbuzz.ketzu.net/to/100 https://fizzbuzz.ketzu.net/to/100 (Also fun when you just outsource task in a language introduction task in a leetcode-like setting and get surprised it actually allowes you to make http requests...)
- TrackerFF 4y agoEnterprise FizzBuzz, obviously https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...
- arethuza 4y agoNo microservices, not nearly Enterprish enough: https://github.com/domdavis/fizzbuzz https://github.com/domdavis/fizzbuzz "FizzBuzz split into a set of 4 microservices designed to be run as a fleet of docker images for highly concurrent, highly resilient deployment."
- danuker 4y agoA bug-free one (nonexistent). :) In seriousness, the interview is supposed to give a taste of how the candidate thinks and codes in general. If they start using every obscure language feature under the sun, I am not too sure they got the idea.
- wiseowise 4y agoDo you seriously believe, that someone who can write stuff like this can’t write ordinary code?
- danuker 4y agoThere are a lot of leetcoders who can't work in a team. Maybe they work well on their own, or maybe their solutions collapse when given a problem of a complexity greater than the leetcode problems.
- spywaregorilla 4y agowe just hardcoded the solutions for 1 to 100 manually. It's been fine so far.
- tekla 4y agoThe point of fizzbuzz is to figure out if you are not a rock. If you cant figure out how to code fizzbuzz from basic 8th grade logic something is wrong.
- gpderetta 4y agoEnterprise FizzBuzz or go home! Realistically this is an appropriate joke answer to a joke question.
- hackandtrip 4y agoTalking about actually genial FizzBuzz interview answers, I have yet to see someone even going close to this: https://aphyr.com/posts/353-rewriting-the-technical-interview https://aphyr.com/posts/353-rewriting-the-technical-intervie...
- jtagen 4y agoGood lord, this made my morning.
- HelloNurse 4y agoIf you do this on the interviewer's computer, their compiler can deal with undefined behaviour using physical violence. It's in the standard.
- mg 4y agoYay, let's talk about FizzBuzz! This is the fastest PHP version I could come up with so far: <?php ob_start(null, 32000); $a = [0, 1, 'fizz', 3, 'buzz', 'fizz', 6, 7, 'fizz', 'buzz', 10, 'fizz', 12, 13, 'fizzbuzz', '']; for ($i = 1; $i < PHP_INT_MAX-15; $i+=15) { $a[0] = $i; $a[1] = $i+1; $a[3] = $i+3; $a[6] = $i+6; $a[7] = $i+7; $a[10] = $i+10; $a[12] = $i+12; $a[13] = $i+13; echo join("\n", $a); } I'm sure there still are ways to make it faster. But I fail to think of them. Except for unrolling multiple loops into one. But that is so ugly. Any ideas of elegant ways to make it faster? Testing the performance like this: php fizzbuzz.php | pv > /dev/null 215MiB/s on my slowish laptop. The "yes" command gives me about 10x the throughoutput: yes fizz | pv > /dev/null 3.04GiB/s Shows how much wiggle room there still is! I also wrote a python version of it, but it performs 10x slower: a = [0, 1, 'fizz', 3, 'buzz', 'fizz', 6, 7, 'fizz', 'buzz', 10, 'fizz', 12, 13, 'fizzbuzz']; for i in range(1,1000000000,15): a[0] = i; a[1] = i+1; a[3] = i+3; a[6] = i+6; a[7] = i+7; a[10] = i+10; a[12] = i+12; a[13] = i+13; print("\n".join([str(x) for x in a])); Maybe the bottleneck is the loop that converts the integers to strings? Maybe someone with Python knowledge can comment on how to approach this in Python!
- lelandfe 4y agohttps://tech.marksblogg.com/fastest-fizz-buzz.html https://tech.marksblogg.com/fastest-fizz-buzz.html Not sure I'd call writing Assembly "elegant" but hey!
- mg 4y agoYeah, I know all the different language versions from the FizzBuzz shootout on Stackoverflow. I'm particularly interested in comparing PHP, Python and Javascript. So I would like to write an "optimal" PHP version first. Afaik that has not been done yet.
- capableweb 4y agoProbably your implementation is limited by the speed PHP/echo can output things to stdout, rather than your implementation per-se. Better, use a benchmarking framework/library to see how many OP/sec you can do, within the PHP runtime, so you remove stdout from the benchmark.
- orangepanda 4y agoThen there's 50GiB/s fizzbuzz https://codegolf.stackexchange.com/a/236630 https://codegolf.stackexchange.com/a/236630
- jessermeyer 4y agoAs someone who avoids C++ these days ... Is this a compile time FizzBuzz? `(char*)&1[""]` is the empty string here returning its data segment address as the offset? Not sure about the purpose of `1`. Is the iteration achieved by the constructor basically re-invoking itself?
- vardump 4y agoIt's a static initialization time FizzBuzz. In this case, I think it's executed before main() is called, and not at compile time. Unless, of course, you have a very clever compiler that determines memory allocation is not actually allocating anything and that the output is a static string, and there are no side effects. Such a clever compiler could optimize it all into just one "puts" call.
- jessermeyer 4y agoNeither GCC nor Clang bake the final string into the data segment. If I had to guess, printf is the one preventing the more fancy optimizations to take place.
- vardump 4y agoI think so too. Also, depending on stdlib output buffering, the external I/O behavior of 100 puts calls is potentially different from just one call. In other words, there might be a different number of stdout write-calls.
- bno1 4y ago&1[""] is the same as &(""[1])
- jessermeyer 4y agoAh, so it's the nil address?
- 4y ago
- dj_mc_merlin 4y agoGood effort but too straight-forward. Using an explicit, readable ternary operator is cheating in obfuscation.
- anfractuosity 4y agoI rather like this too - https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/ https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/
- jamesbfb 4y agoHoly moly, this gave me a good (well needed) chuckle.
- wolframhempel 4y agoI always felt that being annoying is an art - something that requires dedication and craftsmanship - and whoever wrote (and hopefully actually did) this is clearly a master at it.
- thisiswrongggg 4y agoYou wouldn't torture him. You would just zero the probability of getting an offer. There's no SW house or colleague in his right mind that would want a developer that writes obfuscated and needlessly complex to understand code.
- easytiger 4y agoUnless they had, and i know this is hard to imagine in a c++ dev, a sense of humour
- shultays 4y agoI mean the code also includes a bunch of undefined behaviors, we do not joke about that
- aloisdg 4y agoWhat if the SW house is competing in the ioccc?
- astrange 4y agoQA engineer for a C++ compiler?
- leni536 4y agoThis program is undefined, so no, except maybe if you can make the compiler itself to crash.
- astrange 4y agoThat's a good thing to test. Btw, here's a C++ sample that crashes icc: https://pastebin.com/PiRtZi1t https://pastebin.com/PiRtZi1t. I haven't reported it because it's funnier this way.
- FpUser 4y agoI would not want developer to write this kind of code in production applications but I would very much hire a person who is capable of coming up with the code like this.
- atemerev 4y agoThe very point of FizzBuzz is that it is a test whether a candidate can write it straightforward enough, even redundantly, but readable — or they have to resort to some ugly cleverness :)
- leni536 4y agoI get how this works, but this has undefined behaviour at multiple places.
- jmartin2683 4y agoI’d walk out. Never, ever take a job given to you for solving a riddle. Everyone else there will have gotten theirs the same way and it won’t be great.
- elijaht 4y agoI mean I would even consider fizzbuzz a riddle. It’s one of the simplest programming tasks I have ever been asked in a technical assessment. Do you have a preferred alternative?
- dasloop 4y agoWe no longer ask candidates to write code. Instead we give them code to review and plenty of time. After that we do a meeting to discuss the changes. Works quite well for us.
- RcouF1uZ4gsC 4y agoThis is undefined behavior. According to the C++ standard, there is no way to reason about what this does. Anything can happen with this code. Once you have undefined behavior, you lose the ability to reason about your entire program. Thus, this program does not solve the problem. It demonstrates a lack of knowledge about undefined behavior in C++. Rating: Absolutely would never hire this candidate.
- kazinator 4y agoUndefined behavior doesn't mean there is no way to reason about what the program does according to ISO C++. It means that the way to reason about it is outside of ISO C++, in specific ways, like that it may be a documented extension in the implementation or numerous other possibilities; you have to research those possibilities before concluding that there remains no way to reason about it.
- lokedhs 4y agoI would probably post the idiomatic version in my programming language. Hopefully it will spark a discussion around language design. If not, perhaps it's a sign that the company is pretty rigid, and might not be a nice place to work. io:println¨ {⍵ (⊂"fizz") (⊂"buzz") (⊂"fizzbuzz") %⍨ 2 (⊥⍤1) ⍉ 0 = 5 3 |⌺ ⍵} ⍳20
- kazinator 4y ago> } *x = new X[100](); This C++ coder seems to have a habit of writing unecessary constructor argument lists. The example works if we change it to: *x = new X[100]; There are situations in C++ in which an extra constructor parameter list will do something different, so I'd be wary of this candidate.