Live data from Hacker News

A way to torture an interviewer (C++, FizzBuzz)

news.ycombinator.com

71–80 of 84 posts

Re: A way to torture an interviewer (C++, FizzBuzz)

#71
post #44

Earlier quoted context omitted.

How much structure can a fizzbuzz have exactly?

How much structure would you like? https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

  > How much structure would you like?

  top = 100

  data = { 
      3: "fizz",
      5: "buzz"
  } 


  for i in range(1, top+1):
      output_num = True
      for j in data:
          if i%j == 0:
              print(data[j], end="")
              output_num = False

      if output_num:
          print(i)
      else:
          print("")
This is enough structure to anticipate the inevitable customer changing the requirements. No magic numbers in the real code.

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.

Re: A way to torture an interviewer (C++, FizzBuzz)

#72
post #65
post #62

Earlier quoted context omitted.

The "" will define a null terminated char array to represent the string. But as it string contains no text it's a char array that only requires one byte (i.e. it contains nothing but the null termination character). 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…

Nit: one past end of an array is actually fine as an address.

Not always. Consider this 'test.cpp' example:

    #include 
    #include 
    
    struct a_struct {
      char a[1];
      char b[2];
    
      a_struct() {
         strcpy(a, "");
         strcpy(b, "b");
      }
    } x;
    
    int main() {
       printf("before:\n");
       printf("a : '%s'\n", x.a);
       printf("b : '%s'\n", x.b);
    
       char *c = &1[x.a];
       *c = 'c';
    
       printf("after:\n");
       printf("a : '%s'\n", x.a);
       printf("b : '%s'\n", x.b);   //b is now corrupt
    }
Compile and running this code:

    C:\temp>g++ test.cpp
    C:\temp>a.exe
    before:
    a : ''
    b : 'b'
    after:
    a : ''
    b : 'c'

Re: A way to torture an interviewer (C++, FizzBuzz)

#73
post #72
post #65

Earlier quoted context omitted.

Nit: one past end of an array is actually fine as an address.

Not always. Consider this 'test.cpp' example: #include #include struct a_struct { char a[1]; char b[2]; a_struct() { strcpy(a, ""); strcpy(b, "b"); } } x; int main() { printf("before:\n"); printf("a : '%s'\n", x.a); printf("b : '%s'\n", x.b); char *c = &1[x.a]; *c = 'c'; printf("after:\n"); printf("a : '%s'\n", x.a); printf("b : '%s'\n", x.b); //b is now corrupt } Compile and running this code: C:\temp>g++ test.cpp C…

Yes, dereferencing past the end is not fine, but the address itself is.

Re: A way to torture an interviewer (C++, FizzBuzz)

#74
post #35

You 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.

I 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.

..except they probably wouldn't come up with janky code like this - especially not during the interview

This is something you memorize to try to show off

Re: A way to torture an interviewer (C++, FizzBuzz)

#75
post #74
post #35

Earlier quoted context omitted.

I 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.

..except they probably wouldn't come up with janky code like this - especially not during the interview This is something you memorize to try to show off

>"person who is capable of" - is not equal to actually doing it on an interview unless it was their own original code

Re: A way to torture an interviewer (C++, FizzBuzz)

#76

Earlier quoted context omitted.

Is consider this entertaining rather than torturous. A playful response to the mind numbing idiocy of fizzbuzz.

If someone wrote that in an interview I'd be keen to get them to explain to me exactly how it works... my C++ being a bit rusty (I mean old).

Explanation:

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.

Re: A way to torture an interviewer (C++, FizzBuzz)

#77

You 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.

Would you deem this needlessly complex?

https://try.fsharp.org/#?code=DYUwLgBAZglgXnARgVwRAdsgthAvAW...

Re: A way to torture an interviewer (C++, FizzBuzz)

#78
post #15

Earlier quoted context omitted.

What implementation of FizzBuzz do you use in your production environment?

A 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.

Do you seriously believe, that someone who can write stuff like this can’t write ordinary code?

Re: A way to torture an interviewer (C++, FizzBuzz)

#79
post #39

Earlier quoted context omitted.

QA engineer for a C++ compiler?

This program is undefined, so no, except maybe if you can make the compiler itself to crash.

That's a good thing to test.

Btw, here's a C++ sample that crashes icc: https://pastebin.com/PiRtZi1t. I haven't reported it because it's funnier this way.

Re: A way to torture an interviewer (C++, FizzBuzz)

#80
post #76

Earlier quoted context omitted.

If someone wrote that in an interview I'd be keen to get them to explain to me exactly how it works... my C++ being a bit rusty (I mean old).

Explanation: 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…

> This is the only UB here.

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.

Post reply on HN