GateAI
Recursion

GATE CSE 2026 Set 1 · Q61

NAT2 marksMedium

Time

Consider the recursive functions represented by the following code segment:int bar(int n) { if (n == 1) return 0; else return 1 + bar(n/2); } int foo(int n) { if (n == 1) return 1; else return 1 + foo(bar(n)); }The smallest positive integer nn for which foo(n) returns 5 is ________. (answer in integer) Note: Ignore syntax errors (if any) in the function.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 65536

Recursion

Let us analyze the behavior of the two recursive functions:

  1. Function bar(n):

    • Base case: bar(1) = 0.

    • Recursive case: for integer division n / 2, bar(n) = 1 + bar(n / 2).

    • This computes ⌊log⁡2n⌋\lfloor \log_2 n \rfloor for any integer n≥1n \ge 1.

    • To get bar(n) = k, the smallest positive integer is n=2kn = 2^k.

  2. Function foo(n):

    • Base case: foo(1) = 1.

    • Recursive case: foo(n) = 1 + foo(bar(n)) for $n > 1$.

  3. Finding the smallest nn for which foo(n) = 5:

    • foo(n) = 1 + foo(bar(n)) = 5  ⟹  \impliesfoo(bar(n)) = 4.

    • Let n1=bar(n)n_1 = \text{bar}(n), so foo(n_1) = 4  ⟹  \impliesfoo(bar(n_1)) = 3.

    • Let n2=bar(n1)n_2 = \text{bar}(n_1), so foo(n_2) = 3  ⟹  \impliesfoo(bar(n_2)) = 2.

    • Let n3=bar(n2)n_3 = \text{bar}(n_2), so foo(n_3) = 2  ⟹  \impliesfoo(bar(n_3)) = 1.

    • For foo(bar(n_3)) = 1, the base case requires bar(n3)=1\text{bar}(n_3) = 1.

  4. Working backwards to minimize nn:

    • Smallest n3n_3 such that bar(n3)=1\text{bar}(n_3) = 1 is n3=21=2n_3 = 2^1 = 2.

    • Smallest n2n_2 such that bar(n2)=n3=2\text{bar}(n_2) = n_3 = 2 is n2=22=4n_2 = 2^2 = 4.

    • Smallest n1n_1 such that bar(n1)=n2=4\text{bar}(n_1) = n_2 = 4 is n1=24=16n_1 = 2^4 = 16.

    • Smallest nn such that bar(n)=n1=16\text{bar}(n) = n_1 = 16 is n=216=65536n = 2^{16} = 65536.

Thus, the smallest positive integer nn is 65536.

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 Q61 of the GATE CSE 2026 Set 1 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. 5 of them are 2-mark NAT questions like this one, and it is the only one in GATE CSE 2026 Set 1.

How GATE tests Recursion

More from GATE CSE 2026 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2026 Set 1 · Q61 · NAT

Consider the recursive functions represented by the following code segment: int bar(int n) { if (n == 1) return 0; else return 1 + bar(n/2); } int foo(int n) { if (n == 1) return 1; else return 1 + foo(bar(n)); } The smallest positive integer n for which foo(n) returns 5 is … (answer in integer) Note: Ignore syntax errors (if any) in the function.

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.