GateAI
Recursion

GATE CSE 2015 Set 3 · Q39

MCQ2 marksMedium

Time

Consider the following recursive C function.void get(int n) { if (n<1) return; get(n-1); get(n-3); printf("%d", n); }If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 25

Recursion

Let T(n)T(n) denote the total number of invocations of the function get() when get(n) is executed.

From the function definition:

  • For n<1n < 1 (n≤0n \le 0), the function immediately returns after 1 invocation:

    T(n)=1for n≤0T(n) = 1 \quad \text{for } n \le 0
  • For n≥1n \ge 1, the function executes 1 call itself and invokes get(n-1) and get(n-3):

    T(n)=1+T(n−1)+T(n−3)T(n) = 1 + T(n-1) + T(n-3)

Now, we compute T(n)T(n) bottom-up for n=0,1,2,3,4,5,6n = 0, 1, 2, 3, 4, 5, 6:

  • T(≤0)=1  ⟹  T(0)=1,T(−1)=1,T(−2)=1T(\le 0) = 1 \implies T(0) = 1, T(-1) = 1, T(-2) = 1

  • T(1)=1+T(0)+T(−2)=1+1+1=3T(1) = 1 + T(0) + T(-2) = 1 + 1 + 1 = 3

  • T(2)=1+T(1)+T(−1)=1+3+1=5T(2) = 1 + T(1) + T(-1) = 1 + 3 + 1 = 5

  • T(3)=1+T(2)+T(0)=1+5+1=7T(3) = 1 + T(2) + T(0) = 1 + 5 + 1 = 7

  • T(4)=1+T(3)+T(1)=1+7+3=11T(4) = 1 + T(3) + T(1) = 1 + 7 + 3 = 11

  • T(5)=1+T(4)+T(2)=1+11+5=17T(5) = 1 + T(4) + T(2) = 1 + 11 + 5 = 17

  • T(6)=1+T(5)+T(3)=1+17+7=25T(6) = 1 + T(5) + T(3) = 1 + 17 + 7 = 25

Thus, get() is invoked 25 times, which corresponds to option (B).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Recursion previous year question is Q39 of the GATE CSE 2015 Set 3 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 10 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2015 Set 3.

How GATE tests Recursion

More from GATE CSE 2015 Set 3

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2015 Set 3 · Q39 · MCQ

Consider the following recursive C function. void get(int n) { if (n If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main() ?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.