Question
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.
Official answer and solution
- Official answer
- 65536
- From
- Official GATE 2026 answer key
Solution
Observe that bar(n) computes , and work backwards from foo(1) = 1 to foo(n) = 5 by choosing the smallest at each step.
- The function
bar(n)counts how many timesnis divided by2until it reaches1, so for all . Notice that the smallest positive integer for which is . - Working backwards for
foo(n):- .
Final answer
65536
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2026 Computer Science and Information Technology - Session 1 (CS1)
- Question
- 61 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.