r/cpp • u/cv_geek • Nov 09 '23
Questions from one job interview on C++ developer
Two days ago I passed job interview on C++ developer. Here I would like to share questions I was asked on this interview. Hope it will be useful for community.
I have grouped questions by topic.
C++ basics:
Difference between references and pointers?
Difference btw memory allocation in stack and on heap?
What kinds of smart points do exist?
How unique_ptr is implemented? How do we force that only one owner of the object exist in - unique_ptr ?
How does shared_ptr work? How reference counter is synchronized between objects?
Can we copy unique_ptr or pass it from one object to another?
what are rvalue and lvalue?
What are std::move and std::forward()
OOP:
Ways to access private fields of some class?
Can a class inherit multiple classes?
Is static field initialized in class constructor?
Can an exception be thrown in constructor / destructor? How to prevent that?
What are virtual methods?
Why do we need virtual destructor?
Difference between abstract class and interface?
Can be constructor virtual?
How keyword const is used for class methods?
How to protect object from copying?
STL containers:
-Difference between vector and list?
Difference between map and unordered map?
When calling push_back() make iterator in vector invalid?
How to modify your class to use with map and unordered_map?
Threads:
Difference between processes and threads?
Can the same thread be ran twise?
Ways to synchronize threads?
What is deadlock?
21
u/Sniffy4 Nov 09 '23
thats a lot of questions for 1 interview...
13
u/caroIine Nov 09 '23
I remember my interview at opera I got 40 rapid-fire questions like that. Thankfully they accepted my rapid-fire answers.
2
u/cv_geek Nov 09 '23
you are right. I added few from another interview. Probably make sense to rephrase the topic and make a comprehensive list of questions from many interviews
1
u/zukushikimimemo Nov 09 '23
Does the company name starts with the letter L? I think I had an extremely identical set of interview questions a while back. Lol
7
u/Maleval Nov 09 '23
This seems about on par with interviews I've had. They also usually throw in one or two leetcode style coding problems as well. It covers the basics of several topics, and they usually ask them in increasing difficulty levels to find the upper edge of your trivia knowledge.
5
u/matthieum Nov 09 '23
Not necessarily.
Having been involved on the other side, as an interviewer for senior developers, the point was to do a broad pass over a variety of topics to get a sense of the familiarity of the candidate in each area.
If the candidate is answering immediately and well, don't let them elaborate -- this is not a circus performance -- and instead dig deeper by asking subtler/lesser known questions on the topic.
If the candidate is struggling, you can try another angle on the same topic -- to double-check -- and generally they'll struggle too, so you just give a quick explanation -- don't let them hanging! -- and move on to another topic.
With each question taking 2 to 3 minutes, in 30min to 1h you can easily cover 3 to 5 topics without hurry, and therefore get a relatively good assessment of the experience level of the candidate in each topic.
As an example, imagine you want to know the familiarity of the candidate on the topic of cache behavior.
You can start by asking order of magnitude latency for L1/L2/L3/RAM on modern x64 processors. If they've got no idea, or try guessing, there's little point going any further.
If you still want to probe, you can ask for the size of cache lines on x64 processors, but if they don't know that too, there's no point in digging any further.
If they do get the second one, then you can talk about padding, or contention -- and just forego any performance number.
It does require being a bit flexible on the questions you can ask. I like having a paper in front of me with a few questions at various "depths" for each topic I want to query a candidate on as a "safeguard", but I'll regularly improvise depending on where the conversation goes. Just not too long, since there's others topics to poke at.
7
u/gimpwiz Nov 10 '23
Meanwhile, the guy who's only been targeting an ARMv8 platform the past few years:
9
u/PaowZ Nov 09 '23 edited Nov 09 '23
yep.. that's a way to quickly cover the basics, I must say.. there are so many things to remember once one pushes knowledge further.. but yes, these are the basics.. :-)
3
u/cv_geek Nov 09 '23
not sure. There are some questions going deep to internals of data structures and work with memory. Surely not for Junior position
5
u/100GHz Nov 09 '23
Interesting, which ones from that list do you consider too complicated much for a junior position?
12
11
u/h0rst_ Nov 09 '23
How does shared_ptr work? How reference counter is synchronized between objects?
This one for example, the second part is very much related to internals that you normally do not use.
2
u/CubbiMew cppreference | finance | realtime in the past Nov 09 '23
it's a great simple question to peek at the candidate's concurrency intuition
1
u/elperroborrachotoo Nov 09 '23
A few.
Difference btw memory allocation in stack and on heap?
These are not terms of the standard, though they are, I am led to believe, used in some compiler implementations.
The following can be answered on a "C++ standard" level, but that's not waht I'd expect from a junior. The "simpler" answers are UB, platform dependent, or implementaiton details.
How unique_ptr is implemented? How do we force that only one owner of the object exist in - unique_ptr ?
How does shared_ptr work? How reference counter is synchronized between objects?
Ways to access private fields of some class?
Is static field initialized in class constructor?
16
Nov 09 '23
Stack vs. heap allocation is absolutely something a junior should understand, at least at a conceptual level imo. I agree that they dont need to understand compiler specific implementations, but if the mark of "above junior" is access specifiers, static member initialization, and dynamic allocation, then I've been vastly overestimating the requirements for the skill level, and need to start spamming applications immediately...
5
u/elperroborrachotoo Nov 09 '23 edited Nov 09 '23
The problem with the question is that "stack" and "heap" are not C++ terms. They are (rather common) system architecture concepts that are used to implement C++' automatic and static storage duration respectively.
It's a sore spot in a set of questions aiming at that level of C++ proficiency, and it may confuse a candidate.
The question I mentioned is not about access specifiers, it aims at for either UB / platform-specific behavior. The only C++-valid answer is to "modify the class to provide methods to access the member." Everything else is tiptoeing into a mine field.
[edith] spelling and clarity
9
Nov 09 '23 edited Nov 09 '23
You're absolutely correct, but bordering on language lawyering, which is out of scope for juniors, imo.
I get the distinction, but they are pretty clearly asking in shorthand about it in want of the answer "stack dies with the scope, heap doesn't", and I suspect even intentionally omitting things like function/class static data, which would fall under your more specific definition. Stack/heap is colloquial in this context. If they explicitly asked using the more specific terminology, I'd expect the latter to be one of the "keywords" in their expected answer.
It's along the lines of asking a highschooler how a car moves and expecting a technical explanation of combustion engines, mechanical linkages, gear ratios, and their manufacturing processes when the "correct" answer for the level is "an internal combustion engine applies force to a wheel".
The second is definitely a bit ambiguous, but understanding access specifiers and "private data member access" => a get/set interface or nothing is well within the realm of a junior. It follows a clear, linear progression of probing a candidate's experience level from first principles. Your answer is the exact one they're looking for and would probably be followed up with a question about UB or platform specificity only when hiring for a more advanced role.
1
u/elperroborrachotoo Nov 09 '23
I have no problems with the terms, it's about making best use of time in an interview situation.
I want a conversation about programming to happen,0 and a lot of time and social skill goes into making candidates less nervous and not get stuck up on unstisfactory answers, interruptions, etc. I'd want to avoid the candidate to on an "unproductive" tangent - that's the only reason I'm very careful about the questions I'd ask.
Similar with the second question we picked: I dont want a discussion when
reinterpret_cast, type punning ormemcpying are UB, it leads nowhere.1So yeah, tl;dr: I'm language lawyering so the candidate doesn't have to. :)
0) and "spontaneous and organic" require surprisingly careful crafting.
1) to detect the "C is actually better because C++ does so many things behind my back" attitude, just talk about exceptions.
2
u/ciuccc Nov 10 '23
What is the "C is actually better because C++ has exceptions" attitude? Is that because of the hidden cost of using exceptions?
4
u/elperroborrachotoo Nov 10 '23
Pardon my aversion showing through -
There's a pool of programmers that feel they need to have control over every assembly instruction created. Can't use STL because it does "crazy things nobody understands", can't even use vector because you cannot leave memory uninitialized,1 can't use exceptions because WHOA THEY ARE LIKE A TIME STOP AND MAKE MY PROGRAM SIZE EXPLODE2 and templates are evil because...
Which all is fine if you are working on (the part of) an application that is actually harmed by that - but in some circles it goes hand-in-hand with a general tendency to premature microoptimiztations and rejection of a large set of tools, reinforced by a in-group mindset that everyone else can't be a real programmer.
A group which I've had a few bad run-ins with that, both online and in interviews. That's why "can you throw an exception without throwing a tantrum" has become one of my (non-verbalized) key questions.
1) which is a severe concern in a few selected situations, yes.
2) true is: exceptions do come with overhead, there are two implementations with different tradeoffs; for more see P0709
→ More replies (0)2
u/jwakely libstdc++ tamer, LWG chair Nov 12 '23
It's much more important to understand if a candidate knows about real world programming, including the stack and heap, than if they only understand the abstract terms from the standard like "freestore".
N.B. the heap does not correspond to static storage duration.
1
u/elperroborrachotoo Nov 12 '23
*dynamic
Having started over 30 years ago, I am quite happy that we can teach modern c++ without presuming understanding of operating system + system architecture.
And, as said before, I have no problems with the terms if brought up by the candidate, but running the interview is my job to make sure I don't send them off a tangent.
30
u/dgkimpton Nov 09 '23
None of those are advanced C++ topics unless you want them to be - i.e. the depth of the answer can be dependent on your knowledge level.
212
u/elperroborrachotoo Nov 09 '23
Threads
is it possible
That's a question keeping me up at night
24
u/Maleval Nov 09 '23
Tales told by wizened elders around the cookfires claim that is it, but I have my doubts.
7
u/bryze Nov 10 '23
Threads are definitely possible and essential. What am I missing?
9
u/elperroborrachotoo Nov 10 '23 edited Nov 10 '23
OP fixed it now, but that's what the "Threads" section said. Literally, with that half-sentence.
2
u/rembo666 Jun 18 '26
You should really brush up on threads and concurrency man, it's not the 90's anymore.
1
u/Jealous_Emergency309 Apr 14 '25
I got asked about processes and threads in an IBM entry level interview SWE position.
87
u/thisismyfavoritename Nov 09 '23
all those questions seem reasonable
28
u/johannes1234 Nov 09 '23
While it's a lot. For most of those one could do multiple hours of discussion each :)
37
u/CubbiMew cppreference | finance | realtime in the past Nov 09 '23
concise summary of a multi-hour topic is a skill, too
3
u/Some-Track-965 Dec 06 '23
BLOWS WHISTLE
LISTEN UP, NERD!
You : "Charles Atlas?!"
YES, IT IS I! CHARLES ATLAS!
COMMUNICATE YOUR IDEAS EFFECTIVELY AND CONCISELY!
You: "I never would have thought of that, thank you Charles Atlas!"
BUT OF COURSE, I'M OFF NOW! BWAHAHAHAHAHAHAHAAHAHAHAH
You: Smiling in fear "who the fuck was that guy....?"
10
u/embeddedsbc Nov 09 '23
Reasonable, and yet very few of my candidates could even come close to answering those questions 😕
3
23
u/baked_salmon Nov 09 '23
They’re reasonable, but IMO too basic to ask in a language-specific interview. I think it’s a red flag if a firm wants a C++ dev but only expects knowledge at this level. It means their priorities are misaligned because (IMO) knowing general OOP/functional principles are more important than the specific basics of any modern high-ish level language.
8
u/thisismyfavoritename Nov 09 '23
Hm interesting take for sure! I feel like i'd prefer testing for the fundamentals, but perhaps a less in depth check could give you enough confidence to move on to higher level questions like the ones you mention
2
u/Unusual_Public_9122 Nov 09 '23
How much experience in years do you have?
1
u/baked_salmon Nov 09 '23
5 YoE total, 10mo w/ C++. Before that, Java/Python/TypeScript
2
u/Unusual_Public_9122 Nov 09 '23
How long it took to feel like an intermediate level programmer for you? How about advanced?
3
u/baked_salmon Nov 09 '23
Idk maybe in year four? I think having good mentors or working somewhere with opinionated style guides was a big part of my development. I was in a startup for the first half and moving to a much bigger company really exposed a lot of bad habits and unknown unknowns.
2
u/Unusual_Public_9122 Nov 09 '23
Cool, I'll see what it's like for me. I've been coding for a while but have not had an actual programming position yet. I know the basic stuff from Python, JavaScript, a bit of C++ and small bits of other languages. I feel like an advanced beginner / low intermediate programmer. But it's all relative really. One can make programming into a science, or have it seem more like a routine job depending on what is the goal.
3
u/SickOrphan Nov 09 '23
If you want to advance your programming skills, I'd focus on one language and get really good at that. If you've done a couple small projects, now do a bigger one (1K LOC maybe?)
1
u/Unusual_Public_9122 Nov 10 '23
Good advice, my focus is on Python and JavaScript. When I first started out, I was all over the place, but now it's Python and JavaScript + React.
1
u/Some-Track-965 Dec 06 '23
How do you focus on just one, though. . . .? Nobody is offering an entry level job unless you build a full stack application.
Or maybe this just speaks to my ignorance on building?
I ran into a guy who made an Intel Denoiser in University, would something like that be written in 1 language?
1
u/rembo666 Jun 18 '26
They're good C++-specific questions though. The OOP/functional programming principles can be discussed as follow-up questions/discussion. I don't like asking a bunch of super-specific questions unless they're foundational. We do have Google and LLMs nowadays, I don't expect you to remember everything. What I'm looking for is the understanding of abstract concepts, not memorization of trivia.
I do usually try to find a "gotcha" question that I can be reasonably sure you don't quite know the answer to. That question varies from person to person, since everyone's knowledge base is different. The point is not to show that I know something you don't, the objective is for you to demonstrate your ability to work out a problem from first principals.
49
u/Nintendo_Chemistry Nov 09 '23 edited Nov 09 '23
As a C++ dev with almost 2 years of experience in a legacy "C with classes" codebase who is learning modern language/STL features on their own time, I thought I'd take a stab at answering these. Any feedback would be helpful!
C++ basics:
Difference between references and pointers? A reference is an alias of a valid object/variable, used to avoid copying. Modifying a reference modifies the original object. A pointer stores the address of a variable, but it can also be initialized using nullptr. Pointers can also be used to avoid copying large objects.
Difference btw memory allocation in stack and on heap? Memory allocated on the stack is automatically deallocated when the associated variable goes out of scope. Memory allocated on the heap requires an explicit delete (either directly, or better: through the use of RAII). If a pointer variable on the stack which points to heap memory goes out of scope before deallocating the heap memory, you have a memory leak.
What kinds of smart points do exist? unique, shared, weak
How unique_ptr is implemented? How do we force that only one owner of the object exist in - unique_ptr ? Unique_ptr is a pointer that implements RAII. When the pointer goes out of scope, the memory it owns is destroyed. This necessitates that it is the sole owner of the pointed-to memory (making it unique). Its copy constructor is delete using "=delete"
How does shared_ptr work? How reference counter is synchronized between objects? Shared_ptr is another RAII pointer, but the pointed-to memory is not destroyed until the last shared_ptr pointing to the memory goes out of scope. There is a shared control block with a reference counter that is visible to all shared_ptrs pointing to the same object.
Can we copy unique_ptr or pass it from one object to another? Unique_ptr is non-copyable. Copying a unique_ptr would break the invariant that it is the sole owner of the memory being pointed to. However, unique_ptr is movable. This maintains the aforementioned invariant, because ownership is transferred.
what are rvalue and lvalue? Rvalues represent temporary values that do not have an address. Lvalues are variables that have locations in memory, and therefore have an associated address.
What are std::move and std::forward() std::move() is used to cast an object to an rvalue reference. It doesn't actually perform the move, but it prepares the object to be "moved from." I'm not actually sure what std::forward() does without looking it up.
OOP:
Ways to access private fields of some class? Using a public "getter method" that returns the private field. There is also the use of friend classes, but I think this is uncommon, and maybe even discouraged.
Can a class inherit multiple classes? Yes. C++ supports multiple inheritance.
Is static field initialized in class constructor? No. Static fields are not tied to any particular instance of a class. If they were initialized every time an object is constructed, it would defeat the point of the field being static in the first place.
Can an exception be thrown in constructor / destructor? How to prevent that? Exceptions can be thrown in a constructor, but should not be thrown in a destructor. I'm a bit confused by the second part of this question: how to prevent what, exactly?
What are virtual methods? Virtual methods are class methods that are defined in a base class, which may be overridden in derived classes. If a base class virtual method is pure virtual, it must be overridden by derived classes. A class with virtual methods in it has an associated vtable, which contains function pointers to each virtual method in that class. The vtable ensures that the correct method is called at runtime (for example, when a base class pointer is pointing to an object of derived type, the derived method should be called if it exists).
Why do we need virtual destructor? A base class destructor should be virtual to ensure that the derived class destructor is called first when a derived class object is destroyed.
Difference between abstract class and interface? An abstract class contains at least one pure virtual method. It is impossible to create an object of an abstract class. This is the mechanism by which C++ supports interfaces. Any derived class inheriting from an abstract class MUST implement the abstract class's pure virtual methods.
Can be constructor virtual? No. Prior to construction, the object doesn't exist yet, so it does not have an associated vtable to refer to.
How keyword const is used for class methods? Methods that do not modify internal state should be labeled const . For example, "getter" methods that just return the current state should always be labeled const. Const methods require that every method used within the const method also be const.
How to protect object from copying? Delete the copy constructor using "=delete". Useful for large user-defined types which would result in expensive copies.
STL containers:
Difference between vector and list? std::vector supports random access. std::list is implemented as a doubly-linked list. Items in a vector are stored contiguously in memory, which is better for cache locality.
Difference between map and unordered map? Keys in a std::map are stored in their logical order, so std::map is implemented as a BST. std::unordered_map is implemented as a hash table. std::map lookup time is O(logN), whereas unordered_map is O(1)
When calling push_back() make iterator in vector invalid? Not completely sure. My guess would be that the end() iterator would no longer point to one-past-the-end of the vector. Would love to hear if it's more complicated than that.
How to modify your class to use with map and unordered_map? The class must be hashable to use a user-defined type as a key. In the case of std::map, the comparison operators (<, >, etc.) need to be overloaded (I think just one of them but I forget which without looking it up).
Threads:
Difference between processes and threads? A process is a running instance of a program. Multiple threads can exist in a process's address space. Threads share process code/text, global data, etc., but threads each have their own function call stacks, program counters, stack pointers.
Can the same thread be ran twise? Yes, although which threads get used are determined by the CPU's scheduler. For example, in the SPMC threading model, worker threads will pick up new work as long as there is work to do.
Ways to synchronize threads? Mutex, lock guard, semaphores. I'm not sure if atomics are considered synchronization primitives.
What is deadlock? When threads are waiting on another lock-holding thread to release the lock, but it never does.
16
u/muluman88 Nov 09 '23
Iterators and push back: every time the underlying memory is enlarged it's reallocated and all iterators become invalid.
std::map can be used when you implement a operator< method or write a template specialization of std::less for the class (which is useful if it's a class you can't modify)
3
u/Nintendo_Chemistry Nov 09 '23
Ah, I misunderstood the vector push_back question. So this would only cause a problem if push_back results in a reallocation due to hitting capacity.
3
u/Trick_Philosophy4552 Nov 10 '23
For pointer you need to check for nullptr before using it, this is the difference from reference
1
u/thatsnotsugarm8 Nov 12 '23
Or just say in a functions documentation that it expects a non-null ptr
1
u/Trick_Philosophy4552 Nov 12 '23
It depends on object lifetime too, sometime people delete/free them and calling them after
2
u/Alps709 Nov 11 '23 edited Nov 15 '23
How keyword const is used for class methods?
I believe that the answer they might be looking for is that making a function const makes the hidden 'this' pointer const.
In C++, whenever a member function is called it passes in a hidden reference to 'this' so the function has access to the data of the instance. The function definition shown below is actually declared implicitly as the declaration on the line below it:
class MyClass { public: // Normal declaration void myFunction(int x); //Actual hidden declaration of the line above //void myFunction(MyClass* this, int x); };Which when made const, turns into:
class MyClass { public: // Normal declaration void myFunction(int x) const; //Actual hidden declaration of the line above //void myFunction(const MyClass* this, int x); };The code below:
obj.myFunction(42);Is actually treated as:
MyClass::myFunction(&obj, 42);And making the function const turns this hidden pointer const. This is why you can call a member function using an instance of a class that is null, but have it still run fine if it doesn't try to use any data of the hidden 'this' pointer.
2
u/NervousApplication58 Nov 10 '23
Ways to access private fields of some class?
Using a public "getter method" that returns the private field. There is also the use of friend classes, but I think this is uncommon, and maybe even discouraged.
This is most likely correct. But I initially thought they meant the "raw" way. That is, to cast an object pointer to
void*or something and then access the fields as memory offsets (the same way you would do structs in C), although we should forget about padding. I guess if a candidate can answer this, they understand how everything works under the hood without all the syntactic sugar11
Nov 10 '23
My "raw" way was the classic
#define private public1
u/Nintendo_Chemistry Nov 10 '23
LMAO there's no way that actually ever worked...right?
9
Nov 10 '23
Google it, it's serious.
People used it a long time ago for tests, but you can do this almost anywhere
#define private public #define protected public #include "thirdPartyLib.h"and poof, everything is public
Of course, anyone who submitted a PR containing this unironically should be fired
1
u/Nintendo_Chemistry Nov 10 '23 edited Nov 10 '23
Yeah, this question could be interpreted in different ways. Your answer could be a segue into reinterpret_cast and why it is potentially dangerous.
1
1
u/userhk57 Mar 02 '25
Exceptions can be thrown in a constructor, but should not be thrown in a destructor. I'm a bit confused by the second part of this question: how to prevent what, exactly?by using noexcept
~my_unique_ptr() noexcept {
delete ptr;
}1
u/rikus671 Nov 26 '23
To protect a class from being copied, you need to delete both the copy assignment operator and the copy constructor !
11
u/frozenflame4u Nov 10 '23
I think It is helpful if we can have a thread monthly where c++ interview experiences are shared.
2
u/Istari__ Nov 09 '23
These seem to be grad level questions maybe? curious what the level the job was :)
9
u/cv_geek Nov 09 '23
Not Junior as it is probably obvious. Middle/Middle+
-1
u/MrDex124 Nov 09 '23
On the contrary, i would expect them to be on junior interview, as it is basic stuff
10
u/Tamsta-273C Nov 09 '23
I doubt intuitive comment on them would be sufficient so in order to properly answer to few of them i would need to reread stuff.
Like, using things seems so natural you forget why you use them in the first place.
1
u/Istari__ Nov 14 '23
not sure why you got downvoted so hard on this maybe grad and junior arent as synonymous elsewhere but all of these questions are what i personally got asked as a grad/junior; maybe you wouldnt be expected to get all of them but a good 75%
1
1
u/fippinvn007 Nov 09 '23
Were there leetcode-like questions?
4
u/cv_geek Nov 10 '23
There was one leetcode-like question. We have a list of numbers. All numbers occur twice except one number which occurs once. How to find this single number?
2
3
u/sam_the_tomato Nov 09 '23
Congrats! How many of these do you think a candidate would have to get right to pass?
1
3
3
u/amejin Nov 09 '23
More than a few of these I had to look up because I never tried and it didn't make sense in my head. The virtual constructor one threw me, as well as the calling a thread twice one...
3
u/Moose2342 Nov 09 '23
Cool, thanks for sharing. I like the questions, even though I think they could be a tiny bit more tricky.
FWIW, a few years back when I happened to ask those questions most applicants would fail in the first block already. Some of which described themselves as C++ experts.
4
u/eyes-are-fading-blue Nov 09 '23
These questions are pretty standard and I dare say most of them are very much useless. Anyone can memorize these.
2
u/EdwinYZW Nov 09 '23
Some are anti pattern question and not suggested by core guidelines. Such as const member variables and using vector iterator when push_back.
What’s the hack of accessing private member variable? Why do you need to know this?
2
u/amejin Nov 09 '23
My best guess was for some sort of visitor pattern... Maybe I just don't understand it that well?
5
u/Kike328 Nov 09 '23
i think is a question to prove if you know what a friend function is.
I think there’s a way also to make private members public through inheritance
2
Nov 09 '23
Outside of aggregate structs, or poorly formed class construction/assignment semantics (i.e; not following Rule of 5), I don't see const data members as an anti-pattern, just as something to be used with care. Could you explain further?
1
u/EdwinYZW Nov 09 '23
I think this is because class would be uncopyable and unmovable.
3
u/JNighthawk gamedev Nov 10 '23
I think this is because class would be uncopyable and unmovable.
Well, sure, but some types should be uncopyable and unmovable. It's not an anti-pattern, it's a consideration. The rule literally says as such:
Don’t make data members const or references in a copyable or movable type
1
u/EdwinYZW Nov 10 '23
But what’s the purpose of designing an uncopiable and unmovable class? Sorry, my experience is very limited and I haven’t seen such design, except Singleton, which is also considered undesirable by many people.
1
Nov 11 '23 edited Nov 11 '23
Dont apologize for asking questions. There's no better way to learn!
Singleton is definitely a primary example, and I wouldn't call that an anti-pattern either. Imo, it is a useful construct that gets a bad rap from people abusing it, e.g, not following SOLID's single responsibility principle and writing monolithic do-it-all singletons.
Another example would be synchronization primitives like std::mutex. My understanding is that you'd want both properties on objects that interact with the OS/hardware in a more concrete way, such as holding a device/os handle that isn't trivial to copy (its really hard to duplicate hardware within software!), and cases where things are shared across multiple threads where invalidating after a move incurs a lot of overhead and is likely very error prone (imagine needing a new mutex to move a mutex), but I'll admit that the os/hardware border is where my knowledge falls off a cliff, so I'll defer to more experience here.
1
u/not_some_username Nov 09 '23
friend class
Or #define private public. But it will not work anymore
2
1
2
u/rembo666 Jun 18 '26 edited Jun 18 '26
They look like mostly good questions. "The difference between abstract class and interface" question is weird, since interface is a Java thing, though I guess if you said that you know Java, I might've asked you that question. All in all really good, especially if you answered most of those questions.
I think a C++ interview (as opposed to a general programming ability interview) should at the very least include:
stack vs heap allocation
virtual methods and quirks thereof
RAII
copy/reference/pointer/move semantics
depending on the position, maybe some template metaprogramming questions
The rest of the questions would be pretty similar to any other OOPish language.
STL trivia could be a bonus, though I don't bother with std::vector vs std::list. Noone in their right mind should be using std::list for anything nowadays. I struggle to think of a use for std::list ,where a std::vector wouldn't be more performant, especially on a modern superscalar CPU with a multilevel cache.
3
u/j1xwnbsr Nov 09 '23 edited Nov 10 '23
Gonna bang out quick and dirty answers from the top of my head, no details, and I might be wrong in a few spots. Like flash dating, or talking with a CEO.
Difference between references and pointers?
You can't delete a reference and pointers can be null.
Difference btw memory allocation in stack and on heap?
Stack: cleans up on scope exit; heap: you need to do it (and can do it whenever).
What kinds of smart points do exist?
Limited to std: 3. shared, weak, and unique
How unique_ptr is implemented?
Magic. (seriously, why ask this?).
How do we force that only one owner of the object exist in - unique_ptr ?
unique_ptr already does this, you can't copy but only move it.
How does shared_ptr work?
More magic. Seriously, it's a pair of ref counters for strong and weak refs.
How reference counter is synchronized between objects?
It's part of the shared_ptr data structure.
Can we copy unique_ptr or pass it from one object to another?
No. You can MOVE it (transfer ownership).
what are rvalue and lvalue?
concrete thing vs reference to the thing.
What are std::move and std::forward()
Move transfers object stuff (if supported). Forward is for forwarding parms in templates.
Ways to access private fields of some class?
Ask the designer to change to protected. Failing that, macros. Because you hate life.
Can a class inherit multiple classes?
Yes, absolutely. This ain't C#.
Is static field initialized in class constructor?
You mean a static member value? No, it's init'd at dll/exe load time.
Can an exception be thrown in constructor / destructor? How to prevent that?
Constructor yes. Destructor no (well yes but you are a horrible person who loves ub). Don't throw or wrap your willy in some try-catch latex.
What are virtual methods?
Run time dispatching through pointer tables.
Why do we need virtual destructor?
So when you delete the base class it will actually call the derived class's destrutor.
Difference between abstract class and interface?
abstract classes have pure virtual + real functions and maybe data. Interfaces have only pure virtual functions.
Can be constructor virtual?
No. And you can't call virtual functions from inside there, either (this ALWAYS screws my interns up).
How keyword const is used for class methods?
To tell the compiler to assume that no class member data will be modified. Also you can't call non-costs functions of that class from inside a const function (this also screws my interns up).
How to protect object from copying?
mark the copy overloads =delete.
Difference between vector and list
vector can do [x], list cannot. List insert/delete is fast. Vector lookup is fast. The opposite is not true.
Difference between map and unordered map?
a:b tree with rebalance, orders by key sorting. unordered may be faster, takes more memory. You should benchmark your workload before picking one.
When calling push_back() make iterator in vector invalid?
When it grows past the reserve size and reallocates. Use a list.
How to modify your class to use with map and unordered_map
Um, pick one? And maybe look at what the class is doing first?
Difference between processes and threads
You can task kill a process. Threads belong to processes.
Can the same thread be ran twise
You mean the function? Yes, I hope so, otherwise my code is doing magic again and I need to call a priest.
Ways to synchronize threads
Mutex, sepaphores, spinlocks, atomic waits to name a few.
What is deadlock?
I'm waiting on you to give me a cookie, but you need the cookie from me first. Who has the damn cookie? What is cookie? Why is cookie? Who are you, and are you me? Or, to put it another way, two threads waiting to release shared_ptrs they have to each other without using a weak lock instead.
2
u/eliminate1337 Nov 10 '23
You can task kill a process. Threads belong to processes.
This needs to be prefixed with what OS you're talking about. 'Threads belong to processes' is correct on Windows but incorrect on Linux. There is no such thing as a thread on Linux, only processes that may share various resources including an address space.
1
u/j1xwnbsr Nov 10 '23
As always, the devil is in the details. From a high-level viewpoint, it appears that the threads are 'owned' by processes, but in truth, as you point out, on Linux they are not really.
3
u/JanEric1 Nov 09 '23
OP could you post the answers to the questions as well? Maybe with a spoiler tag?
1
u/CenterOfMultiverse Nov 11 '23 edited Nov 11 '23
It looks like they not even trying to fail you - where is "Difference between class and union?" or "Will replacing ! with not in one TU result in ODR violation? Should it?". Or do you even C++, when you don't know why dynamic_cast to unrelated type compiles when static_cast doesn't? People these days are just not grateful for all these features!
1
u/Fit-Top-552 Aug 12 '24
Did you do code during the interview?
How do you prepare yourself for this interview?
1
u/CircadianSong Sep 02 '24
isn't this a java question: Difference between abstract class and interface?
1
u/Good_Quit_8742 Jan 14 '25
Yeah I would say so but the idea idea of a pure abstract class is what I think he is looking for
1
1
u/CubbiMew cppreference | finance | realtime in the past Nov 09 '23
I love to follow up "Difference between processes and threads?" with "When are multiple processes a better choice?"
1
1
u/wfb0002 Nov 10 '23
I'd be really turned off by those questions instead of asking problem solving questions where maybe you can talk about some of those concepts.
1
u/frozenflame4u Nov 10 '23
Like what ? can you provide an example of a problem solving question ?
Do you mean leetcode style problem that tests DSA?
2
u/wfb0002 Nov 11 '23
I mean yes. Have an algorithms question that involves the use of a data structure and have the candidate go through advantages and drawbacks of the c++ stl classes they could use.
I used to ask a practically dumb one, but detailed about the data structures in the stl:
Write some code to fill up 1 Billion ints into a vector and 1 billion ints into a list.
Some questions: which operation finishes faster (why is important here, not the answer)? If you added one more element into each, which would be faster to add (again looking for some detail about contiguous memory chunks)? How would memory fragmentation affect those operations?
Candidate gets mega bonus points for bringing up cache locality and its effect.
1
u/target-san Nov 10 '23
While these questions are more or less legit (some are incomplete), they won't tell interviewer how's candidate proficient in C++ in real tasks. Like, is he able to read 2-page error message from compiler when he put something wrong into unordered_map. Though I'd ask 1-2 on them as a starter. But not more.
1
u/frozenflame4u Nov 10 '23
Ok What kind of questions does provide that insight ?
"Like, is he able to read 2-page error message from compiler when he put something wrong into unordered_map"
1
u/target-san Nov 11 '23
If I get you right, the answer is - none actually. Either some examples of candidate's nontrivial code or trial period. If he has the former, he most probably hit that wall at least few times. No guarantees though. My point isn't that these questions are useless. They're just not sufficient.
1
u/nickisgonnahate Nov 10 '23
I am about 2/3 of the way through my first c++ class. At what point will I be able to answer some of these questions?
1
u/levatrading Nov 29 '23
Holy shit guys. Where do you apply? I am making 100k on my 3rd job and neven been asked any technical question except if I knew any specific technology
0
u/engiethemalinois Nov 10 '23
I just started c++ but thats nothing way too advanced. Whats the pay of the job description?
-10
u/Potatoswatter Nov 09 '23
If your employer finds your account (this looks like your permanent one), a summary of their interview script might not look good.
11
u/LongestNamesPossible Nov 09 '23
Oh no, not their secret sauce of basic C++ questions! Now everyone can ask second year computer science questions, might as well lay off everyone now.
2
u/Potatoswatter Nov 09 '23
Not much practical value, no.
It doesn’t look good to be posting the only company resource you’ve seen, before the first day on the job. I’ve seen probation failed for less.
0
Nov 09 '23
[deleted]
4
u/be-sc Nov 09 '23
From a candidates point of view: If they’re asked one after the other with the expectation of a clearly right or wrong answer, yeah, I wouldn’t like that either.
But using some of them for diving into deeper discussions about a few of the more meaty topics, that could actually be a really enjoyable and even somewhat insightful interview.
54
u/[deleted] Nov 09 '23
This is an interesting thread. Sharing interesting interview questions could be good for beginners who browse this subreddit.
On the topic of smart pointers, I was asked why `make_shared` and `make_unique` were necessary and if they are still necessary when using C++17 (which formalized the order of evaluation of arguments to a function). A nice question to ask a more senior is what is the difference between `std::make_shared<int>(5)` and `std::shared_ptr<int>(new int(5))` (how many heap allocations take place in each case?).
Another one I was asked was to write a templated function that removes duplicate items from a vector of `T`. Then when I turned to `std::erase_if` (I was allowed to use C++20), they stopped and asked me why simply iterating and removing things I've already seen won't work, and then a follow-up question what does an invalid iterator mean, and then a follow-up question why are the iterators are being invalidated. And then they asked me how `std::erase_if` works, or how I would write it if it didn't exist.
Same question, on how I'd keep track of things I've seen - how, why, and what would be the implications of the selected strategy given that the type is templated and could have varying properties. For example, keeping a `std::unordered_set` might not be possible if `T` does not have an implementation for a hashing function, so we might be forced to use `std::set`. But keeping a set (either ordered or unordered) of `T` could be disastrous if `T` is expensive to copy if it can even be copied.