this post was submitted on 21 Dec 2024
71 points (93.8% liked)

Fun Loops ▶️

282 readers
310 users here now

Posting interesting/cool/funny videos from Loops here

https://loops.video/

Thanks to @Bluefruit @lemmy.world for the icon and banner!

Discussion of and questions about Loops should go over in [email protected]

If the loop you're posting isn't original content uploaded by the creator, prefix the title with [R] for "reposted". For example, [R] Cute dog.

Rules:

  1. Don't be a dick
  2. Don't make me add more rules

founded 1 month ago
MODERATORS
top 34 comments
sorted by: hot top controversial new old
[–] [email protected] 7 points 16 hours ago

const isEven = (n) => !(n & 1)

[–] [email protected] 13 points 20 hours ago

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.

[–] [email protected] 23 points 1 day ago (1 children)

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.

[–] [email protected] 4 points 13 hours ago (1 children)

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.

[–] [email protected] 7 points 12 hours ago

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.

[–] [email protected] 18 points 1 day ago* (last edited 1 day ago) (2 children)

Good news: The Loops embed feature has moved to beta.

https://tesseract.dubvee.org/c/[email protected]

If anyone is willing/able to test through the hosted instance (linked above) and submit any bugs they may encounter, it would be greatly appreciated :)


On the array sort....that is clever but it doesn't return those values to something you can use; it just sorts them on screen. I respect the cleverness, but yeah, not useful.

[–] [email protected] 3 points 13 hours ago

On the array sort…that is clever but it doesn’t return those values to something you can use; it just sorts them on screen. I respect the cleverness, but yeah, not useful.

This is not the reason it isn't useful. The function could just as easily modify an array as print; you could also keep a list (or an atomic integer if that functionality is available) of which have been sorted and spin until it shows that every member has been sorted. Taking n milliseconds to sort an array whose highest element is n, with the cost of all of the thunks, plus the potential for a (very rare) race condition... not so great.

Regardless, I'd hire either of these programmers if there weren't red flags elsewhere.

[–] [email protected] 4 points 1 day ago (1 children)
[–] Cris_Color 5 points 1 day ago* (last edited 1 day ago) (1 children)

Hey, is there any chance it might make sense to slow down the rate at which loops are posted?

I really enjoy seeing them on my feed, but if I'm subbed to the loops comm it just floods literally everything else and every other post is a loop with a vertical thumbnail twice the height of other posts. Edit: just checked and it's actually several loops for every other post 😅

If it's not something that makes sense for the comm that's fine, I just wanted to ask :) engagement seems like it might also be a bit higher with a more limited selection of posts

Hope you have a lovely day :)

[–] [email protected] 4 points 22 hours ago (1 children)

Thanks for the feedback! Would it help if I used something like schedule.lemmings.world to spread the posts out over the day?

[–] Cris_Color 2 points 21 hours ago* (last edited 21 hours ago)

I think that would help a lot! There are just so many of them in quick succession that it's really easy for them to drown out all the other stuff I'm subscribed to

Thanks for the work you do running this community and posting stuff for folks, it's appreciated :)

[–] [email protected] 5 points 19 hours ago (1 children)

Why does the sort one work? 🤨

[–] [email protected] 15 points 19 hours ago (2 children)

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.

[–] Randelung 7 points 17 hours ago

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.

[–] Maalus 2 points 15 hours ago

Also since you aren't guaranteed that there is a "happens before" relationship, it's a wrong implementation that returns unsorted arrays in text too.

[–] Cris_Color 10 points 1 day ago

That's honestly so cool lol. Fun to see logic problems solved in really inventive ways

[–] TootSweet 7 points 23 hours ago (2 children)

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.

[–] [email protected] 5 points 22 hours ago

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.

[–] [email protected] 3 points 20 hours ago (1 children)

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?

[–] TootSweet 1 points 6 hours ago (1 children)

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.

[–] [email protected] 2 points 2 hours ago

Oh my, I definitely missed something when reading lol. That’s insane behavior! I hope he learned from the mistake.

[–] [email protected] -4 points 21 hours ago (4 children)

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!

[–] [email protected] 8 points 19 hours ago (2 children)

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
}
[–] [email protected] 4 points 14 hours ago* (last edited 14 hours ago)

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...

[–] [email protected] 5 points 18 hours ago

Yep! I'm wrong. Pretty embarrassing!

That's a nice solution though! Gonna have to try and remember that one!

[–] [email protected] 11 points 21 hours ago* (last edited 20 hours ago) (1 children)

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.

[–] [email protected] 2 points 18 hours ago (1 children)

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

[–] [email protected] 2 points 15 hours ago

Bitwise and with 0x1. If result is 0, it's even. Least significant bit is always 1 for odd numbers.

[–] braindamagebuddy 9 points 20 hours ago (1 children)

Lol I think the interview pressure got to you.

[–] [email protected] 6 points 18 hours ago

Ha ha ha, yup... whoops!

[–] [email protected] 8 points 21 hours ago (1 children)

I'm not following.
n == abs(n) only tells you if it's positive.

[–] [email protected] 6 points 18 hours ago (1 children)

yes, I'm being a dumb tonight. I'm completely wrong

[–] [email protected] 3 points 14 hours ago

Haha, s'all good, yo.

[–] Orbituary 0 points 21 hours ago

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.