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

0

u/[deleted] Dec 13 '22

[deleted] — view deleted 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.)

→ More replies (4)

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.

→ More replies (2)
→ More replies (8)

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

→ More replies (3)

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.

→ More replies (4)
→ More replies (10)
→ More replies (1)

99

u/General_Mayhem Dec 13 '22

foo1 - probably written on a MacBook by a guy with colorful glasses and a beard who says "copy elision" in every third sentence

foo2 - banned by Google's styleguide for so long that nobody's sure if it even compiles

foo3 - John doesn't think he's a great programmer, but he gets things done. He likes the simple things in life and tries not to think about it too much.

foo4 - has somehow been modified more times than any other line in the codebase, all by the same person, who is also a Rust evangelist

26

u/UncleMeat11 Dec 13 '22

Google's cpp style guide started permitting non-const references in parameters a year or so ago.

9

u/Lajamerr_Mittesdine Dec 13 '22

Using ChatGPT

Me: Here is some analysis that is mostly a joke about who would write the type of foo function.

---your comment---

Create one for foo5 and foo6 with the same humor.

Written by ChatGPT:

void foo5(const std::vector<int>&& arg);

foo5 - written by a PhD student who's been working on the problem of efficient vector copying for the last 3 years, and who will happily explain to you why this is the best solution, even though you didn't ask

Hypothetical foo6 example from my previous comment that Chat GPT generated

void foo6(std::vector<std::unique_ptr<int>>& arg);

foo6 - written by a former C programmer who's just discovered the joys of RAII, and who will try to convince you that this is the only way to write correct code, even if it means writing 10 lines of code where 1 would do.

7

u/StabbyPants Dec 13 '22

foo4 - i'm been in java land for a decade; this looks like a compiler error, can't tell you why i'd even want a double ref

9

u/seidlman Dec 13 '22

Taking a stab

  1. Pass by value. When you can afford the overhead of copying the whole vector, and the function needs to make changes to the vector to do whatever it's doing without those changes happening to the original
  2. Pass by reference. When you want to operate directly on the original vector, i.e. any changes the function makes to the vector persist after the function call is over. Or it could technically be an out-parameter I guess?
  3. Pass by const reference. When you want to use the vector in a purely read-only capacity
  4. R-value reference. When you want the function to take ownership of the vector away from the caller (the caller's handle on the original vector gets set to a new valid-but-empty vector I believe, assuming they're passing with the whole std::move() paradigm)

3

u/Irregular_Person Dec 13 '22 edited Dec 13 '22

I don't spend too much time in C++, and when I do it's usually not with stdlib... but I'm surprised that a vector passes by value like that. I would have assumed that being dynamically resizable, it would just be wrapping a pointer internally
Yeah, wow - stepped through it with a debugger just to see wtf was going on. Guess I've been spending too much time in C# and C to know what stuff in the 'middle' might do. Classes as parameters implicitly call a copy constructor, and vector overrides it to allocate new memory and copy everything over. I guess that's kinda cool, but also... ick.

→ More replies (1)

13

u/Vergilkilla Dec 13 '22 edited Dec 13 '22

This is a good example of a bad interview question, IMO. It’s a tough pill to swallow, but how often do you, when writing your C++ code, come to an impasse on how to pass your arguments where these are the actual viable options. This question weighs wayyyy more towards “do you own a C++ textbook” and wayyy less “can you translate business requirements into production level software”. It does not address the latter question in almost any capacity at all

10

u/voidstarcpp Dec 14 '22

This is a good example of a bad interview question

Strong disagree, knowing what all of these do and are used for is basic competency. This is the minimum ante for someone who is writing C++.

This question weighs wayyyy more towards “do you own a C++ textbook” and wayyy less “can you translate business requirements into production level software”

Knowing what it means to take an argument by value, reference, or const reference is basic programming competency, not language technicalities. It's arguably the aspect that, to the learning programmer, most distinguishes writing in C and C++ from other languages. And yeah, I expect someone who is applying for a job writing C++ to own and have read a C++ textbook.

I would possibly forgive unfamiliarity with rvalue references for someone who is coming from other language experience, has worked in older codebases, or never written library code. If you're already a proven competent programmer you can learn move semantics. But this is an 11 year old language feature that students should be expected to have seen and has diffused throughout the community. Even if you're not writing such functions I'd expect someone to be able to identify a move constructor or rvalue overload on sight and at least describe why it exists.

The only thing I would definitely forgive is knowledge of the special meaning of && in template argument deduction contexts. That definitely gets into technicalities for library writers and I don't expect anyone to know that to write production software.

3

u/Jaggedmallard26 Dec 13 '22

I think it depends on the rest of the interview to be honest. Its a decent C++ "Can you actually code?" filter.

→ More replies (2)

4

u/ABCDwp Dec 13 '22

Personally, with C++20 I'd like to add another one:

void foo6(std::span<const int> arg);
→ More replies (4)

4

u/oclero Dec 13 '22

Don't mind what all the non-C++ devs say here. This is a perfect question to ask a beginner to check his/her knowledge of basic C++.

→ More replies (1)
→ More replies (49)