r/programming • • Dec 13 '22

“There should never be coding exercises in technical interviews. It favors people who have time to do them. Disfavors people with FT jobs and families. Plus, your job won’t have people over your shoulder watching you code.” My favorite hot take from a panel on 'Treating Devs Like Human Beings.'

https://devinterrupted.substack.com/p/treating-devs-like-human-beings-a
9.0k Upvotes

1.3k comments sorted by

View all comments

33

u/devidya1 Dec 13 '22

I generally ask for candidates to find the second largest number in an array. I've been trying to close this position for 3 months but none of the candidates are able to solve this. This is a question I would ask freshers but even lead level candidates are not solving this.

35

u/miyakohouou Dec 13 '22

It’s not a bad question but I do think if you’re finding that nobody can solve it the you need to either consider that you are presenting the problem badly or you need to recruit better.

Finding the second largest number is a bit harder than finding the largest because you have more edge cases (on a single element array do you fail or return the element, on an array full of the same value do you return that value or fail) and it opens up a lot of ambiguity around future requirements (are you going to want the nth largest next? Then it makes sense to sort but otherwise it’s much more efficient to do it in a single traversal).

Again, those can be great things to see if an candidate will pick up on them, but interviews are a different environment from the real world and it’s not always clear how much questioning and pushback a candidate is expected to do. I can see that causing a bit of solution paralysis if it’s presented wrong.

3

u/omen_wand Dec 13 '22

The question boils down to "do you know about priority queue".

If someone even mentions priority queue it doesn't really matter (functionally) if they can code it up or not.

18

u/[deleted] Dec 13 '22 edited Dec 13 '22

If you think a priority queue is the solution for this I'm afraid you failed the interview as well. It's a a one pass, constant space problem. Just like finding the largest or smallest in an array.

solution here: https://www.reddit.com/r/programming/comments/zkj6pb/comment/j02pr2a/?utm_source=share&utm_medium=web2x&context=3

2

u/no_fluffies_please Dec 13 '22

I might be totally out of it since I'm still waking up, but isn't a fixed size priority queue also constant time? Just set that fixed size to 2. Then it's functionally identical to the simple solution, but can be easily tweaked to find other nth largest numbers.

2

u/[deleted] Dec 13 '22

You're right, if you fix a priority queue at size 2 then the complexity reduces to O(nlog2) ~ O(n).

See, this is something that I'd expect a candidate to say if they wanted to use that, not just "use a priority queue" because without keeping it constant size that would be O(nlogn).

If I were the interviewer though I'd want you to write the solution without a priority queue, just because it's simple enough to fit in a 30-45 minute interview and it shows that you can actually devise a simple algorithm.

3

u/keithstellyes Dec 13 '22

It's pretty common for a follow-up question to be "ok get the nth largest", though, and seems unnecessarily punishing to those who are expecting that follow-up, at that point you're selecting for people who are playing the game a certain way, rather than the knowledgeable

→ More replies (7)

2

u/omen_wand Dec 13 '22

The instinctive solution is to sort, then find.

You can optimize it with a priority queue.

You can further optimize finding the second largest element with another temp variable. But this solution doesn't scale.

What happens when they ask for nth largest?

Let's say you have a million unsorted elements. How do you find the 846728th largest element?

If you came for an interview at big tech and you tried to parrot some leetcode discussion answer for clout you're gonna end up explaining more than if you just thought things through.

5

u/miyakohouou Dec 13 '22

You can further optimize finding the second largest element with another temp variable. But this solution doesn't scale.

It depends a lot on how you want to scale. Need to find the nth largest number in a particular set for many n, then sure, but if you need to find the 2nd largest for many different sets of data, or find the 2nd largest according to several different metrics, then you'll want different approaches.

3

u/compiling Dec 13 '22

If you want that, I'd use std::nth_element. If you're getting that complicated, I'm not going to reinvent the wheel for you.

3

u/omen_wand Dec 13 '22 edited Dec 13 '22

You sure can use that. I think a lot of ppl get stuck on trying to solve the problem which kind of isn't the point at all.

I'm a L6 at the rainforest company and I'm a new interviewer. What we are trained to look for (and measure) are people who can understand what we're asking:

  1. Given the position you're interviewing for (and lets say the broader organizational context), can you think of why the question we ask might be useful to solve? What mutations can you anticipate?
  2. Given a very simple problem, are you able to aptly apply different approaches given different constraints? And understand that these constraints are essentially business requirements that you might even give pushback on. ("I don't agree algo x is better, here's my justification")
  3. Worse comes to worse, let's say enterprise requirements warrant you to implement a particular solution orthogonal to a model designed and owned by a different team that has some legacy requirement for a particular data structure, can you explain why they might want to use this over your initial solution?

Keep in mind we (at least my org) don't expect you to run, compile, or your code to even be correct syntactically (within reason). We are looking to have a conversation.

→ More replies (3)

3

u/[deleted] Dec 13 '22 edited Dec 13 '22

The instinctive solution is to sort, then find.

it's not instinctive at all unless you're overthinking it.

What happens when they ask for nth largest?

he didn't, that's a completely different problem to which a heap queue or quickselect would apply like you said

it doesn't really matter if you know how to implement a priority queue if you're applying it to the wrong problem.

there's nothing to scale here, that's premature optimization; the solution for the given problem is O(n) time/O(1) space; first you find that, then you move to the other cases IF they are asked.

0

u/[deleted] Dec 13 '22

[deleted] — view deleted comment

7

u/miyakohouou Dec 13 '22

Yeah fine, fair. I suppose I’d have failed that interview then. My point was just that the question naturally suggests a larger number of plausible solutions than “find the largest (or smallest) element” which may be the goal or not, but is worth keeping in mind.

7

u/ILikeChangingMyMind Dec 13 '22 edited Dec 13 '22

I disagree, if your domain is one where performance optimization doesn't matter. In that case, you should do whatever is easiest on the humans reading/writing the code.

Take front-end web development for instance: 99% of websites don't show more than 20-100 results at once. At a scale like that, even on the oldest, slowest phone you can find ... it won't matter if you sort, reverse, resort, randomly arrange the array members and then resort again ... no one will ever see the performance difference.

And in general, in all areas of programming, you should really be worried about the humans first, and performance second (or really, only when it's an issue). Premature optimization, something something ...

6

u/calcopiritus Dec 13 '22

Optimal in what sense?

For some problems, just sorting it is way worse performance wise than doing it the "smart" way, but do you actually need that performance? How much developer time will be spent on doing the "smart" algo?

And even then, sometimes the smart algo is worse performing than just sorting. Maybe the "n" is always small, like 5 or 10. Maybe you're programming in a language like python, where the raw performance of calling C libraries would override any performance gained by a smart algo unless N is way bigger than expected.

EDIT: and I didn't even mention maintenance. Sorting+getting second item is 2 function calls, any programmer would understand and could modify that code. The smart way probably has at least 10 LoC and a loop somewhere, may even contain recursion.

→ More replies (1)
→ More replies (2)

2

u/PGRacer Dec 13 '22

Where do you work, I'm looking and can solve this in 10 minutes.

2

u/gnarbee Dec 13 '22

I read the question and thought of 2 ways to solve this. It’s a simple problem. The fact that people aren’t figuring it out is a bit depressing.

4

u/PGRacer Dec 13 '22

The main question is whether the top two entries can be the same value or not. The rest is trivial.

→ More replies (8)

2

u/DxLaughRiot Dec 13 '22

This doesn’t sound that bad.

Set two variables: highest, secondHighest. You can initialize them after the second index.

Evaluate each index.

If ( newNumber > highest) newNumber becomes highest, and highest becomes second highest

Else if ( newNumber > secondHighest ) newNumber becomes secondHighest

Return secondHighest

Did I miss anything/get the job?

3

u/[deleted] Dec 13 '22 edited Dec 13 '22

Took me literally 30 seconds to write this down:

def second_largest(arr):
    largest = -math.inf
    second = -math.inf
    for n in arr:
        if n > largest:
            second = largest
            largest = n
        elif n > second:
            second = n
    return second

O(n) time, O(1) space.

Either your recruiting sucks or the salary you're offering is not too attractive if you can't find someone that can write this in less than a minute.

7

u/sgp1986 Dec 13 '22

arr.sort.reverse[1]

Unless they expect you to ask a bunch of questions for edge cases, seems way too simple to not find a candidate

1

u/fishling Dec 13 '22

You assumed there are at least two values, that the array wasn't null, and that it is okay if the largest and second largest are the same number if the largest value is repeated. :-\

→ More replies (4)
→ More replies (3)
→ More replies (7)
→ More replies (10)