For-loop even number guy can get lost. Sleep-sort guy I would at least be interested to see the rest of the test. As long as he has an awareness of when not to do that kind of thing in production code, it actually might indicate he has some ability.
For-loop even number guy can get lost
I don’t get this. Who cares if they forgot or just didn’t know the better solution? They were able to solve the problem under pressure, even if not in the most optimal way. They even remembered to handle the negative case, which is straight up a good sign (no pun intended). And while it’s slow, it would honestly still be fine to use in production anywhere you’d use typescript, as long as it’s not in a hot spot.
In my mind this tells me approximately as much about the programmer as if they wrote it using modulo.
It’s really just not a good solution. It’s more complex, it’s way slower, it shows they’re not familiar with stuff they really should be familiar with.
Anyone who struggles to write a function to determine evenness of a number, and has to throw in some crazy solution to be able to make it across the finish line, is going to really struggle with real production code. They’re going to produce stuff that’s going to make everyone’s job a lot more difficult.
return (x % 2) == 0; return (x & 1) == 0; return (x / 2 * 2) == x; return x/2 == (x+1) / 2;
You can come up with unusual solutions and it can be fine. You can’t come up with cockamamie solutions, that’s why you’re doing the test.
That sort is pretty bonkers. I agree with the no hire because it doesn’t really display an understanding but if I got that as an answer in a technical interview, it would be fun to dive into why that code was written and might turn into a hire if we peer just a bit deeper under the hood of the thinking. This is why “solve for x” technical questions suck unless you make the actual interview about the back and forth. Assume a correct premise if it works and tweak and change and poke and prod to really sus out if they understand what and why that was written.
That’s honestly so cool lol. Fun to see logic problems solved in really inventive ways
const isEven = (n) => !(n & 1)
I don’t have any interview stories. Alas, I’ve only given a couple of interviews. (Or maybe rather fortunately I’ve only given a couple of interviews, depending how you look at it.)
But I do remember a first code review for one guy. Dude implemented his own bubble sort rather than using a sort method from the standard library. Aside from that one interesting choice, he was a good coder as I recall. He’s a manager now.
I did a review of someone’s code submission once where they pretty much wrote a bespoke JQuery implementation to handle the super-complex task of incrementing a number in a div when a button is pressed. I don’t think they got the job. I felt a little bad for them, but a valid part of those coding interviews is “Will this person go off and do something crazy like write their own half-baked React instead of just using React?”. Showing that you know how to write code as simply as possible is a big plus.
I have yet to administer or discuss interviews where using an api was an acceptable answer. Granted my experience is more on the engineering side but it seems odd to me to not expect someone to implement their own algorithm. If they had 60 seconds to answer a multiple choice technical question on a prescreen online test, maybe. But it seems that person demonstrated their understanding on a technical level that should have been expected and explored?
Oh. Oh no. You misunderstand. My story was about one of the first pull requests a guy opened after he was hired. He put his home-grown bubble sort in code that would have ended up in production in an e-commerce application had I not pointed him to the sort functionality in the standard library.
Oh my, I definitely missed something when reading lol. That’s insane behavior! I hope he learned from the mistake.
Why does the sort one work? 🤨
It doesn’t, the array is still in the same order it started in, it’s members are just printed to the console in numerical order.
It just prints the number 1 after 1 ms, 2 after 2 ms, 3 after 3 ms etc.
Instead of print you could add them to a second array, though. Even clear the original first for “in place” sorting; never mind the memory allocation for the lambdas.
Sorry, but, I think all my coding would go out the window and I’d start making some nonsense up if she was interviewing me.
I’m sorry, but even without knowing about the mod operator, this is inefficient and over engineered. Why have a loop at all?
fun isEven(n: Int){ return n == abs(n) }
no loop required…
having said that, I can totally see how that was missed in a high pressure interview. I hate interviews like that!
edit: Ha ha… isEven…not isPositive… I’m tired. ignore me!
Because the abs(3) == 3 is true and that isn’t even.
An even number of flips would be true and an odd number of flips would be false which works out.
I was thinking a bitwise & or converting it to a string and testing if the right most character is 0, 2, 4, 6, 8 would be panic mode solutions too.
you might be able to do it with a bitwise op? My track record tonight is not great so I’m not going to comment. Have a look at @ImplyingImplactions comment for a loopless solution
Bitwise and with 0x1. If result is 0, it’s even. Least significant bit is always 1 for odd numbers.
I’m not following.
n == abs(n)
only tells you if it’s positive.yes, I’m being a dumb tonight. I’m completely wrong
Haha, s’all good, yo.
Lol I think the interview pressure got to you.
Ha ha ha, yup… whoops!
That would be isPositive.
Without using the modulo operator you’d essentially have to reimplement it. Divide the number by 2 and round down. Multiply that by 2 and then subtract it from the original number.
isEven(10) results in 10-10==0 (true) whereas isEven(13) results in 13-12==0 (false).
function isEven(n){ n = Math.abs(n) return (n - (Math.floor(n/2) * 2)) == 0 }
Yep! I’m wrong. Pretty embarrassing!
That’s a nice solution though! Gonna have to try and remember that one!
Minor simplification: this works even without taking absolute value first of you use fix instead of floor.
Edit: I don’t know if fix is in the stock math library on second thought…