IAE Academy

GATE 2026 DA Q.39: solution and answer

GATE 2026 · DAQ.39 · 2 marks · MCQ

A recursive function in Python is given.

def mystery(n):
    if n <= 0:
        return 1
    else:
        return mystery(n-1) + mystery(n-2)

Now, consider the following function call:

mystery(4)

Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.

Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery(4)?

  1. (A)

    5

  2. (B)

    9

  3. (C)

    15

  4. (D)

    17

See the question in the paper

Answer and solution

ANSWER

Option (C): mystery(4) makes 15 function calls in total, counting the initial call.

Official answer: CSource: Official answer key, GATE 2026 DA (IIT Guwahati)

  1. Let be the number of activations (including the first one) when mystery(n) is called.

  2. Base case: if n <= 0 the function returns 1 at once and makes no further calls, so for every . In particular .

  3. Recursive case: for n > 0 the call counts once and then makes mystery(n-1) and mystery(n-2), so .

  4. .

  5. .

  6. .

  7. .

  8. Check with the call tree: every call with has exactly two children, and every call with is a leaf. Let be the number of leaves. Then for and otherwise, giving , , , . A full binary tree with 8 leaves has internal calls, so the total is .

KEY IDEA

Count activations, not the returned value. The recurrence is with for . Note that n <= 0 makes both and negative base cases.

WATCH OUT FOR

Treating only n == 0 as a leaf, or forgetting that mystery(n-2) can reach , which is also a leaf. Another slip is answering 5 (the depth plus one) or 9 (the count for mystery(3)), or counting only the leaves (8) or only the internal calls (7).

From the GATE 2026 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions