7 ms·
> Explain what ["123", "456", "789a", ...].map(parseInt) returns I understand the frustrations with questions unrelated to the job you'll be performing, such a
by reificator 9y ago
> Explain what ["123", "456", "789a", ...].map(parseInt) returns
I understand the frustrations with questions unrelated to the job you'll be performing, such as asking about balancing b-trees when you're applying to make frontend CRUD apps.
But trick questions bother me so much more. This one in particular is something you'll probably encounter if you do frontend development in a functional style for long enough, but it's still a trick question at heart.
- czardoz 9y agoWhat's the trick here?
- txttran 9y agoFrom: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... ['1', '2', '3'].map(parseInt); // While one could expect [1, 2, 3] // The actual result is [1, NaN, NaN] parseInt is often used with one argument, but takes two. The first is an expression and the second is the radix. To the callback function, Array.prototype.map passes 3 arguments: the element, the index, the array. The third argument is ignored by parseInt, but not the second one, hence the possible confusion.
- crimsonalucard 9y agoThis shows how badly javascript was designed. Functions when used improperly should throw an exception not "sort of work."
- reificator 9y agoTo take up the mantle of Devil's Advocate: Neither of these are doing anything wrong on their own[1]. When you go to map something you want to be able to access the index sometimes. When you parse an int, you sometimes want to be able to specify a base. Maybe in a strongly typed language you might use an enum with valid bases instead of an int, but that doesn't make sense in a dynamically typed language[2]. Maybe you use different function names if you want to parse an int in decimal vs an arbitrary base, I can't argue with that, but overloaded functions/methods are the norm and not the exception in most languages I've used. [1]: Except parseInt's default radix which is ambiguous and the cause of countless bugs. But this bug is because we ARE specifying it. [2]: And no, being dynamically typed is not inherently bad design.
- dragonwriter 9y agoIt makes sense to have a function that is like the common functional “map” but passing the index as well, it's probably not a great design to have it be called “map” and have the basic, no-index behavior rely on the passed function taking only one arg, especially in a language where functions that usually take one arg but also have optional args are common.
- crimsonalucard 9y agoIn terms of design I'm talking about an exception not the arity of the function. A hidden NaN in an array of numbers is like a javascript promise for an error in the future. This isn't IO so it's better to kill it now rather than killing it sometime in the future. Actually, to be honest will NaN even launch an exception? Does 1 + NaN cause an exception or does it propagate more NaNs all over the code? Imagine you are debugging a 10000 line function that's suppose to return an int, but instead returns a NaN. Where did this error occur in the 10000 lines? Sure, I can say all kinds of stuff to make finding this error sound simple but if exceptions were thrown instead of assigning NaNs to some variable life would be easier. The exception would tell me the exact line of the illegal operation while a NaN tells me nothing. Javascript treats the "NaN" type as if it's a number... in the sense that when you do a math operation on something that is a NaN you get another NaN... The reality is... the very abbreviation "NaN" stands for is "Not a Number" so the addition of a number and something that is not a number should not even occur. Seriously? They should have called it "LoL" or "WtF" because these are more appropriate abbreviations.
- deleted 9y ago[deleted]
- reificator 9y agoLook, misattributing the introduction of NaN to JS is not going to help make the case that JS is poorly designed[1]. JS uses double-precision floating point numbers, based on the IEEE 754 standard first published in 1985. [1]: Though there is a case that using doubles for all numbers is poor design, that's a different topic altogether.
- reificator 9y ago`parseInt` takes two arguments, the string to parse and an optional radix. (or base) `Array.prototype.map` takes a function and calls that function for each element in the array, passing in the value, the index, and the array itself. That means that your 0th element will be parsed as you would expect, your 1st element will be parsed as gibberish, your 2nd element will be parsed as base 2, your 6th element will be parsed as base 6, etc....
- yelnatz 9y agoYou should rephrase your first sentence since both arguments are required, and your statement can be interpreted otherwise.
- reificator 9y agoI have never seen a browser that requires the radix, and MDN specifically calls out that you should pass both arguments or you will get unexpected results. They wouldn't need to do that if the radix was required. Now, should you treat the radix as optional? Absolutely not. I've got several commits on newly adopted codebases where I went through and explicitly used `10` as the radix anywhere `parseInt` was called. But is it technically optional? Absolutely. Proof: https://i.imgur.com/BnAiN8s.png https://i.imgur.com/BnAiN8s.png
- reificator 9y agoI forgot to mention, the worst part about trick questions is that you never quite know what's meant to be a trick question and what's not, and handling that gap can be extremely tricky. For example: >> Which C++ data structure would you use to store a sorted list of 5,000 integers, a linked list or a vector? The values will be randomly generated one at a time, and after inserting an integer the list must be in a sorted state. > I would use a vector because of its compact and linear nature. It would- >> Sorry, the correct answer is a linked list. With an array if you want to insert something in the middle it's going to require you to move all elements that come afterward. Linked lists on the other hand have a constant time cost for insertion and removal. > But the cost of constant cache misses while traversing the linked list are going to significantly outweigh the cost of shifting the array by one space. And on top of that the 3-4x overhead is going to push nearby nodes out of the cacheline. >> I'm not convinced. > We could benchmark it together, or find an existing benchmark online. >> We're on a strict schedule here, so let's just move on to the next question. > No thanks, I've realized I wouldn't be a good fit for this position. Thank you for your time and best of luck. How do you handle a situation like that, where you're asked a trick question but the interviewer doesn't know the trick? It's difficult to find a balance between assertiveness and argumentation. Do you insist on giving the correct answer and backing it up with facts? Or do you tend toward conflict avoidance, taking the loss on one question and hoping to make it up on the rest? When you're on a team, you can plan your approach based on the personalities and culture around you. But in an interview you usually have minutes of experience talking to your interviewer. Would they be impressed at your knowledge, confidence, and assertiveness? Or will they perceive your confidence as egotism, your assertiveness as an issue with authority, and your knowledge as incompetence? The correct answer varies based on the interviewer's personality and your tactfulness, but realistically you need to either stand your ground or walk away. If you acquiesce, you're setting yourself up for a miserable work environment.
- mehrdadn 9y agoI'm confused, aren't both of those answers flat-out wrong? If you want a dynamically sorted data structure shouldn't you be using a BST?
- 9y ago
- adrianratnapala 9y agoIt would be bad as the lone question. A candidate who whappens to not spot the "789a" will get it wrong, and you don't get any info about the quality of the candidate. But it is a good question if there are other fall-backs because you get lots of info in the case where the candidate gets it right. Spotting the "789a" shows an eye-for-detail (important!). The candidates' explantation of the consequence tells something you about how well they know the environment, and how well they can logically think about cause and effect.
- reificator 9y agoIf the correct answer was [123, 456, NaN...] then it would actually be a good question for the reasons you bring up. The thing that makes it a trick question is that map calls the given function with the value, the index, and the original array. And parseInt takes a value and a radix. Which means your 4th entry is parsed in base 4, your 8th is parsed in base 8, etc.... So the correct answer is [123, NaN, NaN...], and that makes it a trick question.