There is no one magical question that can determine if someone is the right candidate. There isn't even one that will determine if their technical knowledge is up to par. But there are those occasional questions that provide a lot of bang for the buck – simple questions that give you a lot of feedback about the candidate.
If I could ask a C++ programmer just one technical question, this would be it. Why? Because the correct answer feels wrong and the initial answer is generally wrong. So seeing how they work through the explanation is very illustrative of not just their knowledge of C++, but their willingness to not get locked in to their initial answer.
Q: Can you call "delete this;" inside a member function?
The instinctual answer of almost everyone is No. Not only no but there is a visceral emotional response from most that doing so would be like crossing the beams in Ghostbusters – very bad news. Ok, so next step, regardless of if they answer Yes or No is to ask "Why?"
This is where you really learn a lot. Even people who answer Yes (and they are few and far between), most of them have trouble explaining what is going on. I have seen many, over 5 – 10 very painful minutes, sketch out most of what is going on, and yet still say that you cannot do it. These are people you do not want to hire under any circumstances because they cannot think outside their initial preconceptions.
I have also had many with some of the most outlandish understandings of what is actually going on in the computer. Many people think when you call the delete that the memory the code is running on is garbage collected. Not the data – the code! Some think the v-table disappears on calling a delete (it doesn't as it's tied to the class) – and they think the v-table is needed for the return from the method.
Ok, so you then have one of the few who has worked through the above, got it figured out, and you're feeling good. We then have a follow-up question that will drop out half of the remaining candidates – "What can you do after calling delete this?"
And you will get answers that are all over the board from "anything" to "nothing." I've had many ask me if local variables are consider part of the object memory – so at least those people are thinking it through even if they don't fully understand what belongs to an object.
So there you go – a great interview question until it is over-used enough that everyone knows about it (like the manhole cover question).
Update: This has received a large number of comments (especially on reddit). I want to stress one key point - I don't ask this because I expect most people to get the initial answer right. 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. As to the question of why would you ever do this - it is very useful in message based systems and when threads are exiting.
PS – If you have a great "just one question" – please post it in a comment below.
And if you need a great C++ reporting system, please check out Windward.


My favorite question - What is the output of following code snippet?
lass Base {
public:
virtual void f(int x = 10) {
cout << x << endl;
}
};
class Derived: public Base {
public:
virtual void f(int x = 20) {
cout << x << endl;
}
};
int main() {
//cout << floor((0.7 + 0.6) * 10) << endl;
Base *b = new Derived();
b->f();
delete b;
return 0;
}
Posted by: PK | 12/08/2009 at 01:56 AM
templated virtual member functions... you wish (I wish too).
After writing a serialization/reflection/dynamic system in C++, I know the language unfortunately well (as in ISO C++ Standard, because Stroustup's book isn't specific enough). That said, there are two ways to interpret the question: is "delete this" valid in terms of the standard stack/data/heap runtime model, or is "delete this" valid in terms of the abstract virtual machine defined in the GIANT BOOK?
I believe the answer is yes and yes. For comparison.. the book says that it's OK to do (in a class C)
inside a member function of on object C(). I am unsure about how DERIVED classes are meant to handle this..., I would guess that in a sane runtime model this would be OK if C didn't have a vtable. So... the book says it's OK, but, please no, don't do it in maintainable code.Posted by: Adam | 12/08/2009 at 07:15 AM
As far as finding out whether the candidate *really* knows C++, this is a fine question. I'm sure it's not the only question you ask in an interview. There are many talented programmers who end up detracting from team for lack of interpersonal skills that are as important as technical skills.
Posted by: Kent | 12/08/2009 at 07:50 AM
I'm not a C++ expert (very far from one, and proud of that :P ) and I find all the questions of this article very very easy, and that includes the "why" part. IMHO so should do anybody with medium knowledge in C and/or computer architecture. The problem are people learning C++ as such, in a features oriented way, without even looking for or thinking about how things are implemented. While this is OK for programmers only using managed languages (and I _do_ think they have a room in the programming field!), it would be an absolute error to hire somebody to program in C++ lacking so much knowledge about computer architecture he is not immediately able to correctly answer your questions. He would have difficulties in a LOT of activities, including debugging, probably would not be able (at least during a very long period) to do multiprogramming in a correct and safe way, and so over.
Find a way to let those kind of people program in managed languages, they will be so more productive.
Posted by: thefre | 12/08/2009 at 09:27 AM
" I have seen many, over 5 – 10 very painful minutes, sketch out most of what is going on, and yet still say that you cannot do it. These are people you do not want to hire under any circumstances because they cannot think outside their initial preconceptions."
This reminds me of a true story: A manager gets back to the office after lunch with a prospective hire. The manager proudly tells his colleague that he didn't hire the candidate because he didn't taste his food before putting salt on it.
Try to remember that you're interviewing people because you need them to do a job. Try having them do that job.
Mark.
Posted by: Mark Maunder | 12/08/2009 at 09:42 AM
My favorite interview question:
"We have discovered a useful idiom in C++, that of implementing the entire body of our program in the destructor of a global object. Can you think of a reason why we might want to do this?"
BTW, there is no v-table in C++.
Posted by: Paul Brannan | 12/08/2009 at 12:18 PM
I'm not saying it's a bad interview question at all. (Hi, Ryan. How's it going? Say hi to Ally for me.)
I'm just saying this isn't even close to the best interview question ever. This is introductory material to see if the person is worth the rest of the interview, at best.
Posted by: John Haugeland | 12/08/2009 at 01:29 PM
"delete this" is actually a fairly common idiom in event-driven systems, and it's perfectly OK -- provided that the code does not reference any member variables or non-static methods of the deleted object following the delete.
Posted by: Billathrst | 12/08/2009 at 05:09 PM
another gem stupid question from your side
my best question ever:
can we call "delete this stupid blog" from internet
Posted by: speedy car | 01/19/2010 at 06:48 PM
I think you are trying to select out the candidate who can play acrobat trick with the language. However, I doubt you can pick up the right guy. The reason is quite obvious, language, to the very end, is the tool engineers use to achieve their goals. You need to pick up an engineer rather than a guy who can play fancy tricks of a tool. Although an engineer may play some tricks also, but these are by-product (and not the purpose) of their engineering life. The important thing is that you need to pick up an engineer who can contribute most to your project. If really such a trick is needed in your project, he can learn and master it in 5 minutes, but an acrobat may not be able do that.
So, why not ask directly to your point? Ask him what project he has done and how he achieved / accomplished, is more worth your interview time than asking such trivial tricks which can be figured out in 5 minutes by google or simply a textbook he learned 10 years ago in the university.
Posted by: David | 10/16/2010 at 09:02 PM
A good programmer will never give you the yes/no answer just like this, give him some space to get wired in, he'll be back with the answer.
Posted by: Ma2moun | 12/12/2010 at 06:28 PM
Hi all,
If the contructor has thrown an exception then its desctructor is NOT called (an object is considered fully ocnstructed only upon exit from the cionstructor).
Maybe calling delete here will work, but that might be a BAD IDEA.
Posted by: S | 12/24/2010 at 06:07 AM
Hi S;
If the constructor throws an exception you will never get the object and therefore cannot call a member function. So not an issue.
Posted by: David Thielen | 12/27/2010 at 09:06 AM
Now software industry has been taken over by developers from Hyderabad (India). No one cares about in-depth learning. Neither hiring managers nor the developers care to understand fundamentals. You write the program, test it and deliver it. This works most of the time. Once in a while it may fails. Companies do not care most of the time. Focus is on volume of the work finished. I work in financial companies. Most (90 %+) of developers coming from other countries have below average education (but have 4+ college degree). Most of them do not understand the inner working of the machine. Most prepare interviews by reading FAQs from different web sites.
It is irony that Hyderabad has most fraudulent companies where one of the largest development center of Microsoft is located in India.
Posted by: Sunil | 05/13/2011 at 12:37 PM