GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 81b

Memoized recurrence storage

Algorithms · Dynamic Programming · Full GATE CSE 2005 paper

GATE 2005, Q81b 2 marks · MCQ
Report issue

Dynamic Programming

Question 81b, 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(b): memoize foo(i), 0≤i<n, as computed. Auxiliary space? A O(1); B O(n); C O(n²); D O(n!).
Question 81b, 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(b): memoize foo(i), 0≤i<n, as computed. Auxiliary space? A O(1); B O(n); C O(n²); D O(n!).
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Caching foo(i) for 0≤i<n requires n scalar values, hence Θ(n) space. Any recursive call stack is also at most O(n), so the total remains Θ(n), option B. Memoization reduces recomputation but does not require storing all call-tree nodes. This counts machine words, as in the question’s double-valued implementation.

Memoization allocates one result per argument value, a linear-size table, rather than one result per recursive-call occurrence. A recursive implementation can still have a linear stack, but adding two linear requirements remains linear. The table itself gives a linear lower bound in the stated storage scheme. Count constant-size machine words as specified by the double implementation; arbitrary-precision value bit lengths would be a different model.

Original paper, page 11