Thursday, September 11, 2014

The factorial exercise

When I'm conducting job interviews, one of my favorite recurring question is: "Can you write a factorial function?".

Even during the first interview, I think it's relevant and important to see your candidate writing code in live. Note that this is a very open question, e.g.:
  • this can be written in any language
  • candidate may supply both iterative or recursive version
Now that I've asked this question many, many times, I decided to share some of the collected answers:








Asking such a simple question may look quite easy, even more for senior candidates. But writing live code, when you're not ready, is never that easy, considering stress and time factors. And as you can see it's amazing to notice so many different versions: useless intermediate variables, useless conditions, brain-cracking complexity... Nevertheless, this basic quizz always ends up with deeper aspects and more interesting questions, such as:
  • overflows: what happen exactly if you compute factorial(2 << 32) ?)
  • memoization: what's that and when could you consider it?
  • unit testing: what and how do you test?
  • how to make your code run in parallel?
  • maths and logic: what are the last 10 digits of factorial(123456789) ?
It's obviously not the only one question, and it's probably not the most important one. But I'm pretty sure you'd also expect a minimal working version from a candidate with a young academic background.