News for hackers. Stuff that matters.
Re:The technical interview is an ego trip by a_e_k on Friday September 11, 2020 @09:57PM (#24449208)
Careful there with that "legit" solution! With extreme values, it can cause overflows and other weird behavior.

For example, in Javascript with:

    x = 2
    y = 9007199254740991
then:

    x = x + y // 9007199254740992
    y = x - y // 1                  (!!!)
    x = x - y // 9007199254740991
Or in a somewhat more sane language like C, it'll usually work but may technically invoke undefined behavior:

    % cat test.c
    #include <stdio.h>
    int main() {
        int x = 2;
        int y = 2147483647;
        x = x + y;
        y = x - y;
        x = x - y;
        printf( "%d %d\n", x, y );
    }
    % gcc -fsanitize=undefined -O0 -g -o test test.c && ./test
    test.c:5:7: runtime error: signed integer overflow: 2 + 2147483647 cannot be represented in type 'int'
    test.c:6:7: runtime error: signed integer overflow: -2147483647 - 2147483647 cannot be represented in type 'int'
    test.c:7:7: runtime error: signed integer overflow: -2147483647 - 2 cannot be represented in type 'int'
And even in Python you can end up with it silently turning your ints into longs.

There are no comments on this comment yet.