Difficulty: Easy
Correct Answer: Stack
Explanation:
Introduction / Context:
Recursion is a fundamental programming technique where a function calls itself directly or indirectly. Even though programmers write recursive code in a high level language, the underlying implementation at runtime must keep track of return addresses, parameters, and local variables for each active call. This question tests whether you know which classic data structure is used by compilers and runtime systems to manage this information and support recursion correctly.
Given Data / Assumptions:
Concept / Approach:
The key concept is that recursive calls follow a last in, first out pattern: the most recent call must finish and return before previous calls can resume. The ideal data structure that naturally matches last in, first out behaviour is the stack. At runtime, each function call creates an activation record (also called a stack frame) that is pushed onto the call stack. When the function returns, its activation record is popped from the stack. Therefore, the correct answer must be the stack data structure.
Step-by-Step Solution:
Step 1: Think about how recursion works conceptually: function f calls itself, which calls itself again, and so on.
Step 2: Each new call needs its own storage for parameters, local variables, and the return address back to the caller.
Step 3: The order of returns is reversed compared to calls. The last call made must complete first before control returns to the previous call.
Step 4: Recognise that this last in, first out pattern is exactly the behaviour of a stack.
Step 5: Recall that most language runtimes maintain a call stack (or execution stack) where activation records are pushed on call and popped on return.
Step 6: Therefore, the data structure used to implement recursion is the stack, matching option (a).
Verification / Alternative check:
To verify, imagine tracing a simple recursive factorial function on paper. You would write down each call with its parameter value as you go deeper, and then erase or tick them off as you unwind. The order in which you remove entries is the reverse of the order in which you added them, which is classic stack behaviour. In contrast, a queue would return calls in first in, first out order, which does not match function call semantics. This mental simulation confirms that the stack is the correct data structure.
Why Other Options Are Wrong:
Option (b), a queue, is wrong because it is first in, first out and would not allow the most recent call to return first. Option (c), a linked list, is a general structure that can be used to implement a stack, but by itself it does not capture the required access discipline; the conceptual answer must be stack. Option (d), a binary search tree, is used for ordered searching and does not match the behaviour of function calls and returns.
Common Pitfalls:
A common pitfall is to answer “linked list” because many stacks are implemented using linked lists internally. However, exam questions usually ask for the abstract data type, which is stack. Another mistake is to confuse the call stack with the heap. The stack is for managing function calls and automatic storage, while the heap is used for dynamic memory allocation such as new or malloc. Keeping this distinction clear helps in both interviews and practical debugging when dealing with stack overflow and recursion depth issues.
Final Answer:
Stack.
Discussion & Comments