News for hackers. Stuff that matters.
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @12:16AM (#31546328)
> or asking obnoxious questions ("Why did you use a foreach and not a for loop?")

you should be able to explain why you wrote something the way you did; that's not an absurd ask.

perf characteristics of different iteration styles are often significant to the task at hand.

Re:In defense of coding interviews by vorpalhex on Sunday May 29, 2022 @01:34AM (#31546686)
Your problem has gone off the rails if that level of performance difference is important.
Re:In defense of coding interviews by koonsolo on Sunday May 29, 2022 @02:49AM (#31547007)
For me it wouldn't be about performance but about readability.
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @01:38AM (#31546707)
try rendering a dynamic React dashboard with 30 line, scatter, and heatmap charts, each containing 3 series of 10k points each, several times per second. welcome to what Grafana can do :)
Re:In defense of coding interviews by onion2k on Sunday May 29, 2022 @02:13AM (#31546868)
If someone suggested that I ought to be able to solve a problem like that in an interview, rather than solving it over the course of several years with a team of developers (like Grafana did) then I'd stop the interview and remove myself from the hiring process.

No doubt there are situations where hugely optimized code is necessary. An interview isn't one of them unless you're hiring someone to write exactly that code. Most companies aren't.

Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @02:29AM (#31546945)
> No doubt there are situations where hugely optimized code is necessary. An interview isn't one of them unless you're hiring someone to write exactly that code. Most companies aren't.

knowing the difference, and consciously deciding that it doesn't matter for a given task is often a strong positive signal. hopefully a good interviewer will not actually ask you this question if it doesnt matter for the given task.

Re:In defense of coding interviews by onion2k on Sunday May 29, 2022 @03:50AM (#31547289)
I don't agree. If someone brought that up in an interview I'd just think "oh, they know a bit of JS trivia that won't impact any of the code they'd write here." and ignore it. It's not a signal at all, let alone a strong one.

If they mentioned the fact that React 18 batches updates in concurrent mode which would greatly improve the responsiveness of a dashboard app that's updating lots of small components frequently, or if they suggested the graphs might be better written using GLSL shaders to move the rendering to the GPU so JS only needs to update a uniform array, then I'd pay attention. Potential new ways to approach a problem or changes to architecture to avoid the problem entirely rather than micro-optimizations are much more interesting signals about someone's deeper knowledge of building things.

Re:In defense of coding interviews by thaneross on Sunday May 29, 2022 @01:49AM (#31546761)
Yes, a regular for loop is a bit faster at iterating over a large list of elements than foreach in Javascript, but why on earth would you care about that kind of optimization minutia in a coding interview?

The question you're asking involves 10k * 3 * 30 = 900,000 data points. If a candidate wants to simply loop over all of them multiple times a second, I'd say that's a much bigger problem.

Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @01:53AM (#31546771)
> If a candidate want to simply loop over all of them multiple times a second, I'd say that's a much bigger problem.

unless the task is to animate them in a rAF loop (which runs at ~60Hz), right? the task matters. in some cases the question is stupid & irrelevant, and in the other case it means getting the job done, or not.

the difference between the brain-dead way and the fast way is literally 30x (53ms vs 1.8ms on my machine): https://jsfiddle.net/3ho6f5k1/

Re:In defense of coding interviews by drewrv on Sunday May 29, 2022 @02:44AM (#31546985)
It’s unrealistic to expect this level of optimization in a generalist whiteboard session. For a specialist, maybe. I’m not specialized in this area.

For an array of 5-10 items the performance hit of foreach is worth it for readability. Maybe an interesting discussion could occur about when to sacrifice readability for performance but you won’t get that by grilling people about minutiae.

Re:In defense of coding interviews by zepolen on Sunday May 29, 2022 @03:58AM (#31547323)
> For an array of 5-10 items the performance hit of foreach is worth it for readability

As always it depends, maybe that component is being called 500 times on the page making it actually 5,000 invocations.

I'd FULLY expect a candidate to understand the difference and would ask in a whiteboard session. There is a lot of bloated slow javascript libraries out there and they almost always stem from the fact that most devs don't realize how much overhead function calls introduce.

For the interview, there is no discussion necessary, the correct answer is simply:

"I used a foreach for readability, because performance doesn't matter in this case because X."

or

"I used a for because performance matters in this case because Y"

End of story.

Re:In defense of coding interviews by aulin on Sunday May 29, 2022 @04:28AM (#31547460)
one could argue that it's a job for the compiler to make sure two equivalent language constructs (map and for loop) perform similarly...
Re:In defense of coding interviews by barrkel on Sunday May 29, 2022 @06:06AM (#31547940)
Foreach with functions being passed being slower is an implementation detail. You don't see it in Rust, where the block body is inlined like a generic, and it's perfectly feasible for a dynamic runtime to specialize the pattern and inline based on it being a common practice, never mind polymorphic inline caches which in principle permit inlining in this case.
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @02:56AM (#31547045)
> For an array of 5-10 items the performance hit of foreach is worth it for readability

100% agree. my point is more about OP's example being used to prove that dumb questions get asked. if it's being asked by a good interviewer, it's usually relevant.

Re:In defense of coding interviews by aulin on Sunday May 29, 2022 @04:24AM (#31547446)
Not sure it proves anything in interview context though. It's a language specific trivia you'll quickly learn either from experience or from a good mentor. Proves nothing about your ability to code or to be a successful member in the team. Unless you're actively looking for a candidate that knows all those JS optimization tricks.
Re:In defense of coding interviews by Gwypaas on Sunday May 29, 2022 @05:36AM (#31547793)
Take Rust. For each and map generally are faster because they often allow bound checks to be elided.

100% trivia.

Re:In defense of coding interviews by barrkel on Sunday May 29, 2022 @06:25AM (#31548006)
Constant factors don't normally matter in an interview algorithm context. Focus on them often results in ugly code and can be a distraction against bigger improvements.
Re:In defense of coding interviews by bluepizza on Sunday May 29, 2022 @05:57AM (#31547896)
I would care about the reading optimization. So if someone tells me "I chose a forEach instead of a for because it provides almost the same performance, but is more readable, and my code does not need the index", that's a very good sign of someone making conscious choices.
Re:In defense of coding interviews by vkou on Sunday May 29, 2022 @03:22AM (#31547160)
If you can't give me the o(n) performance of the algorithm that you just wrote, this is a pretty serious red flag about your ability to understand what you are writing.
Re:In defense of coding interviews by krainboltgreene on Sunday May 29, 2022 @04:45AM (#31547544)
I'm going to blow your mind but a ton of important software was written by people who have a hard time explaining these things.
Re:In defense of coding interviews by mikebenfield on Sunday May 29, 2022 @11:07AM (#31549714)
Also a ton of important software was written as a giant mess of spaghetti code, or with horrible security vulnerabilities.
Re:In defense of coding interviews by krainboltgreene on Monday May 30, 2022 @08:56PM (#31565145)
I've never been tested on my ability to not write spaghetti code or write security vulnerabilities in my 15 years doing many many interviews.

Unless you're suggesting that knowing the performance of a loop helps with that?

Re:In defense of coding interviews by mikebenfield on Tuesday May 31, 2022 @10:27PM (#31577370)
What I'm suggesting is that "Important software has been written by people who couldn't/didn't do X" is not a very strong indication that X is not a valuable quality that employers should want in their engineers.
Re:In defense of coding interviews by Voloskaya on Sunday May 29, 2022 @03:40AM (#31547247)
Except for the fact that using foreach instead of for has no impact on your O(n) performance.
Re:In defense of coding interviews by arethuza on Sunday May 29, 2022 @04:10AM (#31547383)
What would you do if a candidate said with a smile "I'm pretty sure it is O(n!)"?
Re:In defense of coding interviews by enedil on Sunday May 29, 2022 @07:16AM (#31548207)
Parent commenter asked for little o, not for big O. So to be safe, better say n^o(n^2).
Re:In defense of coding interviews by robertlagrant on Sunday May 29, 2022 @02:08PM (#31551292)
Hah this is the best comment.
Re:In defense of coding interviews by xigoi on Sunday May 29, 2022 @11:17AM (#31549783)
“So you want to put code into production without having a better estimation of its time complexity than O(n!)?”
Re:In defense of coding interviews by tshaddox on Sunday May 29, 2022 @01:27AM (#31546648)
If the choice is relevant to the task, then sure. If it’s an arbitrary choice then the question is meaningless. It’s like asking “why are you wearing a blue shirt?” when you would have asked “why are you wearing a black shirt?” had they been wearing a black shirt.
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @01:30AM (#31546667)
i'm not claiming that this is a good question for every case. but there are many times where the choice of loop makes a significant difference. asking this question for a task that only requires iterating 3 elements every 1000ms would indeed be quite stupid on the interviewer's part.

it always amuses me when people use these seemingly stupid questions (out of context) to prove how broken tech interviews are. but of course, if your job will be write high perf code, then suddenly they question is incredibly relevant.

to be clear, i agree that there are a lot of stupid questions that get asked which are not relevant to the actual job you'll be doing (exceedingly few SWEs will need to implement a merge sort as part of their job)

Re:In defense of coding interviews by light_hue_1 on Sunday May 29, 2022 @12:37AM (#31546437)
> perf characteristics of different iteration styles are often significant to the task at hand.

This ridiculously untrue.

It's also a great example of how dumb current interviews are. Someone like this poster could easily interview you, and now what? Do you just play along or begin to explain how incredibly wrong they are?

Re:In defense of coding interviews by pkolaczk on Sunday May 29, 2022 @02:01AM (#31546810)
This is untrue in languages with proper optimizing compilers, designed for performance, e.g. C++, Rust, Zig. A functional iterator transformation chain is often just the same performance as a "traditional" for loop.

But try that in Java and you'll often see a 3x-10x difference, especially if you run into problems with boxing. Streams are known to be notoriously slower than for loops with index. I once have even doubled the speed of a for loop by unrolling it manually (Java 8). Not all compilers are equally strong at optimization.

Anyway, asking such questions in an interview for a junior/regular developer is just a distraction.

Re:In defense of coding interviews by Dayshine on Sunday May 29, 2022 @06:45AM (#31548089)
Even in java/c#: what kind of application has its performance dominated by a control structure? If you're doing so little work inside the loop then you should probably look at using fancy cpu instructions like SIMD
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @01:10AM (#31546585)
> This ridiculously untrue.

at least for JS VMs, this is true today. this answer will certainly vary for other languages/runtimes.

i profile and optimize a lot of JS/TS code that handles datasets with millions of datapoints at my day job. but you don't need to take my word for it; this claim is not exactly difficult to verify.

> Do you just play along or begin to explain how incredibly wrong they are?

in this case, i'd prefer to be proven wrong with code, rather than prose.

Re:In defense of coding interviews by vorpalhex on Sunday May 29, 2022 @01:26AM (#31546644)
It's probably time to move into a faster language if you're getting caught up in language specifics on loop execution.
Re:In defense of coding interviews by leeoniya on Sunday May 29, 2022 @01:34AM (#31546688)
quite impossible to move into a faster language when it would

1. require a complete rewrite of an existing, massive codebase

2. where the primary task is rendering interactive charts for a React web app

Re:In defense of coding interviews by light_hue_1 on Sunday May 29, 2022 @11:05AM (#31549694)
> at least for JS VMs, this is true today. this answer will certainly vary for other languages/runtimes.

Total nonsense. It is not true of JS today, not has it ever been true.

No modern web app's or website's performance is dominated by the performance of what loops you choose. It isn't even remotely meaningfully impacted by that; by this I mean, any differences are totally unmeasurable beyond some 3 line micro-benchmark.

You have never benchmarked or profiled your code.

Of course, tight inner loops for image processing or ML, etc. yes, this can make a difference. But we're only talking about a handful of loops that matter out of many tens of thousands an application may have, you are almost certainly never writing those loops, and that's a specialized field that basically has nothing to do with the web. And it isn't what you're talking about.

> in this case, i'd prefer to be proven wrong with code, rather than prose.

What absurdity. And this is exactly when interviews suck. Because people have strongly held beliefs that are ungrounded in reality, and since there are no objective measures by which to rate interviews you need to pretend that they're real, because a lot of interviewers are basically just testing "cultural fit".

But hey! I have to say. That if you brought this up in an interview. I would at least know to never take the job!