Recursion vs Iteration - when to use which in interviews?
I always default to recursion because it's easier to think about. But I've been told iterative solutions are preferred. Why?
- Stack overflow risk with deep recursion
- Space complexity - recursion uses O(n) stack space
- Performance - function call overhead
But recursion is:
- Easier to code for trees/graphs
- More readable for backtracking
- Natural for divide and conquer
My approach: write recursion first, convert to iterative if the interviewer asks. Is this a good strategy?