#include
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(){}A way to torture an interviewer (C++, FizzBuzz)
1–10 of 84 posts
Re: A way to torture an interviewer (C++, FizzBuzz)
#2Re: A way to torture an interviewer (C++, FizzBuzz)
#3This is the fastest PHP version I could come up with so far:
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!Re: A way to torture an interviewer (C++, FizzBuzz)
#4Re: A way to torture an interviewer (C++, FizzBuzz)
#5If a candidate were suggesting this as their serious FizzBuzz implementation fit for production, I'd have a hard talk with them about readability.
Re: A way to torture an interviewer (C++, FizzBuzz)
#6If a candidate were suggesting this as their serious FizzBuzz implementation fit for production, I'd have a hard talk with them about readability.
Re: A way to torture an interviewer (C++, FizzBuzz)
#7Is 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?
Re: A way to torture an interviewer (C++, FizzBuzz)
#8Re: A way to torture an interviewer (C++, FizzBuzz)
#9Re: A way to torture an interviewer (C++, FizzBuzz)
#10As 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?
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.