dimanche 11 janvier 2009

C++ interview questions/inspiration - part 2

So, the candidate has given an ok answer to the first question and/or you feel they deserve a second chance to impress you. I looooove this second question. It can really show how much of a professional the candidate is.

Here is the question :

Read the following program (the code is at the end of the post).

a) If the second dynamic memory allocation fails by throwing an exception, what is printed on the console?

b) Where is the bug in the application ; how would you fix it?


I love it because there is fun to be had for everyone. If the candidate is kinda junior, make sure he can tell the difference between the objects that are dynamically allocated vs those that are allocated on the stack. If the candidate can't tell the stack from the heap, you can skip the rest ; it's hopeless.

The key to the first part of the answer is to know that the members of an object are initialized in the order they are declared in the class, not in the order of the initialization list. The door class is written with that in mind. All the rest is straightforward for the first part of the test.

The question b) is much more interesting. The idea here is to make sure the candidate understands RAII. Bonus if he can mention it by name, but that's not what is important.

A senior programmer will jump on the memory leak like a wolf on a sheep, but others might not. I suggest you don't fail those that don't get it right away. Ask them if they know what a smart pointer is ; if they ever used one. If you can get someone to say that it's a good idea to use boost::scoped_ptr in this case, it's a keeper :-)

I also always ask the hidden sub-question c) What's the difference between boost::shared_ptr and boost::scoped_ptr ; and what is wrong with std::auto_ptr? There is no such thing as a senior C++ programmer that has never heard of boost. Call me elitist all you want, you won't be able to convince me otherwise.

Now, my favorite interview questions always allows exceptionnal candidates to distinguish themselves. In this case, if you get a pretty good answer to question b), repeat the question a) with the fixed program. It's harder, because the candidate has to know how destructors are called in the stack unwiding process. Once again, if he can't quite do it, help him a little. Tell him "The destructor is called if the object was fully constructed, and an object is fully constructed when the flow of the program leaves a constructor normaly." ; it should help.

If you ever try it on a candidate, tell me how it goes :-) I hope it will be as usefull to you as it has been to me.

#include <iostream>

struct door_handle
{
door_handle()
{
std::cout << "c-door_handle\n";
}

~door_handle()
{
std::cout << "d-door_handle\n";
}
};

struct window
{
window()
{
std::cout << "c-window\n";
}

~window()
{
std::cout << "d-window\n";
}
};

struct door
{
door_handle* m_door_handle;
window* m_window;
door()
: m_window( new window() )
, m_door_handle( new door_handle() )
{

}

~door()
{
delete m_window;
delete m_door_handle;
}
};

struct car
{
door m_left_door;
door m_right_door;
};

struct acura : public car
{
};

int main()
{
try
{
acura some_car;
std::cout << "we have a car!\n";
}
catch( ... )
{
std::cout << "error\n";
return 1;
}
std::cout << "bye-bye\n";
return 0;
}


Update

Parts three and four are now available.

2 commentaires:

UncleZeiv a dit...

In my experience the compiler always warns about declaration order and initialization order being different; unlike the other things you mention I can't see this as a sign of a deep conceptual understanding of C++. Very interesting interview questions anyway.

MAD a dit...

UncleZeiv, you are right. This specific C++ trivia is not all that interesting and that's why it's not important to get it right the first time. To me that's the whole difference between a test (offline) and a technical interview.

In the interview you can help the candidate - tell him what the rule is if he doesn't know it. This way what you are really testing for is the candidate's ability to learn and communicate.

In practice, the difference between the pros and the rest is obvious. Sometimes I hear people that say "Oh, I don't usually call delete, I guess I am being a little wasteful". Or better yet "exceptions are not used in the industry ; that's the try-catch thing, right?".