The factorial exercise

For years, one of my favorite recurring question when I was conducting job interviews used to be: “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, there is obviously not only one accepted solution:

  • this can be written in any language
  • candidate may supply both iterative or recursive version

Asking for a simple question that is supposed to take about 10 lines of code should allow any candidate to quickly write something, make him/her feel confident. Now that I’ve asked this question many, many times, I decided to share some of the collected answers:

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Asking such a simple question may look quite easy and uninteresting, especially to 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
  • naming conventions
  • brain-cracking complexity…

Nevertheless, this very basic challenge always ends up with deeper aspects and more interesting questions, such as:

  • overflows: what happen if you want to compute factorial(2 << 32)?
  • memoization: what’s that and when could you consider it?
  • unit testing: what and how do you test?
  • concurrent programming: 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.

Conclusions

I eventually stopped asking this question to try different approaches. But if you want to try asking this question during your next interviews, I’d like to share some thoughts about it:

  • As weird as it may seem, I can say that only ~70% of candidates have been able to write a proper function. This means that 30% failed (over ~200 candidates). This can be a decent filter if you expect your candidate to write — at least — simple code. If you’re expecting your candidates to write much more complex code, then you should perhaps consider more complex coding problems to solve?
  • Since this will probably give you just a bunch of lines of code — definitely not enough to build an accurate opinion — it might be worth to dig deeper and deeper into the internals of the used programming language (typing system, optimizations…), kernel architecture, hardware architecture to evaluate fundamental CS skills. If you’re not looking for these skills, skip this step.
  • Keep in mind that this barely represents world-life problems. This means that seniors candidates can consider this a bit too academic, and junior candidates may have written this function many times at school before being interviewed. This question does not fall in the same category than “please code a bubble-sort on a whiteboard”, but you may prefer a wider challenge, more focused on your own business. After all, many real-world positions won’t require brilliant engineers write tail-recursive functions.

Electronic Music artist as Paranormind. Photography. Computer Science. Machine Learning & Data-related things.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store