What is your favorite C++ interview question? It may be any question, for example:
algorithms, multithreding, gamedev area, low-level system programming, strings...
What is your favorite C++ interview question? It may be any question, for example:
algorithms, multithreding, gamedev area, low-level system programming, strings...
These pages has over 40 examples of C++ interview questions:
This article: The Guerrilla Guide to Interviewing has some quite nice thoughts about interviewing and asking C-questions. Personally, I don't see algorithms as a pure C/C++ question.
Multithreading - questions about threads are evil, really many programmers won't be able to anwser them correctly (they seem to be quite like Joel's pointers questions).
Gamedev - if it's your industry, ask them, but it's not some kind of skill a programmer should have when applying for a job.
And questions about strings are really about pointers, so reaction of the person you're talking to as soon as he hear that question should tell you much about him/her.
My personal favorite question is always related to a project someone worked on. Listen carefully what he's talking about and try to figure and ask what he could have done better. If it will be about pointers, ask. Really, many people feel much better when they talk about their recent work then when they have to anwser some theoretical questions.
I was asked this one in a c++ course final exam:
Say you have a class Parent
with a virtual method a
and a class Child
that implements a
.
You also have a bunch of functions:
void f1(Parent p) { p.a(); }
void f2(Parent* p) { p->a(); }
void f3(Parent& p) { p.a(); }
When called with a Child
instance, which method will be invoked for f1
, f2
and f3
?
Write a simple custom memory allocator for std::vector
and for std::list
. This checks applicant's:
I should add here, in response to a comment, that the point is not to see them write an error-free, compilable implementation on a white board. The point is to check that they are comfortable with the syntax and style of the language, comfortable with some advanced but important concepts, and see how well they think through a problem. Most people, as Drew notes, will never have had a need to write a custom allocator. That's good! That means it's a problem whose solution they won't likely be able to regurgitate from experience. I want to actually see them solve a problem before my eyes.
One of my favorites is to ask the applicant to write code to shuffle a deck of cards. This is one that sounds easy, but is difficult to get right. See Atwood's The Danger of Naivete and see also How We Learned to Cheat at Online Poker: A Study in Software Security.
What is the difference between pointer and reference? Please describe the two main differences.
I usually try to avoid asking candidates language specific questions: intelligence and adaptability are more important to me. I think any smart person should be able to learn C++ fairly quickly, and I don't think knowing language specific trivia (e.g. how to write a custom allocator for a stl type) is a good indication of a candidates potential.
If speaking of a just one, what is a smart pointer? With a detailed description and typical usage guidelines. Simple and reveals C++ background quickly.
This is the one that I was given when I interviewed:
Given a singly-linked list that contains a character, you have to print the linked list in reverse order. You are given the head pointer, how would you go about solving the problem.
For professional games programming: I like to ask a mix of high-level questions (OOP, class design, etc) and low-level questions (write a function that performs endian change, write an Align() function, etc). Some people have the low-level skills but choke on the high-level (so these people are good are optimizing and finding obscure bugs) and then other people know a lot of OOP, UML, etc (so they are good for architecting modules)., but they don't know what happens 'under the hood'.
Not specifically C++, but it was about Pointers.
Can a pointer point to itself ?
Answer to this question has earned me my first job.
When phone-screening candidates, I sometimes ask:
When does a class need a virtual destructor?
This should be an extremely easy question for someone who has done any amount of C++ class design, but is really hard for people who list C++ on their resume, but don't have any actual experience with it.
I like the simple things that shows basic understanding of the language:
An old boss of mine used to ask candidates to write a function to swap the two bytes of a 16-bit value. 75% of the candidates had no idea how to do it.
If a candidate couldn't do it, the follow-up question was "What is 13 in hexadecimal?" Most of the candidates who couldn't do that first exercise also had no idea how to answer this. (The boss told one candidate that she should give her Ph.D. back to her university.)
These very-simple questions got rid of lots of candidates. It made me sad.
One of my favourite question is:
What is the answer of F + 1, given F and 1 are hexadecimal numbers.
This can be extended to n-base numbers.
[EDIT: I think what I've commented for Robert, should've put into original]
It tells me that the person I'm interviewing can understand hexadecimal, or can think about number system outside decimal. Yes, it is fairly basic, yet I've found candidate who cannot answer this question. Or struggled answering it.