Pointer Hurdles

At Joel On Software there’s a great article about how to interview for programming jobs, which includes getting the candidate to write some code, on paper, in the interview.

For programming questions, I ask candidates to write a small function in C. Here are some typical problems I would ask:

  1. Reverse a string in place
  2. Reverse a linked list
  3. Count all the bits that are on in a byte
  4. Binary search
  5. Find the longest run in a string
  6. atoi
  7. itoa (great, because they have to use a stack or strrev)

And here are some of the things he suggests looking out for:

  • Is their function fast? Look at how many times they call strlen. I’ve seen O(n^2) algorithms for strrev when it should be O(n), because they are calling strlen again and again in a loop.
  • Do they use pointer arithmetic? This is a good sign. Many “C programmers” just don’t know how to make pointer arithmetic work. Now, ordinarily, I wouldn’t reject a candidate just because he lacked a particular skill. However, I’ve discovered that understanding pointers in C is not a skill, it’s an aptitude. In Freshman year CompSci, there are always about 200 kids at the beginning of the semester, all of whom wrote complex adventure games in BASIC for their Atari 800s when they were 4 years old. They are having a good ol’; time learning Pascal in college, until one day their professor introduces pointers, and suddenly, they don’t get it. They just don’t understand anything any more. 90% of the class goes off and becomes PoliSci majors, then they tell their friends that there weren’t enough good looking members of the appropriate sex in their CompSci classes, that’s why they switched. For some reason most people seem to be born without the part of the brain that understands pointers. This is an aptitude thing, not a skill thing – it requires a complex form of doubly-indirected thinking that some people just can’t do.

I totally agree with this: pointers and pointer arithmetic represent a specific level of skill that allow you to say confidently either “this person has no hope as a programmer” or “this person gets it“.

However, I think you can extend this idea further. I’ve found that there’s a second hurdle, beyond which lie the really good programmers. That hurdle is function pointers.

There’s something about function pointers that many otherwise-good programmers just don’t get. Ask them to define and initialise an array of function pointers to factory methods, and index into the array to instantiate an instance of the appropriate type, and they’ll be totally lost. They just won’t get it. They’ll write reams of bug-free procedural code when a simple function pointer would have saved them hours, and made their code vastly more maintainable and extensible.

Leave a Reply