Hacker News new | ask | show | jobs
by tloup 1606 days ago
Am I the only one just googling for this stuff because it's way faster? like, "maximum value in array" gives:

var max = arr.reduce(function(a, b) { return Math.max(a, b); });

which is totally valid for javascript and also pretty neat. I would probably do some funny things with loops instead, wasting time and resources.

8 comments

That particular question was not supposed to be a trick question. If the answer given in, say, Python was 'max(array),' I would have found the answer acceptable ('max' is an intrinsic function in Python); a better answer would be writing a for-loop. The person facing this question was an applied CS PhD (student at the time) at a Top-3 in Europe with prior working experience (IIRC).
That sounds an awful lot like panic not incompetence.
I didn't realise what it was like to panic in an interview until it happened to me - couldn't write code to detect whether two rectangles were overlapping even though I had been doing graphics programming for 10+ years at that point.

It was a CTO role and I'd just aced a presentation on "How to launch a new product" and I could not context switch to coding mode fast enough.

It happened to me once too, when I was still wet behind the ears, my mind went totally blank, could barely remember my own name. It didn’t help when one of the two men interviewing me said “look, we don’t want bullshitters here”.

I wonder if I’m one of the anecdotes about people in tech who can’t do the basics. That would at least amuse me somewhat.

I appreciate the sympathy, but, in this particular case, the question was part of homework, not on-the-spot pop whiteboarding. To this day, I find this curious. I asked folks at other firms, too, and apparently, this kind of phenomenon is common. Hence, all the damning statistics about the FizzBuzz.
My story: Years ago I went across country (ok it's UK!) for a position with an education startup. It was an all-day-er. The morning went well I aced the in person stuff, we gelled. Then I started to realise why I was there. Was I risking my marriage by taking a cross country position? Is the CEO just a bit too full of himself? What don't I trust about the product manager?

At the end is a hands on leet-code test. I cannot for the life of me remember what it was about. I do remember writing a function to replace 'abs', and realising that my brain was not even in the same county.

Yeah. People fail for all sorts of reasons. But when it's odd, maybe it is them. But beware. Maybe it's you.

I am glad I flunked hard in Bristol. I think I would have hated it. But then if I was a rubbish coder I would say that too.

Proctored homework? If not, I don't understand how he couldn't come up with a solution at all.
You can speculate but anyway there are all kinds of reasons a person can fail to do something that they are in fact capable of doing.

Sometimes people who have DECADES of walking experience just... fall.

No need to waste any time: Math.max(...arr)

Even when I know how to do something, I look it up just in case. Sometimes there's a cleaner approach, or I learn that I don't fully understand the problem.

In this case if you look it up on MDN you'll see that Math.max(...arr) isn't recommended for large arrays (ask me how I know)
I would suspect that. How do you know?

(this sort of discussion is what I'd love to see in a tech interview)

You're not the only one, but it's the equivalent of walking round a foreign country with a phrasebook versus being fluent in the native language. In this case, for example, you seem unaware that reduce is just doing a funny thing with a loop and won't be faster, but you've been seduced by shorter looking code because it looks more clever. There is, in fact, value in the reduce method but it's nothing to do with speed or efficiency (it's because it's declarative). But if this were Python, and the reduce was a call to numpy, then it would be about speed and efficiency. The point of doing stuff like leetcode is (or should be) to test real understanding of the technology, not an ability to copy/paste from StackOverflow.
But if you can grind these what's the point then? All I'm doing at work is building glorified crud apps. I'm sure it's what many, many software engineers are doing. Not all, but many. My understanding of e.g. Django is much more important to my daily work than being able to come up with the fastest way to find a maximum value in an array.
Nobody said anything about the fastest way to find a maximum value in an array. The comment you were replying to was just about finding a maximum value in an array. Idiomatic usage would presumably be a bonus but anything not ridiculously inefficient would be fine. This is absolutely something that someone building a CRUD app should know how to do without StackOverflow.
Sure, in a real world scenario you would Google the most efficient/elegant way to get the max item in an array. However in an interview situation, the interviewer is usually expecting a language agnostic approach, i.e. using loops.
That's my point, those questions are pointless. They don't show anything meaningful besides someones ability to learn a lot by heart. I mean, just based on the fact that someone asked if it was necessary to grind leetcode questions shows that you cannot gain any meaningful insight by asking questions like that.
Actually having to google something so basic makes your experience suspect. That's almost as bad as having to google for loop syntax in a language you say you know.
I have ADHD, I'm glad I can remember to not put the underwear on my head instead of where it belongs. Of course I can't remember how a for loop looks like, that's why some nice people invented code completion.
First, thank you to share so directly. HN is stronger for anecdata like this.

Let us assume that you are a competent worker that other firms want to hire. What is the best way to find people like you and to test/interview people with ADHD?

To be clear, I do not have ADHD, so I have no personal experience with this important topic.

I think homework would be fine, it really depends on the person. You can find people with ADHD who would crush these questions and others who wouldn't.

Personally... I had another profession before and was never asked anything during interviews. They just assumed I was capable because I had a degree, work experience and references. So I'm not quite sure where that even is coming from, it's unheard of in other professions.

Some companies want you to describe your solution, or say pseudo code is fine. But not every company will be completely honest about how much they actually do penalize for improper syntax.
You made two mistakes: You optimise prematurely and your assumption is incorrect.

Premature optimisation:

In most cases the performance would not matter. Instead of optimising for performance you should optimise for readability. IMHO IFs and FORs are way more readable in any context than any language specific methods like REDUCE and MAX.

Incorrect optimisation:

At least in modern browsers, simple IFs and FORs are the most optimised methods of doing anything. See for instance https://leanylabs.com/blog/js-forEach-map-reduce-vs-for-for_... (Operations per second, higher is better).

Conversely if I saw someone use a for loop (especially one with mutation) where a trivial usage higher order function existed, and I was giving a “leetcode” interview (which I refuse to do), I would take it as a signal that there is an important missing concept if it were not corrected when prompted.

The idea that “reduce” is language specific is frankly mind boggling. It’s no more language specific than a loop.

I am talking about my own time and resources, not those of a computer - say, it might take ~one hour to come up with a solution for problem x. It might very well be not the best solution either, just the one that works okay (we've all been there). Or I could look up what other people have done in 2 minutes - say I'm comparing solutions too, that would make it 10 minutes. That's 50 minutes of work I can spend elsewhere. Much more efficient.
I see. I completely agree.
Thank you for the nice exchange and learning opportunity though :)
As someone who prefers constructs like map, reduce, etc to loops this is a key example of why it’s important to a) ask why the candidate went that route and have a conversation about pros and cons and b) recognize that this is one of those no right answer opinion things and not hold it against them if they think different than you

Mind you I’m talking in general and now in browser specifically

Explicit loops are only more readable in languages that suck.
Well, the thread is about Javascript...

I take the exception that on this case in particular, the reduce is much more readable than a loop. Javascript isn't completely on the "anything non-imperative could as well be in machine code" league, but has plenty of problems that appear when you try to write other kinds of declarative code.

I'd go with `Math.max(...arr)` personally. Math.max is a variadic function, no need to add a reduce into the mix.
That implicitly calls Function.prototype.apply(), which will fail if given more arguments than an implementation-specific limit (https://stackoverflow.com/questions/22747068/is-there-a-max-...). Looping with reduce() avoids that restriction.
No, you are definitely not. But some insecure interviewer might feel this is an enormous red flag to be doing so.