GATE CS 2026 Set 1 | Question 61

Programming & Data Structures › Functions · NAT · 2 marks · Answer: 65536

  1. Question 61Programming & Data Structuresnat2 marks
    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.

    Answer: 65536

    Solution

    Answer: 65536 bar(n) , and foo counts how many times you can apply before reaching 1. That is an iterated logarithm, plus 1. Build the smallest for each value, working backwards:
    foo(n) | Need bar(n) to be             | Smallest n
    -------+-------------------------------+---------------
    1      | –                             | 1
    2      | 1                             | 2
    3      | ≥ 2 (smallest giving foo = 2) | 4
    4      | ≥ 4                           | 16
    5      | ≥ 16                          | 2^(16) = 65536
    Check: . That is 4 calls to bar, so foo returns . Note that bar(1) = 0 would make foo(0) recurse forever, but the chain above never calls foo(0).

More GATE PYQ on Functions

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2026 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →