« Our new ribbon UI | Main | A better GetCookie() »

12/06/2009

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a0115711bf0ae970b01287620cbf3970c

Listed below are links to weblogs that reference The Best C++ Interview Question – Ever!:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Seriously, knowing when a singleton can clean itself up safely is the best interview question ever?

You guys need more difficult interviews. The average college sophomore knows this.

C++ trivia is the best interview question ever? Are you joking?

Uh, what was the question?
I believe my answer to this if I were in an interview would be something along the lines of, either
"I don't think you can ask about my member's functionality during an interview, that would be discrimination." or "I finally deleted my Members Only jacket, after I realized I was the only member left." and then try to distract the interviewer by asking whether he bought his hair piece from Taiwan or Thailand?

Are great interview questions only questions that provide an excuse for removing someone from the applicant pool? If you need someone to design manhole covers, ask them a manhole cover design question. If you need someone to design software, ask them a software design question.

Apparently you've never had a really good candidate. The really good candidate would look at you with an archly raised brow and ask, in deadpan tone, "Why the hell would you even think about doing such a thing? What, are you nuts?"

You would think about doing it when you want to make the destructor private to enforce an internal reference counting scheme on a memory restricted embedded device.

@John:

> "Why the hell would you even think about doing such a thing? What, are you nuts?"

Auto-deleting thread classes frequently call 'delete this' just before the end of thread's execution.

You're a terrible interviewer. For the common good of all programmers worldwide, step down from your job and work in McDonalds.

The funny thing here is the "I'm so brilliant I understand that it's 'nuts' to do something like that" types compared to the "I'm so brilliant I know that there are the occasional times when its perfectly sensible" types.

Rather than bitching about which side is right - an argument that will go nowhere fast - I'ld be happy to make do with noting that a great interview question is one that helps ensure that everyone who gets hired can actually work together without constantly complaining that everyone else is nuts. And if they really are nuts, you don't want to work there anyway.

What depresses me is that a below average newbie ought to be able to answer this question easily - yet people still apply for jobs when they don't know the difference between a local variable and a member variable, imagine that every class has to have a vtable, imagine that the vtable is used to return from a function, and even imagine that C++ has garbage colection.

This stuff may be "trivia" but people who still don't know it certainly need to disqualify themselves asap.

So yes, ask a few trivial questions - and if the candidate can't answer them easily and confidently, assume they over-stated their many years of "experience".

I gotta say I reluctantly agree with @John Haugeland.

However, I can understand why you would ask such a question. Every person who's ever turned on a computer these days fancies themselves computer programmers.

One could also ask to read a pointer signature, or work with const/volatile keywords.

And example would be: why would you declare a function like so:
void doSomething() const;

Clearly, it doesn't add any functionality, so why do it?

I think perhaps a good answer would be to also explain what implications this might have on the rest of the program. "You could do it, but in most cases that's not a good place to release memory, since the existence of an instance usually implies that somewhere there is a reference to it. The referer should free the memory and remove its own reference. If the instance frees itself, this will corrupt any existing references in the rest of the program. So only in very unique circumstances could you get away with this approach."

Obscure C++ trivia.

http://www.goingware.com/tips/member-pointers.html

I'm sure the proper answer is "I don't know, but I can find out in 5 minutes."

While it may indeed be C++ trivia, you don't need to know the trivia to get it wrong. Even someone who never thought of this question in their wildest dreams can work out a solution if

a) They understand the memory model of C++ programs
b) They understand the basic nature of how compilation / loading / program execution works
c) They understand the semantics of object / member function interaction in C++.

So in that respect I agree with the author. This is a good question.

For the record, delete this is useful in rare occasions. Microsoft COM uses it, for example, so that IUnknown::Release() can free the object when the reference count reaches 0.

An interesting follow-up question to ask the few people who get it completely right is:

Assuming I do write a class that uses delete this, are there any implications to consider with regards to how the user *creates* instances of this class? If so, can the class be written in such a way that the compiler can enforce correctness in this regard?

bluh, obviously I meant "you don't need to know the trivia to get it right."

The discussion over on reddit has a good signal to noise ratio if your interested in the answer/discussing this one.

I'm not sure why so many people think this is trivia. Trivia is something obscure that you either know or do not know. This is clearly something that can be reasoned about and the correct answer can be arrived upon if you're willing to think about it a bit.

Would you want to employ a programmer who thought this was a good idea?

I'm going to disagree with my friend John, here. (Hi John!)

I like the question for the same reason that I advocate new programmers learn C++ even if they never plan on using it: It requires understanding what's *really* happening in the machine.

I've nothing against people who want to program in VB. Good for you that you've taken an above-average interest in computers and want to learn something new. That said, I'm not interested in hiring you.

I want people who understand what the machine is doing. It shows that they care about their craft enough to have delved into the inner workings of the machine. It shows that they aren't content in knowing that something works. They want to know *how* it works. THAT is the person I want to hire, regardless of the language they use, and regardless of whether they get this interview question correct.

As a technical question for a C++ job, it's very good. It will [hopefully] cause them to talk about vtables, local stack variables, the return from the jmp, dereferencing this after the delete, undefined behavior vs. a NPE, etc. If they get that stuff mostly correct, I can assume that they have the requisite experience and then I can move on to the more important parts of the interview, like fit-with-the-team, SCM practices, testing, demeanor, leadership, etc.

funny, I think the best part of this question is it removes people (like many of the commenters here) from the pool because they don't understand hypotheticals. this is a great question for reasoning through, even if the solution is unimportant. much more interesting than the number of piano tuners in Chicago.

It's been quite a while since I understood the internals to C++ but my instinctive answer would be "I don't know. Can we try it? Can we pull up a computer?"

It sounds like an interesting puzzle.

I have a question. What happens if you call delete this against an instance that is stack allocated?

your program is going to die, because the call to delete is going to pretend it's a heap block and your heap is going to become corrupt.

You partially answered the "follow-up" question I suggested at the end of my post above.

I particularly like asking:

What does (0.7 + 0.6) * 10 return?
What does floor((0.7 + 0.6) * 10) return?
Why does it return this?

In my experience, many people do not properly understand floating point math in a computer. More of them do not know how to minimize the impact of floating point errors.

I would think the first problem would be the fact that you're interviewing a C++ programmer. That in, and of itself, should make you question if they're actually capable of anything.

This is not a trivial question; it affords the interviewer an excellent opportunity to evaluate the candidates depth of knowledge, experience, judgment and devotion to the technology.

This isn't a particularly hard question either. My end-boss question is: "templated virtual member function?". This question probes deeply in to the core of the language, the runtime and the build system.

The comments to this entry are closed.

David Thielen

B.I. Tweets

    follow me on Twitter

    Windward Reports

    Quantcast

    • Quantcast