63 points · by jpegqs · 4y ago · 82 comments
#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(){}This is something you memorize to try to show off
A playful response to the mind numbing idiocy of fizzbuzz.
1. There are no data members inside X, in this case the size of the structure is 1 byte.
2. main() does nothing.
3. *x = new X[100]() - this requests a new array of 100 elements of type X, at a stage before main() is called.
4. The new[] operator is overloaded in the class, this operator must return the address for the requested array. It returns the address of the empty string object plus 1.
5. For each of the 100 elements, the X() constructor is called, the "this" address for those elements will be the value returned by new[] plus an index from 0 to 99. The X() constructor converts this to a number from 1 to 100 by subtracting the address of the empty string. This is the only UB here, but it works in all major modern compilers since they combine the same strings into one instance.
Btw, here's a C++ sample that crashes icc: https://pastebin.com/PiRtZi1t. I haven't reported it because it's funnier this way.
While this code wouldn't be an instant offer, it would be a great solution in my eyes and it would follow hardest questions to qualify a candidate for senior position.
That's my opinion at least. I'm making a hiring decisions in my company.
https://try.fsharp.org/#?code=DYUwLgBAZglgXnARgVwRAdsgthAvAW...
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/sShows 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!Not sure I'd call writing Assembly "elegant" but hey!
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.
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.
There is a large FizzBuzz shootout here that I like:
https://codegolf.stackexchange.com/questions/215216
I think it's a nice "standard" for FizzBuzz comparison.
Conflating the two seems to confuse more than measure anything useful.
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?
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.
In other words, there might be a different number of stdout write-calls.
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.
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.https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...
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."
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...)
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.
Realistically this is an appropriate joke answer to a joke question.
io:println¨ {⍵ (⊂"fizz") (⊂"buzz") (⊂"fizzbuzz") %⍨ 2 (⊥⍤1) ⍉ 0 = 5 3 |⌺ ⍵} ⍳20