Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Data Structures & Algorithms basics — a prerequisite for any developer role.

Answer

Recursion is a programming technique where a function calls itself with a smaller or simpler version of the problem until it reaches a base case (termination condition). Every recursive function needs: (1) Base case: condition that stops recursion (no more self-calls); (2) Recursive case: function calls itself with a modified argument that moves toward the base case. Example — factorial: f(n) = 1 if n==0; n * f(n-1) otherwise. The call stack stores each recursive call's state. Space: O(n) stack depth for linear recursion. Tail recursion: the recursive call is the last operation — compilers can optimize to use O(1) space (tail call optimization). Problems well-suited to recursion: tree traversals, divide-and-conquer (merge sort, quicksort), dynamic programming, backtracking, fractals. Common pitfalls: (1) No base case → infinite recursion → stack overflow; (2) Base case never reached; (3) Deep recursion → stack overflow (use iteration or increase stack size). Every recursive solution has an iterative equivalent (using explicit stack), but recursion is often more elegant and readable. Think recursively: define the solution in terms of smaller subproblems.

Pro Tip

This topic has Data Structures & Algorithms-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.