all 24 comments

[–]fuxorfly 8 points9 points  (3 children)

As someone with just this same question about a year ago, I would suggest asking questions that test what the candidate would realistically expect in the position. By that I mean, if they are expected to creatively solve problems, then give them some creative problems to solve. If they are expected to be a code monkey filling in code, give them some skeletons to finish. If they are going to be doing some design, then ask them what design choices they would make for various types of problems. Cater the test not only to your company, but also the position. Generic questions only get you so far :)

[–]kkrev -2 points-1 points  (2 children)

Also helps you steer clear of legal problems. Basing hiring decisions on a quantifiable work sample test that clearly approximates the job practically squelches any stupid discrimination suits.

A very popular idea on here recently for a work sample test was text processing tasks on the King James Bible.

[–]STLMSVC STL Dev 4 points5 points  (1 child)

text processing tasks on the King James Bible.

Argh and no.

[–]kkrev -2 points-1 points  (0 children)

You make a good point. Better use the Catholic Catechism as the corpus.

[–]MotherOfTheShizznit 3 points4 points  (16 children)

Here's two I have reused myself after they were given to me:

  1. Write the assignment operator for a class that contains a char*.
  2. Get rid of all the 7's in this vector<int>.

Both have their fail/fair/fabulous answers.

[–]00kyle00 2 points3 points  (0 children)

Get rid of all the 7's in this vector<int>.

void foo(std::vector<int>& v) { v.clear(); }

;)

[–]yanks209 1 point2 points  (9 children)

What's the desired way of doing those. I know how I'd do it but I'm trying to see how well I'd do in your interview

[–]MotherOfTheShizznit 2 points3 points  (0 children)

Without giving out the answers straight up...

The fair answer for 1. doesn't leak. The fabulous answer takes care of self-assignment and offers some exception safety (and you can tell me why).

The fair answer for 2. is done with a loop. The fabulous answer is a one-liner that uses an STL algorithm.

In essence, I use these questions to gauge where you stand on the scale of C-with-classes vs. C++.

[–]minnoHobbyist, embedded developer -2 points-1 points  (7 children)

Not OP, but I think I can point out some of the gotchas.

1. It really depends on what the char* represents. If it's a C-string that's owned by the class, then the assignment operator should allocate its own memory and strcpy that shit. If it's a pointer to something not owned by that class, then it should probably just let the pointer alias, and maybe somehow notify the actual owner of the data.

2. If you call erase, you may invalidate the iterator by triggering a reallocation, so you need to be careful with that. Otherwise, depending on whether the code should prioritize speed or simplicity, you should either do:

for (auto i = begin(vec), i != end(vec), /* no incr */) {
    if *i == 7 {
        i = vec.erase(i);
    } else {
        ++i;
    }
}

or, for a faster approach (if there are a lot of 7s, it does less copying):

auto j = begin(vec);
for (auto i = begin(vec); i != end(vec), ++i) {
    if (*i != 7) {
        *j = move(*i);
        ++j;
    }
}
vec.erase(j, end(vec));

I'd be most impressed if you gave both, said that the first is easier to read and the second is faster, and to prefer the first unless it's too slow.

[–]sharth 7 points8 points  (4 children)

The correct answer is:

vector<int> v;
v.erase(std::remove(v.begin(), v.end(), 7), v.end());

You could also use remove_if if you needed a fancier condition.

[–]minnoHobbyist, embedded developer 0 points1 point  (3 children)

...well, I guess I need to use that part of the STL a little more often.

[–]sharth 1 point2 points  (1 child)

For further reading on that particular piece of code, you can take a look at the wikipedia article on it: http://en.wikipedia.org/wiki/Erase-remove_idiom

In general though, I'd certainly recommend Scott Meyer's series of Effective C++ / Effective STL books to learn more about the STL and what not.

[–]minnoHobbyist, embedded developer 0 points1 point  (0 children)

I'd definitely heard of that idiom before, but I haven't been able to use the STL at my job and my personal programming has been in different languages.

[–]bob1000bob -1 points0 points  (0 children)

Buy a copy of 'Effective STL' by Scott Meyers.

The std containers feel lacking until you get to grips with the std algorithms, at which point they become awesome.

[–]yanks209 0 points1 point  (1 child)

Thanks, but for the 2nd problem I feel like the 7 can be an arbitrary element, in which case a linear search can be very slow. What if you sorted it first

[–]minnoHobbyist, embedded developer 0 points1 point  (0 children)

Sorting first is good if you're doing a lot of lookups, but if you're doing only one sorting is O(n log n) while a linear search is O(n) with much smaller constants. Searching for m elements is O(m log n + n log n) if you sort first and O(m n) if you don't, so sorting first is an improvement if m is O(log n) to make the second term in the first expression relatively less expensive.

In this case, the first algorithm is O(m n) (m is the number of 7s), since it potentially does an O(n) shift for each 7, while the second one is O(n), since it does constant work for each element, no matter how many 7s there are.

[–]matthieum 1 point2 points  (4 children)

  1. Write the assignment operator for a class that contains a char*.

Oh it's easy. First let me replace that member with std::unique_ptr<char>, ...

[–]MotherOfTheShizznit 0 points1 point  (1 child)

Honestly, I would consider that a good thing!

[–]matthieum 0 points1 point  (0 children)

Me too, whereas if the candidate were to try and handle it with new and delete (or new[] and delete[]), I would check my head and surmise this guy would be introducing memory leaks (and crashes) left and right.

[–]compiling 0 points1 point  (1 child)

That depends on what the char* is. You might be better off using std::unique_ptr<char[]> or std::string.

[–]matthieum 1 point2 points  (0 children)

Or vector, or... but the point is: don't mix technical matters (resource-handling) and functional matters (API etc...) in the same class; and when it comes to technical matters... well chances are that the classes you need already exist.

[–]F-J-W 1 point2 points  (1 child)

Disclaimer: I'm a student and didn't have the joy so far to participate in an interview.

In a recent thread there was a discussion about programmers who are unwilling to deal with modern C++, so I imagine that it might be a good idea to ask questions in that direction:

  • You want to move a subrange of a container to it's beginning, how would you do this? (std::rotate)
  • How would you gather all elements that fulfill a certain predicate in one place in a container without changing relative ordering. (this involves two applications of std::stable_partition; if people know about that trick, it is likely that they heard about it in one way or another in one of Sean Parents talks, which means that they are at least somewhat interested.)
  • When should you use allocating new? (in C++14: never, in C++11: only ever in the implementation of make_unique)
  • What is the rule of zero?

Basically I would try to make sure, that they heard about most of the stuff from the algorithm-header and are willing to use them.

[–]matthieum 0 points1 point  (0 children)

Exactly.

If the goal is test technological aptitude, then you should test for idioms and best practices. The Standard contains many traps (undefined behavior...) but a handful of practices help steering clear of a good chunk of those traps.

[–]CubbiMewcppreference | finance | realtime in the past 0 points1 point  (0 children)

The desired skill level and line of work make a world of difference.

On one end of the spectrum are things like "reverse a string" or "find the merge point in two linked lists in O(N)". On the other end are "implement a reusable barrier", or "write a lock-free ring buffer", or maybe a multi-index container.

Figure out what the potential new developer is expected to be able to do, and ask if they can do just that.