The standard interview question is "write a function to solve this problem which has an elegant recursive solution", not "think of a recursive function". But if you do want to think of a recursive function, try factorial.
int fctl(int n){ return ( (n = 1) ? n : n * fctl(n)); }
(Don't use that code in an interview. It's questionable style for C, and the ternary operator is scary to people.)I was asked to write a fibonacci function once, and I actually said, "you can do this recursively but the performance is awful" and wrote an iterative solution.
The point of an interview is for you to show off. If you start going on and on and on and on about unnecessary details the interviewer might stop you, but if you launch into a talk about the space and time properties of recursion vs. iteration and show some judgment for how to use recursion and how not to use recursion, then I know how knowledgeable you are.
int fctl(int n){ return ( (n == 1) ? n : n * fctl(n)); }If somebody makes a mistake I tell them and give them hints until they fix it. Well... it depends on the magnitude of the mistake I suppose. If they're not even in the right ballpark I keep my mouth shut.
Hence my mention above of how it feels like a trick question. I really don't know whether the interviewer wants the autistic-but-informative answer or the quick of-course-I-know answer. Trying to guess what sort of answer you are seeking induces mild panic in me: I don't know you, but if your response in an interview was 'what a crap algorithm' I'd be thinking 'this guy is a bit Machiavellian...'.
I mean, I can think of a much faster way (than this) to get a given term in the fibonacci series using a loop, but this one recurses, which was what was requested - I had to quickly choose a problem domain in which to demonstrate it. Towers of Hanoi would have been more impressive, but I don't know if I could just rattle that off correctly, verbally. See what I mean?