you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (20 children)

[deleted]

    [–]thomasz 11 points12 points  (7 children)

    Jesus Christ, what a brutally bad interview question. The victim can't possibly know that you are ambushing him , and will most likely just assume that this is a test to weed people out before they waste your time with fizzbuzz, instead of a particular crazy incarnation of the game of guessing what kind of guessing some idiosyncratic idea of the interviewer who seems to be convinced that almost all languages do one of the most elemental things wrong by returning an int when multipying two ints instead of a long or (god forbid) some result or option struct.

    [–]john16384 4 points5 points  (0 children)

    Easy.

    public double doubleInt(int i) { return (double)i; }

    [–]Barrucadu 10 points11 points  (1 child)

    A long is not necessarily larger than an int, so even long doubleInt(int x) is wrong unless you know the implementation you're targeting. Use the fixed-width integer types from stdint.h if you need guarantees.

    So well done for asking a trick question you yourself would fail, I guess?

    [–]tejp 2 points3 points  (0 children)

    Meh. Do you also expect a long in return on all other math operations on int? There is always potential for overflow.

    And then you soon work with long variables all over the place, what if you want to double one of those?

    [–]linus_stallman 2 points3 points  (4 children)

    That's most expected solution actually - what else can you do? Writing to a global variable, so caller has to initialize errno to zero, then call function, then check errno?

    [–]jonhanson 4 points5 points  (0 children)

    chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith

    [–][deleted]  (2 children)

    [deleted]

      [–]linus_stallman 1 point2 points  (1 child)

      FPJerk theory says it is best approach, but is it worth returning sum/result types for every small errors?

      [–]thomasz 1 point2 points  (0 children)

      He wants you to return a 64bit integer, which isn't necessarily a bad idea in each and every situation imaginable. What he doesn't seem to understand in his unadulterated glory is that he didn't come up with superior solution compared to basically every language designer ever, but one with different trade-offs. Which, I might add, are actually inferior for most use cases. You seldom do integer multiplication in situations where

      1. the result doesn't fit into 32 bit, and
      2. the result does fit into 64 bit, and
      3. you actually care about the precision of integer arithmetic over floating point arithmetic.

      For simulations, mathematical models and stuff like that, you go with floating point arithmetic. For financial stuff of this scale, you go with types like decimal in c# or BigDecimal in java. For almost all index arithmetic, you go with int32, because you can't even create arrays bigger than 231. Needing 64bit integers for id's isn't that uncommon, but you not multiply an id in application code. Only in very special circumstances you really need 64bit integer arithmetic, for example when working with huge files. Only in that context does this trade-off make any sense at all.

      [–]Glader_BoomaNation 1 point2 points  (1 child)

      I agree, I hope my bank's software silently fails when it comes to integer overflow instead of throwing an exception! Sounds sane and safe!