Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If you could ask a C++ programmer one question to measure their C++ skills, what would it be?

The question I think is best is: Can you call "delete this;" inside a member function? (I put this as a link so you can think it through first, then go to The Best C++ Interview Question – Ever! to see the correct answer.)

I don't ask this because I expect most people to know the answer. If they did it would not be that useful a question. I ask to see if they can work their way to the correct answer and how they do so.

share

locked by maple_shaft Jun 13 at 2:12

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as not constructive by ChrisF Dec 15 '11 at 22:45

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

comments disabled on deleted / locked posts

17 Answers

up vote 8 down vote accepted

I would ask them for the reason why virtual member function can not be used with template in C++. Of course, there is only few people who can actually answer the question immediately. But if an interviewee properly understands the core mechanics of C++(how template works in C++, how virtual function is implemented, how compiler and linker generate an executable from source code, etc.), you may get an answer with appropriate step-by-step leading questions.

Added : Template virtual member function in this answer means template function that is virtual member function of some class - not a regular member function in template class. Sorry for confusing :(

share
3  
@summerlight: I do mix virtual member functions and templates and nothing bad happened so far... So I am afraid I didn't understood what you were getting at. Care to enlighten me :) ? –  Matthieu M. Dec 14 '10 at 19:28
2  
This answer is almost but not quite correct. You can't have template virtual member functions, but you can have virtual member functions of a template class. –  catphive Dec 14 '10 at 23:29
1  
@summerlight: ah got it! Amusing question yes, often pops up ;) –  Matthieu M. Dec 15 '10 at 12:59
2  
@nikie: That's a virtual member function in template class - not a virtual template member function like template<typename T> virtual void doSomethingWithGenericTypeT(T arg); –  summerlight Feb 23 '11 at 5:57
2  
@summerlight: You should have included that example in your post. I had no idea what you meant until now. Good question, though, now that I do understand it ;-) –  nikie Feb 23 '11 at 8:13
show 2 more comments

The best C++ interview question would be a programming problem, not a quiz question.

share
4  
Even something as simple as "write a function to reverse a string" can tell you a lot about an expert programmer: do they use CString, char*, std::string, etc.; do they return a new string or reverse in place; do they manually loop over the characters or do they call a library function. And of course if they can't do something simple like reverse a string, that also tells you a lot about them! There are also lots of follow-up questions, like does it work with Unicode, does it work with UTF-8, etc. –  Gabe Dec 13 '10 at 6:03
16  
@Ernelli: C++ never really took off? Seriously? –  Steve Evers Dec 14 '10 at 15:02
1  
@Gabe - good point. For example, doing a template in-place reverse with a policy parameter would either be not very general, or (probably the policy rather the function) quite complex, when it comes to variable-bytes-per-character strings. I don't know whether no-premature-optimisation (create a new string because it's easier to be general) or agility (don't worry about that case until it happens, and the in-place reverse is no harder, maybe a tad easier, for fixed-width-character strings) would win out. –  Steve314 Jan 16 '11 at 20:28
1  
@Ernelli - you're forgetting wchar_t and std::wstring and them meaning different things on Windows/Unix –  Martin Beckett Mar 5 '11 at 0:02
show 3 more comments

I would ask them what they like about C++0x. From this I could "bucket" them into various stereotypes:

  • super old school, uses C++ compilers to compile C code
  • old school, scared of (or sees no point to) STL, hasn't been keeping up with the changes
  • loves lambdas, loves STL getting faster from rvalue references, big RAII fan, hot to use shared_ptr, unique_ptr etc
  • is bitter because all the boost code written over the last few years will need to be adapted to use C++0x equivalents
  • insane template metaprogrammer who makes my head explode while answering a relatively short question

Chances are some of these buckets are perfect for you and some are "thanks for coming in". As a question that gets you a lot of information fast, this is my winner.

share
1  
+1 for a good leading question that encourages a lot of discussion around the subject. –  Gary Rowe Dec 12 '10 at 22:43
8  
You forgot the category of people who have no idea what C++0x is. I guess that would be more of an answer, and would fit the first two types. –  Fast Fish Dec 13 '10 at 0:40
1  
@Fast Fish, yes, people who are like C++0-whatnow? will generally be one of the first two categories, and typically will illuminate that in their answer. –  Kate Gregory Dec 14 '10 at 14:07
add comment

I am somewhat bemused as to why "can you delete this?" is considered to be an interesting question. Anyone who has written COM code in C++ knows that the very first thing you learn on day one of basic COM programming is how to use "delete this" correctly. I suppose it might be useful to determine whether, say, someone is lying on their resume about having written COM programs, but as a general knowledge C++ question, if someone couldn't answer it immediately then they are not going to be a good fit for my team.

Anyway, if your goal is to come up with a question that measures C++ skills, then picking one question is the wrong way to go. Turn it around. The right question to ask is:

On a scale from one to ten, how good a C++ programmer are you?

This is not the question that gets you the answer you want. Everyone says "eight" regardless. The question that gets you the answer you want is:

OK, so you're an eight. What is a problem area that you think a seven would have a difficult time working with?

And boom, now you've got them. If the candidate thinks that "something to do with recursion" or "when to use a virtual destructor" is the sort of thing that a seven has a hard time with, then you know that they know a little bit about recursion or destructors or whatever, and that their knowledge does not go much farther than that.

That should give you much better calibration than coming up with some trivia question. If you were to force me to come up with a good single question about a fact about C++, I'd probably ask something like "how would you design the semantic analyzer and code generator for the portion of a C++ compiler that deals with virtual methods called in a base class destructor?" You should ask questions that have to do with real stuff that you work on, and that the candidate is likely to work on. That's a problem I had to work on once, and I think it would give a pretty good insight into how a person designs semantic analyzers and code generators, as well as their knowledge of C++.

share
3  
I'm going to start using this one: OK, so you're an eight. What is a problem area that you think a seven would have a difficult time working with? –  David Thielen Dec 14 '10 at 4:04
10  
Another good question here: what could a nine do that you'd have trouble with? –  David Thornley Feb 17 '11 at 20:41
show 2 more comments

Just for fun, I've had C++ programmers reeling with this little baby:

Why does this run into an infinite loop (and yes it's typed correctly)?

int x=0;
while (x<3) {
  x = x++;
}

It's amazing how many get tripped up. Of course, I can't use it any more after it appeared on Stack Overflow. Mutter... mutter...

(To get the proper answer read the confession and definitely read the comments)

Added bonus question

There is the all-time classic FizzBuzz (as featured in the now-famous Coding Horror article). I've never actually used it in an interview myself, but after chucking it around the development team one lunchtime the results were... um... surprisingly accurate.

Confession

I was caught up on the standard "x++ returns the original value" response. However, the correct answer is that the behaviour is undefined because of sequence points. Nobody ever mentioned sequence points in all the times I used this until I posted it here.

I have been educated and I'd genuinely like to thank all those who have taken the time to comment.

share
1  
Gary Rowe: Whilst I love that example (first time I've seen it). It does sadden me that people do not see that fairly quickly, let alone not know. –  Orbling Dec 12 '10 at 22:33
2  
When I was at Microsoft I started asking applicants to do an insert in a linked list. Why? Because about 80% couldn't do it. Amazing... –  David Thielen Dec 12 '10 at 22:48
43  
This is not necessarily an infinite loop. You're invoking undefined behavior here (x is modified twice with no intervening sequence point). And in fact, with my particular compiler, gcc (Debian 4.3.2-1.1) 4.3.2 on my particular arch (x86) it does terminate. –  Logan Capaldo Dec 13 '10 at 2:29
2  
@Logan +1 for mentioning sequence points. You'd get a second interview for sure ;-) –  Gary Rowe Dec 13 '10 at 9:35
4  
This is a really good interview question because it tells the candidate whether it's worth coming back for another interview ;-) . If the interview really thinks that the code does result in an infinite loop (and the interviewer is supposed to be technically competent) then the candidate can safely give the "opportunity" a miss. –  Charles Bailey Dec 13 '10 at 11:44
show 20 more comments

The first thing to ask is a simple question about pointers. I was astonished to see how many people who claim to know C++ or, even worse, C, are unable to answer it. Then I would ask a simple question about virtual functions. This combination tells you very quickly whether or not the person actually knows C++.

share
4  
depends on their background, never met someone with a hardware background who didn't understand pointers but some who didn't really get encapsulation or polymorphism –  jk. Feb 23 '11 at 19:55
add comment

That depends a lot on what kind of C++ is written at your company. (When I had to hire C++ programmers, I never asked about delete, because I asked them about techniques to avoid having to write delete altogether.)

Ideally, you want to hire people that write code above the level of what your team currently writes, but not too high above it, unless you want to combine this with training the team to take it to a higher level.

share
add comment

Tell me about the copy constructor.

share
2  
+1. That one weeds out the incompetent candidates in five seconds. –  Nemanja Trifunovic Feb 17 '11 at 17:42
add comment

I believe I would ask them what else they know, or more likely give them a question in something quite different.

Far too many C++ coders about who have little experience of other languages. Narrow experience is usually restrictive.

share
add comment

The best question for a great C++ developer is "Tell me the reasons why we should NOT use C++ ?"

share
add comment

I don't think there is a best C++ question. C++ is such a large language, and with C++0X it's grown, that you can be in strong in a certain subdomain of the language and no little about another part of the language. A 'one shot' question will only exercise someone's knowledge of a small part of the language and risk overlooking his or her's knowledge of other aspects of the language.

share
add comment

My favorite question to ask is a simple one:

a) Did you start with C and then switch to C++?

b) If so, then what were the first few things you learned to do differently?

This answer usually gives me a bead on how the programmer approaches C++ and his/her grasp of OOP and C++ in general.

there's no "Right" or "Wrong" answer here, but usually it's a great way to get a sense of where the candidate is coming from.

share
add comment

I would actually present the person with a bit of code from another programming language that was object oriented and preferably not on the list of languages they know. The code should represent a simple task done in that language.

The point of this would not be to determine their knowledge of C++ but find out how well they understand programming. When you understand the basic concepts behind programming, then you can solve problems.

share
add comment

How do you implement Virtual Functions in C++ , explain a real world scenario.

share
add comment

At the moment, I would say: "How would you go about implementing a move constructor for a container?"

My rationale is that, because move constructors are a feature of the next standard and not the current one, the candidate's answer will indicate whether the developer in question actively pursues knowledge about their language or just sticks to the tricks they know.

share
add comment

I think I'd ask their opinion of the design of, say, std::string. I'd also ask them if they've used other designs (e.g., MFC/ATL CString, wxString, etc.) and assuming they have, ask them to compare and contrast the designs.

Strings are widely enough used that almost anybody with real experience using C++ should have used at least one or two of the above. Almost all designs embody some compromises between theoretical purity and practical use. Some can work really well, but only if used exactly as intended, while others trade greater versatility for some possible clumsiness. In short, almost anybody who has real experience should be able to express some meaningful opinions of their designs and those opinions should tell you a fair amount about how they think, how they design code, the degree to which they value pragmatism versus theoretical purity, and so on.

share
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.