That's one way to do it



  • I was interviewing applicants for a senior programming position, writing in C. One of the programming tasks involved negating an integer. Here's what I got:

    x = ~x + 1;

     The good news is the applicant understood the assumptions made. The bad news is who would do this?

     



  • I'm pretty sure they were annoyed with the simpleness of the question and wanted to show you they knew how signed integers are stored.



  •  ^



  • But then: who choses the name x for an integer? Everyone knows that's a real number.

    BTW, what would you have thought of: x = NULL;



  • @zmafoox said:

    I was interviewing applicants for a senior programming position, writing in C. One of the programming tasks involved negating an integer. Here's what I got:

    x = ~x + 1;

     

    From a pedantic point of view, this is incorrect. Signed overflow is undefined in C, so this is undefined behaviour if x == 0 (and some C compilers may thus optimise it under the assumption that x != 0, which might be relevant if you compared it to zero right after).

     



  •  @ais523 said:

    From a pedantic point of view, this is incorrect. Signed overflow is undefined in C, so this is undefined behaviour if x == 0 (and some C compilers may thus optimise it under the assumption that x != 0, which might be relevant if you compared it to zero right after).

    You're quite right.

    Recently, there was a discussion about applicants not catching an infinite loop in 5 lines of code, but this one is even more trivial. What's the ultimate interview question? Write "hello world" in the language of your choosing? Add zero, one or more semicolons to make this code syntactically correct: int a = 0 int b = 1?



  • @ais523 said:

    @zmafoox said:
    I was interviewing applicants for a senior programming position, writing in C. One of the programming tasks involved negating an integer. Here's what I got:

    x = ~x + 1;

    From a pedantic point of view, this is incorrect. Signed overflow is undefined in C, so this is undefined behaviour if x == 0 (and some C compilers may thus optimise it under the assumption that x != 0, which might be relevant if you compared it to zero right after).

    Where is the signed overflow when x == 0?

    Assuming 32-bit signed integers (adjust the values for other sizes)...I think you mean when x == -2147483648? But then, you can't meaningfully negate that either, so it's not really a problem.



  • @TGV said:

    Add zero, one or more semicolons to make this code syntactically correct: int a = 0 int b = 1
     

    I PROGRAM JS YOU FIEND



  • @dhromed said:

    @TGV said:

    Add zero, one or more semicolons to make this code syntactically correct: int a = 0 int b = 1
     

    I PROGRAM JS YOU FIEND

    a, b := 0, 1
    // Or alternatively
    var a int = 0
    var b int = 1
    // See? No semicolons needed.


  •  @zmafoox said:

    I was interviewing applicants for a senior programming position, writing in C. One of the programming tasks involved negating an integer. Here's what I got:

    x = ~x + 1;

     The good news is the applicant understood the assumptions made. The bad news is who would do this?

     

    How nonportable.

     Here's a better version (though it may not complete always, I recommend using a timeout and declaring x to be non-negatable when it expires)

     

    #include <stdlib.h>
    
    int find_negative(int x) {
        int y = 0;
        while (x + y) y = rand();
        return y;
    }
    


  • @dhromed said:

    I PROGRAM JS; YOU FIEND
     

    Is that the right answer?



  • @Cassidy said:

    @dhromed said:

    I PROGRAM JS; YOU FIEND
     

    Is that the right answer?

     

    No, but you still score points.

     



  •  @Ben L. said:

    @dhromed said:

    @TGV said:

    Add zero, one or more semicolons to make this code syntactically correct: int a = 0 int b = 1
     

    I PROGRAM JS YOU FIEND

    a, b := 0, 1
    // Or alternatively
    var a int = 0
    var b int = 1
    // See? No semicolons needed.

    You have completely failed the test. OMG! You had to add semicolons, not newlines, nor change the syntax. And I don't know why you think your solutions are proper JS.

    NEXT!



  • @TGV said:

    And I don't know why you think your solutions are proper JS.

    @Ben L. said:

    Filed under: GO IS SUPERIOR LANGUAGE

    ?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.