Question presented to candidate: "What is recursion, and what is a base case? Write a small recursive function in Python β say factorial or the sum of a list β point to the base case, and tell me what happens if it is missing."
What a strong answer should cover:
- Recursion: a function that solves a problem by calling itself on a smaller instance of the same problem.
- The base case: the smallest input, answered directly without another call; the recursive case: reduces the problem and calls itself.
- Every call must make progress toward the base case; a missing or unreachable base case recurses until the stack overflows.
- The call stack: calls wind down to the base case, then results unwind back up; stack space is the maximum depth, time is the number of calls times the work per call.
- Python specifics: a missing base case ends in
RecursionError(default limit 1000), recursing on slices copies data, andfunctools.lru_cachememoises a recursion in one line.
Code / implementation expected: Yes β a short recursive Python function (factorial, sum, palindrome) with its base case, and its cost.