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(){}
83 comments
[ 1.6 ms ] story [ 215 ms ] threadhttps://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."
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.
Maybe they work well on their own, or maybe their solutions collapse when given a problem of a complexity greater than the leetcode problems.
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...)
Realistically this is an appropriate joke answer to a joke question.
This 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:
215MiB/s on my slowish laptop.The "yes" command gives me about 10x the throughoutput:
3.04GiB/sShows how much wiggle room there still is!
I also wrote a python version of it, but it performs 10x slower:
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.
https://codegolf.stackexchange.com/a/236630
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.
The code uses "" as an arbitrary address, and ""[1] as that address + 1.
This way this-"" gets you 1-based index into the array.
It assumes the compiler dedfuplicates strings, making the behavior of this program undefined.
Here's how I unpack this: ""[1] is the 0 (termination) byte. The 0 byte is then interpreted as an address -- the nil address.
But what's the use of asking for the address of the null terminator? Where is that stored exactly?
Now the first character of that char array is found here: ""[0]
The second character is found here: ""[1]
So the address of the second character is found here: &""[1]
But as the string was represented by one byte char array that second address is past the end of the string.
So it's actually an undefined address.
Its address is taken there with &, which yields a const char*. The (char *) cast is only there to cast away const.
Btw, here's a C++ sample that crashes icc: https://pastebin.com/PiRtZi1t. I haven't reported it because it's funnier this way.
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...
Sometimes I wonder why some dev teams take 10x longer to develop some functionality than what I would consider reasonable.
Then I see code like this.
Of course, I just banged this out in a web browser text editor. In PyCharm that better be wrapped in a function at minimum, or a module.
This is something you memorize to try to show off
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.
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.
That's debateable.
1. new[] doesn't return a pointer that is suitable for storage for the objects. Yes the objects are empty (only contain padding), but that doesn't absolve from UB.
2. "" is used at two places, assuming that they refer to the same const char array. This is optimization is allowed, commonly implemented, but not guaranteed.
3. An empty struct having 1 byte size is also implementation defined, although common ABIs specify this. It also wouldn't make sense to implement it differently.
But maybe it is not UB because it is not calculated explicitly by the program, hard to tell
https://try.fsharp.org/#?code=DYUwLgBAZglgXnARgVwRAdsgthAvAW...
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:
There are situations in C++ in which an extra constructor parameter list will do something different, so I'd be wary of this candidate.