GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 81a

Space used by repeated recursive calls

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2005 paper

GATE 2005, Q81a 2 marks · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 81a, GATE 2005: double foo(int n){int i;double sum;if(n==0)return 1.0;sum=0.0;for(i=0;i<n;i++)sum+=foo(i);return sum;} Printed 81(a): maximum auxiliary space of foo(n)? A O(1); B O(n); C O(n!); D O(n^n). Seek intended tight order, not merely any loose upper bound.
Question 81a, GATE 2005: double foo(int n){int i;double sum;if(n==0)return 1.0;sum=0.0;for(i=0;i<n;i++)sum+=foo(i);return sum;} Printed 81(a): maximum auxiliary space of foo(n)? A O(1); B O(n); C O(n!); D O(n^n). Seek intended tight order, not merely any loose upper bound.
Question 81a, GATE 2005: double foo(int n){int i;double sum;if(n==0)return 1.0;sum=0.0;for(i=0;i<n;i++)sum+=foo(i);return sum;} Printed 81(a): maximum auxiliary space of foo(n)? A O(1); B O(n); C O(n!); D O(n^n). Seek intended tight order, not merely any loose upper bound.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Every activation stores only a constant number of locals, but calls are nested. A path foo(n)→foo(n−1)→…→foo(0) reaches n+1 simultaneous activations. The loop calls execute sequentially, so the exponentially many calls are not all live at once. Maximum stack space is Θ(n), option B.

Count simultaneously live activations, not total recursive calls. During its last recursive branch, foo(n) may be suspended while foo(n−1), foo(n−2),…, foo(0) are active, giving linear depth. Each frame contains only a constant number of scalar locals. Separate loop iterations return before the next call, so their storage is reused. This distinguishes linear stack space from exponential running time. Larger Big-O choices are also loose bounds; the intended answer describes the tight growth.

Original paper, page 11