What is your favorite C++ interview question? It may be any question, for example:
algorithms, multithreding, gamedev area, low-level system programming, strings...
FizzBuzz [1] :)
I love these phone-screen questions from Steve Yegge [2].
[1] http://www.codinghorror.com/blog/archives/000781.htmlI 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.
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.
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.
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
?
Not specifically C++, but it was about Pointers.
Can a pointer point to itself ?
Answer to this question has earned me my first job.
This article: The Guerrilla Guide to Interviewing [1] 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.
[1] http://www.joelonsoftware.com/articles/fog0000000073.htmlAn 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.
swap = lambda x: type(x).from_bytes(reversed(x.to_bytes(length=2, byteorder="big")), byteorder="big")
- Janus Troelsen
What is the difference between pointer and reference? Please describe the two main differences.
These pages has over 40 examples of C++ interview questions:
http://www.techinterviews.com/?p=238
http://www.techinterviews.com/?p=340
From the similar beginner-specific question C++ areas you look for during interview [1]...
I've found myself dividing C++ interview questions into certain categories. Of these, many interviewers only touch on fundamentals and details. Junior candidates can only be expected to know some of this, but you definitely want to see that they've attacked it with some enthusiasm.
It's hard to find people who've made a systematic, broad investigation of the how-tos of C++ programming at the low-level library level (where the most ingenuity is generally required), and equally hard to find people who know what to do in terms of large-scale design and architecture trade-offs. Very roughly - just to illustrate these factors - I've compiled some examples:
f(X&)
and template <class A, class B> struct T
such that f
can accept any object of type T and handle them via virtual dispatch#ifdef
sI like the simple things that shows basic understanding of the language:
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.
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 [1] and see also How We Learned to Cheat at Online Poker: A Study in Software Security [2].
[1] http://www.codinghorror.com/blog/archives/001015.htmlI wrote a blog post [1] on this subject, before I discovered this question here. I argue that the specific question is not the important thing. It's a small piece of the overall technical interview.
[1] http://andrewcraze.com/blog/index.php/2009/01/what-is-your-favorite-technical-interview-question/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.
Write a program which take a integer value (n) from user and calculate the sum of 1 to n. The time complexity of program should be 1.
I did reply there [1].
[1] http://stackoverflow.com/questions/347793/c-areas-you-look-for-during-interview/347845#347845For 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'.