GATE CSE 2026 Session 1 Q61: Smallest n for which foo(n) = 5 with foo(bar(n))
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
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.
Try it: numerical answer: type the value
Show answer and explanation
Answer: 65536 (official GATE 2026 final answer key)
Concept used: bar(n) , so foo counts iterated logarithms.
First step: Work backwards: foo(n) = 2 first at n = 2, so foo(n) = 3 first when bar(n) = 2, i.e. n = 4; continue the pattern.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2026 Session 1 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15