Skip to interview prep
Apexed

6. Recursion vs iteration: when do you prefer each?

EasyRecursion & backtracking

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

Recursion mirrors problems defined in terms of smaller copies (trees, divide and conquer, backtracking) and is often clearer. Every call uses stack space, so very deep recursion can overflow; iteration uses constant stack space and avoids call overhead.

Any recursion can be converted to iteration with an explicit stack. Run the program to compare a recursive and an iterative factorial.

Example program

Recursion vs iteration: when do you prefer each — Data structures & algorithms interview question — Apexed