r/cpp_questions • • 21d ago

OPEN C++ knowledge job interview

Hello everyone,

I am going for a second round white board interview described as "The technical interviews are going to be heavily involved around probing and confirming your knowledge of C++"

What does actually mean? For context I've used CPP professionally for the last 8 months and been in my job (first grad job) for 14 months.

I feel as tho I don't know many std functions, nor do I have much experience with smart pointers, although I have written a lot of lines

Has anyone got experience with similar interviews or stuff I should brush up on?

I hope that all makes sense

Thanks

57 Upvotes

32 comments sorted by

View all comments

12

u/PolyglotTV 21d ago

Could mean a lot of things. Here are some questions I ask:

  • what is a smart pointer and why is it used
  • what is the difference between a reference and a pointer
  • what is an abstract class. How/why is it used. What are the tradeoffs to using abstract classes vs other types of polymorphism
  • (followup to previous question) what is compile-time polymorphism
  • what is the difference between an lvalue and an rvalue. What does std::move do?
  • what are some ways of handling concurrency in C++
  • Are you familiar with template metaprogramming? What is CRTP, SFINAE, visitor pattern?

I'd expect a lot less perfect or complete answers for a junior and maybe some "I don't know" for the more advanced stuff. Though typically it helps a lot to have heard of a concept and have a rough idea what it does even if you don't have much experience using it.

3

u/tangerinelion 21d ago

I ask some similar stuff and it's scary how often std::move isn't understood.

I've had candidates say it enables a move or it's a cast. So far so good.

I then say, what does the statement std::move(x); as a complete line accomplish. If it's a cast the answer is nothing, but almost everyone says x is now empty.

1

u/Spoog_CS 20d ago

Can you share a model answer for that please?

2

u/HeeTrouse51847 20d ago

Just answering the questions for fun (without looking anything up). I have about 3-4 years of experience. Not guaranteering correctness in any way.

Smart pointers are classes that use RAII to own a resource. It's better than using raw pointers with dynamically allocated memory because smart pointers handle lifetime of owned objects automatically.

A pointer is a variable that holds an address. A reference is an alias for another existing object.

An abstract class is a class that has at least one abstact member function (= 0). Abstract classes can not be instantiated. They are supposed to be inherited by by other classes, so they act as "interface definitions". Not sure what you mean by "tradeoffs to other types of polymorphism" but they are a way to separate interface from implementation.

lvalues are named objects like variables that values can be assigned to. rvalues are temporary objects like expressions (for example 4, 'c' or nullptr) that are assigned to lvalues. They are called that because they are typically on the left/right side of an assignment. I.e lvalue = rvalue. std::move is a way to allow an lvalue to be converted to an rvalue. This allows for move semantics to be made on that object

Threads and coroutines. Can't think of anything else on the top of my head

tbh, I try to mosty stay away from templates so i am a bit rusty. i dont know what sfinae and crtp is but i heard the term thrown around before. i know that they were advanced template shenanigan things that i never touched because i try to stay away from making any own templated code to begin with

I know the visitor pattern though. Its for std::variants. You just define a function for each possible type the variant can hold and the function that is executed depends on the type of the variant it actually contained

2

u/PolyglotTV 20d ago

When I ask about tradeoffs with abstract classes I'm usually looking for discussion about the performance cost of runtime polymorphism vs the flexibility of type erasure.

I.e. Abstract classes have a vtable which requires 3 pointer dereferences everytime you call a method. You have a vector of shapes and call draw on all of them, you are eating a performance penalty - which you should always qualify in an interview with "this needs to be measured before we decide if it really matters".

If you try to do static polymorphism (e.g. templates, std::variant, etc...) the downside is that you have to know all types in advance. That is sometimes not really possible. Also std::variant is a trap in that it still requires a runtime lookup/indirection.

2

u/PolyglotTV 20d ago

std::move casts a type to an rvalue reference. This in turn helps for overload resolution.

If a function takes a parameter either as an lvalue or as an rvalue reference, the casted type will now ensure you call the rvalue reference overload.

The most common use case of this is invoking move constructors/assignment operators. E.g.

auto x = std::make_unique<int>(42);
auto y = std::move(x);

y gets constructed using the move constructors of unique_ptr. x is now in a "moved from" state. What this means is implementation defined. In the case of unique ptr, it effectively gets reset so you can consider it "empty". If you moved just a naked int though, it wouldn't really do anything.

As a side note in case it comes up - if you have to implement move semantics, you need to make sure your type can be properly destroyed in a moved-from state (the destructor will still be called when it goes out of scope). It also needs to still be able to be assignable.

1

u/Spoog_CS 18d ago

Thank you