lundi 13 avril 2009

C++ interview questions/inspiration - part 3

Back to basics

Early exit is a nice and simple optimization strategy. If you are designing an interview algorithm to help you select great hackers, you can expect most candidates to fail the test. That's why you should try to sort your interview questions. By putting the better discriminants first, you will know sooner if the candidate is not quite as good as you would like him to be.

Knowing how good the candidate is early in the interview does not necessarily mean asking fewer questions, it means making the most of the little time you've got. If the guy looks like a super star you will want to skip one or two easy questions and get with the fun stuff. With super stars, it's the interviewer that has to show how good he is. Stars like to be asked tough, relevant questions. An easy interview indicates that anyone could get the position - and that the 'winner' will be surrounded with mediocre coders.

All this to say : don't focus on this 3rd question if the candidate looks like a star. Don't skip it entirely either, it's not that bad a discriminant. You definitely want to be sure that everyone that works with you can answer it straight, it's just not very challenging to experienced C++ programmers.

The feature presentation

a) What is printed on the console after the execution of this program?
b) What would you have done differently?



#include <iostream>


struct animal
{
~animal()
{
std::cout << "bye\n";
}

virtual void print()
{
std::cout << "I am an animal.\n";
}
};

struct bird : public animal
{
~bird()
{
std::cout << "bye bye birdie\n";
}

virtual void print()
{
std::cout << "I am a bird.\n";
}
};

void print( animal a )
{
a.print();
}


int main()
{
animal* a = new bird();
print( *a );
delete a;
return 0;
}


Low level

Can he spot the obvious errors? The print free function taking its argument by copy. The destructor of the super class not being virtual. Can he explain what the default copy constructor does? Does he know how to disable it? What about the assignment operator - did he think about it by himself? Virtual member functions - how do they work? What happens if the derived class's destructor is specified as virtual but not the super class? What if it's the other way around? Ever heard of a vtable? A vpointer?

High level

Will the candidate only worry about the low level details or will he think about the design globally? Does he know his sacred books? Can he actually recite Meyers and say "Make non-leaf classes abstract". Why make the super class's destructor virtual if there is nothing to free? Program in the future tense, of course.

Dazed and confused

Only the best will be able to speak about high level concerns easily, others will need a little help. You can help them with a derived example

int main()
{
animal* a = new bird();
animal other;
*a = other;
print( *a );
delete a;
return 0;
}
Is it sending them on the wild goose chase of virtual assignment operators or do they know better? It's ok to need a little help - it's not ok to become confused and disoriented.

Stay tuned for the next episode - it's a tough one.

Update

Part 4 is now available.

Aucun commentaire: