views:

670

answers:

12

Hi,

If a candidate says that his knowledge in C++ is 7/10 and you want to test his knowledge of references in C++, what question will you ask?

I thought of the following:

  1. Write a function declaration that takes a pointer as reference with default values and ask him to figure out a mistake and explain it.
  2. Pass a literal as argument to a function that takes that parameter as reference.

Any other question that is better at testing candidates overall knowledge of references in C++?

Thanks,

A: 

Both the stated examples are good however they don't necessarily test C++. I would rather ask him how a reference member variable is initialized in a class.

Ali
I'm not sure a true 7/10 would know that. If you are trying to catch someone lying, ask a 5/10 question.
T.E.D.
+16  A: 

Personally I dislike code puzzles. I'd much rather have the interviewer just ask me to explain references and actually engage in a conversation about them (or any topic). Start by asking the candidate to explain C++ style references. Ask followups and prompt for more information.

Brian Ensink
I can't agree more with this. Interesting article about interview questions here: http://www.aaronsw.com/weblog/hiring
Karim
A: 

See if your candidate can explain the difference between passing by value and passing by reference using the following example:

struct Value
{
   int x;
};


int main()
{
    Value v;
    v.x=1;
    foo(v);
    // what is the value of v.x?
    bar(v);
    // what is the value of v.x?
}


void foo(Value& value )
{
    value.x = 12;
}

void bar(Value value )
{
    value.x = -12;
}
Phillip Ngan
using that example you get a compile error
jk
trick question. bar != boo.
Adriano Varoli Piazza
`Value v = new Value();` ?
jon hanson
There is too much Java in this C++.
Kaz Dragon
A: 

I'd probably most want to know that they wouldn't return a reference to a local variable. other than that a discussion about the differences between pointers and references?

jk
A: 

Looks at Stack Overflow...

Ask him to write an assignment operator for a class that has, as a member, an instance of an inner class that has a reference member. That should do it.

ObRef: http://stackoverflow.com/questions/1832704/default-assignment-operator-in-inner-class-with-reference-members

Kaz Dragon
You could let them try, but you'd also need to make sure to describe the desired effect rather well. (And the accepted solution mentions this has nothing to do with an inner class.)
UncleBens
Absolutely, but it is a question that you would have to know exactly what references can and can't be used for in order to answer the question, which I think fits the OP precisely.
Kaz Dragon
+15  A: 

in my book 7/10 means very competent. and 9/10 means someone who can do generative programming targeting c++ or template meta programming and 10 is people who write portable libraries. I do not believe in trick questions - only broad knowledge. So, here is the questions I would ask.

  1. What does it mean for code to be exception safe.
  2. What are the differences and pitfalls between the is-a relationship and the has-a relationship.
  3. Why should pointers be used if array entries are polymorphic.
  4. what is the difference between std::vector and std::deque.
  5. What is std::map good for ?
  6. What is RAII or what strategy do you use to prevent resource leaks.
  7. Why would you declare a destructor virtual ?
  8. What does it mean to provide a partial template specialization ?
  9. What is a functor ?
  10. what does the mutable keyword do and what does it mean for a function to be declared with const at the end of the signature.
  11. Why do you override compiler generated fuctions.
  12. what does it mean to bind variables to a function in STL vocabulary.
  13. Why is it a good idea to use std::string over the C functions.
  14. Do you know of any good c++ libraries, what is special about them ?
  15. what strategies do you use to ensure code is portable.
  16. What can you - as an application programmer * do with templates ?
  17. Can C++ stl containers create a performance bottleneck, give me an example.
  18. Is there a specific scenario you can think of that would require custom memory allocation.
  19. What are smart pointers, are they smart ?
  20. What is the pimpl idiom / compiler firewall, or how do you ensure short compile times across dependencies.
  21. Why are exceptions from destructors bad ? can you think of a way to get arround this issue.
  22. What is heap allocation and what is stack allocation.... is calloc evil ?
  23. What are cyclic dependencies and you can you tell me of one or more ways it complicates c++ programming.
  24. What is RTTI ?
  25. can you think of any runtime machinery (e.g., the exception mechanism) that would make your life easier ?
  26. Java and C# are considered amazing languages. What project should be written in C++ ?
  27. What are your opinions on multiple inheritance ?
  28. Can you think of a reason to use the preprocessor ?
  29. what are the benifits of declaring variables const ?
  30. What tools do you use to develop ?

Could go on for ages :D but 30 will do , if you need more just ask :D

Hassan Syed
the question is not give me C++ interview questions, its about what test knowledge of references in C++ best.
Priyank Bolia
but don't delete it please, it's nice :)
Samuel_xL
Priyank, no - the OP is asking rather explicitly for questions.
Georg Fritzsche
yes but questions about references, not any old corner of c++
jk
You may just ask "See this book here? It is called 'The C++ Programming Language'. Please explain it to me."
Daniel Daranas
re "Whats wrong with the following code": The formatting. It hurts my eyes.
shylent
So what's wrong with it? No return statement? Undefined monkey? Some style issues?
UncleBens
there's nothing wrong with the code, reference-wise :oalthough i would usually pass a const reference if im not going to change the object.
rmn
I don't see what could be wrong with this code either
Samuel_xL
What's wrong with this code is that if a monkey and donkey are going to make a zebra, one of them is going to be in a lot of pain.
Mark Ruzon
A: 

Familiarity with references should emerge from other demonstrations of programming capability. If the candidate passes function arguments by value -- especially of non-primitive types -- you could probe why he made that choice, and ask him to explain the trades involved in that choice.

One item to add to your list comes to mind: Show a function that tries to return an automatic value by reference, and ask the candidate to spot the error and explain its consequences. Most compilers will warn upon detecting this situation.

If pushing on seven out of ten, presumably you won't involve function templates. If so, explore the difficulty of template arguments capturing reference types, and mistakenly forming reference-to-reference types in declarations. C++0x will address this problem, but I doubt a seven-out-of-ten candidate will be familiar with the problem and its resolution.

You'll learn far more by poking at familiarity with pointers. As Joel Spolsky likes to point out, pointers are a sharp partitioning point for programmer aptitude.

seh
+5  A: 

Well, there are some questions. Let him explain:

1) the difference between reference and const reference;

2) the difference between reference and pointer;

3) advantages and disadvantages of passing parameters by values / by references;

4) issues, when keeping references as class member variables;

5) issues, when returning references from functions;

It is easy to generate a code to test an understanding of each issue, listed above...

SadSido
+1 Yes, knowing a loose definition of reference is one thing, being able to distinguish it from other superficially similar concepts is another.
Marcin
A: 

A possible shibboleth with references is to ask the candidate about the "Most important const":

http://herbsutter.wordpress.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

Not by name, necessarily, but by code examples. Does the candidate know that you can extend the lifetime of a temporary object by binding it to a const reference (but not to a non-const reference), and how this interacts with polymorphism?

The reason I say not by name necessarily is that C++ guru-hood really isn't that big a world. If someone is serious about their competence in C++, then they will know who Herb Sutter and Andrei Alexandrescu are. Quite possibly they will have encountered that exact blog post before, and/or will have looked at the implementation of ScopeGuard. It may be that just by dropping names like "ScopeGuard" or "most important const", you can quickly get them to indicate how well they understand it.

But I'm assuming that 7/10 means, as well as other things, "I take an active interest in the cutting edge of the language, and am somewhat familiar with what goes on". It's perfectly possible that 7/10 means, "I'm perfectly able to write C++ apps, but I have no time for those academics and their trickery". 7/10 just means the candidate is aware that some people are considerably better than he/she is. It doesn't tell you how good the candidate is claiming to be, and as long as he/she can write decent C++ code, I don't think it's fair to try to catch out an exaggeration.

Personally I've never interviewed any job candidate. But given this brief, I'd probably start with whether the candidate can implement a simple function that accepts and returns a reference (an operator+=, say, or if you want to avoid operator overloading then maybe a function that appends ".txt" to the end of a string, and returns the input by reference to allow call chaining). If they do this without thinking (other than to work out if it's a trick question), then they might be a Real Princess, and you can move straight on to the "are you an expert, that other programmers in the company can go to for advice?" questions, where they'll probably get some but not others. Otherwise, move to a general discussion of what references are, and what they can be used for, to let them show you what they can do.

Steve Jessop
Hmm, as someone who knows about lifetime extending by binding to const references, and the fact that they call the right destructor even if it's non-virtual, I'd never heard the term "most important const", and think it's pretty stupid....
DrPizza
No problem, I don't think not knowing that should count against you. It's not a "term of art", just what Alexandrescu said in some talk, and Sutter repeated on a blog. I just mean that if I show you a code sample and you say, "ah, yes, Alexandrescu's 'most important const', this is how ScopeGuard calls the right destructor non-virtually!", then you've pretty much proved you're aware of that trick. Otherwise we go through Sutter's three questions in turn, and you get them all right, and prove it that way.
Steve Jessop
A: 

Were you expecting this question because you read StackOverflow?

Phil Whittington
A: 

You have to ask FizzBuzz Questions. It is simple but also powerful:P

qba
A: 

Ask them "When do you use a reference". If they say anything but to pass in a value to a function to avoid a copy, don't hire them. Fire anyone who uses it for anything but that.

Charles Eli Cheese