views:

1579

answers:

14

I have to interview some C++ candidates over the next few weeks and as the most senior programmer in the company I'm expected to try and figure out whether these people know what they are doing.

So has anybody got any suggestions?

Personally I hate being left in a room to fill out some C++ questions so I'd rather do a more complex test that I can chat with the interviewee about their approaches and so forth as we go along. ie it doesn't matter whether they get the right answers or not its how they approach the problem that interests me. I don't care whether they understand obscure features of the language but I do care that they have a good solid understanding of pointers as well as understanding the under lying differences between pointers and references. I would also love to see how they approach optimisation of a given problem because solid fast code is a must, in my opinion.

So any suggestions along these lines would be greatly appreciated!

+2  A: 

This article offers some general ideas that are relevant regardless of what language you're working with.

Amber
+5  A: 

Checkout Joel's Guerrilla guide to interviewing. Seems a lot like what you are looking for.

kripto_ash
+8  A: 

This one is a great complex task, even though it is looking quite harmless.

h0b0
Fantastic read and definitely makes a valid point on C++ and large projects.
jim
I can't be the only one who, on being posed that question, would ask the interviewer "can I use a std::string internally, and only expose the name as a const char* via an accessor?" Resulting source is about the size of the initial wrong answer, and unlikely to be very much less efficient than the answer he finally settles for.
Steve Jessop
It is actually rather surprising that people claiming 3 years of C++ experience would have problems with that. Anyone ever got to copy-and-swap idiom for assignment (got to the place where you might want to start considering stronger exception guarantees)? :)
UncleBens
The correct response for a C++ programmer is to point out that representing the name as "char *" is dumb. If you insist on it, the correct response is to ask if you micromanage everything. The problem is that you will lose out on good C++ programmers who aren't assertive in interviews.
David Thornley
I'm not sure what the relevance of this is. The question was not "is C++ a good language for large projects", but about interviewing candidates for a C++ position.
jalf
@UncleBens: That particular article is from 2001. I don't remember when people got the hang of exception safety in C++ (I think it was very roughly then), but not a whole lot of people knew how to handle exceptions back then. Nowadays I'd like C++ people to have a clue about exception guarantees, but at that time it would have been unusual.
David Thornley
@jalf: most of that linked article actually is not directly to do with its title, and is a (perhaps stereotyped) account of a "typical" interview programming task. The point of the article is that almost everyone fails to produce competent code (and, hence, in the author's opinion, C++ is "too hard"). Since the question is about how to test who can competently write C++, it seems relevant to me :-)
Steve Jessop
+2  A: 

You can choose potentially problematic task and see how they approach it. Ask them to write a smart pointer for example, you'll see if they understand pointers, references and templates in one step :) Usually they are stressed so they will do mistakes, those mistakes might help you find out how good they problem solving skills are, what paths would they use to fix a mistake and so on. The only problem with this approach is that sometimes interviewee just don't know anything about the task and you would have to quickly figure out something easier. If they do perfect code you can discuss their choices but when there's nothing to look at it is depressing for both of you.

vava
Good point on asking relevant questions. No point in asking someone a question on x86 assembler latency/throughput when they are a higher level programmer ;)
Goz
+8  A: 

I wouldn't make them write code. Instead, I'd give them a couple of code snippets to review.

For example, the first would revolve about design by contract: see if they know what preconditions, postconditions and invariants are. Do a couple of small mistakes (never initialize an integer field, but assert that it is >0 in the invariant) and see if they spot them.

The second would be to give them bool ContainedIn(char * inString). Implement it with a trivial loop. Then ask whether there are any mistakes. Of course, your code here does not check for null in the input parameter in_string (even if the very previous question talked about preconditions!). Also, the loop finishes at character 0. Of course, the candidate should spot the possible problems and then insist in using std::string instead of this char * crap. It's important because if they do complain, you'll know that they won't add their own char *'s to new code.

An alternative which addresses STL containers: give them a std::vector and code which searches for prime numbers or counts the odd numbers or something. Make some small mistake. See if they find any issues and they understand the code. Ask in which situation a std::set would be better (when you are going to search elements quite systematically and original order of entrance doesn't matter.).

Discuss everything live, letting them think a couple minutes. Capture the essence of what they say. Don't focus on "coverage" (how many things they spot) because some people may be stressed. Listen to what they actually say, and see if it makes any sense.

I disagree with writing code in interviews. I'd never ask anyone to write code. I know my handwritten code would probably suck in a situation like that. (Actually, I've seldom been asked to do so, but when I have, I haven't been hired.)

Daniel Daranas
I DO fully agree on the not writing code. I was more thinking about providing them with a question and asking how they would approach the problem. Good idea to do a "find the f**k ups" in a block of code though! Knowing my luck, though, they'd spot a mistake i hadn't noticed ... I've done that before ;)
Goz
@Goz I think a question about approaching a problem would be good, too.
Daniel Daranas
I don't know that making sure they prefer std::string over "char *" is really helpful in assessing the general abilities of a programmer, regardless of their experience with C++. I like the suggestions about having them look for coding mistakes, though.
Reuben
The sample is pretty good, I guess a programmer saying char * should be replaced by string or we should check for NULL pointer. It will be too harsh to ask buried mistakes and write a code at the first interview. Most applicants will be exited and they will jump to code right away, without thinking on the subject, resulting a very crappy code.
Cem Kalyoncu
+5  A: 

I believe that a C++ programmer needs more than just generic programming skills, because...

In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.

Writing bug-free, maintainable C++ code places a much higher demand on a few areas than most languages.

One thing I'll call "pedanticness". You know how some people can spot spelling errors in something at a glance? A C++ programmer needs to be able to spot simple bugs while they read or write code (whether the code is their own or not). A programmer who relies on the "compile and test" technique just to get rid of simple bugs is incompatible with the C++ language, because those bugs don't always lead to immediate failure in C++.

C++ programmers also need a good knowledge of low-level stuff. Pointers, memory allocators, blocking, deadlocks. And "nitty gritty" C++ issues, like multiple inheritance and method hiding and such, particularly if they need to maintain other people's code.

Finally, C++ programmers need to be able to write code that's easy for other people to use. Can they design stuff well?

A good test for the first two areas is "Here's some C++ code I got off the internet. Find the bugs, and identify the unneccessary bits." (There's lots of really bad C++ code available on the internet, and often the programmer does unnecessary things due to a faulty understanding of how to be "safe" in C++.)

The last area you can test with more generic interview questions.

Artelius
The first paragraph is not a bad point. You probably don't want someone who assumes "if it compiles, it's correct" (or even "if it runs, it's correct")
jalf
Indeed, good debugging skills are incredibly useful but nothing like as useful as not writing the bugs in the first place.
Goz
What I had in mind was more about people who *know* that code which appears to run may have logic bugs, but nonetheless rely on testing to remove all the "implementation" bugs. That is, the bugs that arise because the code you write doesn't reflect your mental model of the program. Like accidentally using the wrong variable name. (Unlike the bugs that arise because your mental model is itself broken.)
Artelius
+3  A: 

Depending on what your organisation's pre-screening is like, assume that the person knows nothing at all about C++ and has just put in on their CV because it makes them look supertechnical. Seriously. Start with something simple, like reversing a string. I have had candidates who couldn't even write a function prototype for this !!

tragomaskhalos
And you can instantly see if they're good if they can do it in-place ;)
Goz
Nowadays it is quite hard problem as the string is always encoded in UTF-8 :)
vava
Reversing a UTF-8 string isn't hard, just requires a bit of arcane knowledge.
Artelius
Certainly harder than reversing the bytes, though! If people have trouble with THAT......
Artelius
@Artelius, try to write code reversing UTF-8 on an interview :) It is much harder than you think.
vava
@vava: not that bad. Run along the string, identifying each character and reversing the bytes just in that character. Once that's done, reverse all the bytes in the whole thing. That said, I'd need a reference open in front of me to write the "find the length of the next character" function, and there's a design decision what to do on encountering invalid byte sequences (in particular, do you abort and leave the string in a mangled state).
Steve Jessop
Yes good point about UTF-8, I should have specified that the string is ASCII or UTF-16; the idea is to see if the person knows basic pointer manipulation and/or can do it using the STL, not to worry about variable length encodings. Obviously bonus points to the candidate who points this out themselves!
tragomaskhalos
A: 

Whatever you do, pairing would be a good idea. Come up with a good program and pair with the guy and work towards solving the problem. IMHO, that a very good idea

Manish Chakravarty
+4  A: 

Do not forget to also test for code bigotry. I know I don't want anyone working for or with me that isn't a flexible and consequently practical programmer both in their attitude to the programming language, but also in their approach to problem solving.

  • Denying any type of preconceptions
  • Understanding the value of the exceptions in any Best Practices
  • Being capable of refusing long term habits in favor of something else if the need arises

These are characteristics dear to me. The manner of testing for these is not ideal if the interviews aren't lengthy or don't involve presenting code. But showing code snippets with purposely debatable techniques while offering a use case scenario and asking the candidate how they feel about the solution is one way.

Krugar
Damned good point. Have come across far too many programmers that can't recognise that straight design patterns are not necessarily the best solution to every problem.
Goz
So the questions: "when have you used goto?"; "should you check a pointer parameter for NULL before using it?"; and "singleton?" should give answers respectively: "almost never, but I sometimes think about it"; "depends whether my caller is a muppet or not"; and "no, I'm engaged actually, and don't call me Tone"? ;-)
Steve Jessop
+2  A: 

Here is my answer to a similar question geared towards C#, but notice that my answer is language agnostic. My interview question is, in fact, in C. I rarely interview a person with the goal of finding out if they can program. I want to find out if they can think, problem solve, collaborate, communicate, understand something new, and so on. In the meantime, I circle around trying to see if they "get it" in terms of the big picture of software engineering. I use programming questions because that's a common basis and an easy ruse.

plinth
A: 

So has anybody got any suggestions?

I'd recommend getting a copy of this:

http://www.amazon.co.uk/Programming-Interviews-Exposed-Secrets-Programmer/dp/047012167X/ref=sr%5F1%5F1?ie=UTF8&s=books&qid=1252499175&sr=8-1

ie it doesn't matter whether they get the right answers or not its how they approach the problem that interests me

You could ask the candidate to come up with a UML design to a common problem. If they show you a design pattern, then you can talk through the pros/cons of the pattern. You could then ask them to produce some code for one of the classes.

This would help you determine their technical knowledge level and their communication abilities.

I do care that they have a good solid understanding of pointers as well as understanding the under lying differences between pointers and references

Linked list problems are good for determining whether a candidate has a solid grasp of pointers.

As for references, you could show them some code that does not use references correctly, and ask them to describe the problem.

e.g show them a class definition that contains a reference member variable, and the implementation of the constructor with the reference initialization missing.

I would also love to see how they approach optimisation of a given problem because solid fast code is a must, in my opinion.

I'd start off simple...

Show them a code example that passes strings to a function by value. (the strings should not be modified in the function). You should check they correct the code to pass the strings by const reference.

After this, you could show a constructor that uses assignment instead of initialization (for objects). Ask them to improve it.

Lastly, ask them simple questions about data structure selection.

e.g. When they should use a list rather than a vector.

If you feel they have a grasp of the fundamentals you could either ask how they approach optimization problems (discuss profilers etc), or ask them to optimize something less obvious.

Steven
+2  A: 

Don't test only the C++ and overall technical skills! Those are of course important, but they are nothing if people don't listen, don't answer properly or don't follow the commitments they made.

Check at most for the ability to clearly communicate. If people cant tell you what roughly they did in their former jobs within a few minutes, they will also be unable to report about their work at your place etc.

In a recent company we invited people for interviews in groups of about 3 people together. They were surprised, but nobody was angry about that. It was very interesting, because people had to communicate not only with us, but also with others in the same position. In case we were interested further, we arranged a second interview.

RED SOFT ADAIR
Yeah, interpersonal skills are VERY important. Amongst other things why employ someone you cannot drink with? ;)
Goz
I'd actually advice the liquor test first!
RED SOFT ADAIR
+6  A: 

A few questions can allow you to know a lot about a candidate:

  • Differences between a pointer and a reference, when would you use each?
  • Why would you make a destructor virtual?
  • Where is the construction order of a class attributes defined?
  • Copy constructor and operator=. When would you implement them? When would you make them private?
  • When would you use smart pointers? what things would you take into account to decide which?
  • Where else have you seen RAII idiom?
  • When would you make a parameter const? when a method?
  • When would you make an attribute mutable?
  • What is virtual inheritance?
  • What is template specialization?
  • What are traits?
  • What are policies?
  • What is SFINAE?
  • What do you know about C++Ox standard?
  • What boost libraries have you used?
  • What C++ books have you read? (Sutter? Alexandrescu?)

Some short exercises (no more than 10 minutes) about STL containers, memory management, slicing, etc. would also be useful. I would allow him to do it in a computer with a ready environment. It's important to observe the agility.

fnieto
Sry I might be a little harsh, but are you trying to interview a scholar or a programmer. Info that should be covered in a book does not worth asking. Programming is about skill, experience and their approach on problems not just knowledge. A person accepted through your questions might still write bad code.
Cem Kalyoncu
Most of the C++ developers out there have over 3 years of experience and won't be able to answer those questions properly, even some of the basic ones. This is a good way to know how if the candidate is a proactive person reading good books and with clear ideas or just a person doing a work he don't like. Of course their skills are important, read the last paragraph of my answer.
fnieto
I'd be careful about questions like these. In my work, we have some language "groupies" - They have read the standard cover to cover several times, follow the proceedings of C++0x committee, etc., etc. They're not that great programmers - to them, programming isn't so much about solving customer problems but instead an effort to create perfect C++ programs. They view working, stable, legacy code that doesn't use exceptions as an abomination that must be fixed, even though expending that effort on code that is in maintance-mode would have zero customer benefit.
Michael
My above comment refers to some of the more specialized questions you asked (i.e., what is SFINAE) that do not come up in day-to-day C++ development.
Michael
This questions are not a panacea. Are just a bunch of short questions with a incremental difficulty level to make yourself an idea about the candidate c++ knowledge. Client orientation and many other important features for a candidate are not covered because are not the question subject.
fnieto
Michael, Google knows what is SFINAE. Google knows almost everything...
fnieto
+4  A: 

"Write a program that receives 3 integers in the range of 0..2^32-1, and validates if they represent valid edges of a triangle".

It seems to be a simple question. The input is considered valid if the sum of any two edges is greater than the third edge. However, there are some pitfalls, that a good programmer will handle:

  • The correct type to use should be unsigned long. Many "programmers" will fail here.
  • Zero values should be considered as non-valid.
  • Overflow should be avoided: "if (a+b <= c) return false" is problematic since a+b may cause an overflow.
  • if (a <= c-b) is also bad solution since c-b may be negative. Not a good thing for unsigned types.
  • if (c > b) { if (a <= c-b) return false; } else { if (a <= b-c) return false; } This is much better.

A good programmer must be detail oriented. This simple exercise will help checking if he is.

Lior Kogan