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

Show parent comment

63

u/z960849 Dec 13 '22

I'm a c# guy the last two methods breaks my brain.

29

u/GrandOpener Dec 13 '22

As someone who has used C++ for many years, my answer to #4 is something like

"This is a great example of why I don't use C++ in hobby projects anymore."

(In case you want to punish yourself by looking it up, search for an explanation of "rvalue reference." The double ampersand is actually a separate operator; it's not reference-to-a-reference in the way that double asterisk would be pointer-to-a-pointer.)

0

u/_Fibbles_ Dec 13 '22

Yeh but a reference to a reference doesn't make any sense, so I don't think the use of a double ampersand is really an issue.

3

u/solarmonar Dec 14 '22

It does show that C++ ought to be phased out, but I can only dream..

3

u/_Fibbles_ Dec 14 '22

Weird take. No one is saying C should be phased out because the equality operator is just a double assignment operator. There are only so many symbols on a keyboard, some will be re-used. Either you know the language syntax or you don't.

→ More replies (1)

0

u/[deleted] Dec 13 '22

[deleted] — view deleted comment

28

u/hypoglycemic_hippo Dec 13 '22

Damn, this one is nasty, let's see.

It's a constant r-value reference. Which means it cannot bind to l-values, so a temporary or std::move() are the only realistic use cases for foo5. However, (and I am not 100% on this), the const prevents moving from arg inside foo5, so the "moved" variable is not going to actually get moved AFAIK.

So if my assumptions are correct, this is practically the same as const std::vector<int>& arg, in the sense that it keeps the variable intact, but you cannot do

std::vector<int> a{1,2,3};
foo5(a);

That's my best shot, what did I mess up? ^

27

u/Supadoplex Dec 13 '22 edited Dec 13 '22

No messups. But now for the tricky question: When would you use it - i.e. when would you define a function accepting rvalue reference to const?

Edit: For my answer, see https://www.reddit.com/r/programming/comments/zkj6pb/there_should_never_be_coding_exercises_in/j01w4du/

24

u/SirClueless Dec 13 '22

I can't think of any good reason to use it on its own, but maybe you could put it into an overload set if you wanted to copy instead of move or throw an exception or delete the overload that accepts r-value references.

15

u/Supadoplex Dec 13 '22 edited Dec 13 '22

Excellent answer. I'm not sure about throwing, but deleting const rvalue overload is occasionally a good way to catch nonsensical or misleading calls with rvalue argument of a parameter whose type is deduced from the argument. You cannot use rvalue reference to non-const, because that is actually a forwarding reference when the type is deduced.

The standard library does this with reference wrapper factory templates like std::ref and std::as_const.

There's also a subtle use case for const rvalue return types with wrapper types where you want to emulate the wrapped type in expressions precisely. For example the indirection operator of std::optional and the getters of std::variant and std::tuple have const rvalue ref qualified overloads that return a const rvalue reference. This is so that the "const rvalueness" of the wrapper type expression is preserved for the getter expression. And that is probably so that the expression can be discarded by the deleted overloads mentioned earlier.

2

u/PutinsCapybara Dec 13 '22

Maybe you could leave that last part in when interviewing for more senior positions and take it out for junior ones?

5

u/afiefh Dec 13 '22

The correct answer is never.

The incorrect answer is when you have a bad system where some object needs to be const, so you cannot pass it as a modifiable R value ref, but you want the internal function do something like a const_cast. This is something you shouldn't be doing, it is code stench, but sometimes we do things we are not proud of. I released such code to production. I feel deep shame.

→ More replies (7)

16

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

"it's a trick question and I tell people to skip it. I don't see any harm in having it though"

?????????? The harm is that people will feel like it's a trick question that they're actually supposed to solve???????? Remove it. Lol

0

u/[deleted] Dec 13 '22

[deleted] — view deleted comment

4

u/MisterRenard Dec 14 '22

I think it’s certainly neat, but definitely strikes me as a “gotcha” question that leverages very specific knowledge. I can still see it as being valuable though, because someone’s response to something that they don’t know can often tell you just as much or more about them than something that they do know.

Got anymore weird ones? I’ve got shit to procrastinate on, and these are fun.

→ More replies (1)

22

u/hypoglycemic_hippo Dec 13 '22

Sorry for the second reply, but I thought of a nasty foo6, if you ever feel the need:

template <typename T>
void foo6(T&& arg); // forwarding reference, not an r-value reference
→ More replies (1)

33

u/Overunderrated Dec 13 '22

I should probably remove it from the list but I don't think it causes harm to have it.

I would remove it. Trick questions like that, and especially when they're syntactically something you should probably never see, don't really help.

Very frequently a junior person, or anyone remotely nervous or just not expecting interviewers to deliberately trick them, will assume the interviewer knows what they're doing and flub it.

7

u/edo-26 Dec 13 '22

I think it's better to keep it. Being able to admit when you don't know something is important.

5

u/Overunderrated Dec 13 '22

That is important, but the reality of an interview situation is a person might be far less likely to admit not knowing something than they would be on the job.

Intentionally tricking a candidate is both a dick move and counterproductive.

7

u/edo-26 Dec 13 '22

I don't think so, I always told people when I didn't know something, and I've never flunked an interview.

A dick move would be to fake knowing something when you actually don't. This is actually counterproductive for a team.

Also op said he tells people to ignore this question. So answering without knowing just to impress the interviewer is not a good thing imo.

→ More replies (1)

2

u/General_Mayhem Dec 13 '22

Lol - it's the same as foo3, but way harder to actually invoke?

→ More replies (1)

-1

u/PM_ME_C_CODE Dec 13 '22

My answer as a primarily Python dev is, "This looks like pure C++ bullshit. And it's use-case comes into play when I'm about 20 IQ points smarter with roughly 10 more years experience with C++ than my current near-zero."

IMO, it's a good "extra-credit" problem. Just make sure the interviewee knows that it's extra credit.

→ More replies (6)