Skip to content
Related Articles

Related Articles

GATE | GATE-CS-2005 | Question 81

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021
View Discussion
Improve Article
Save Article

Consider the following C-function:




double foo (int n)
{
    int i;
    double sum;
    if (n = = 0) return 1.0;
    else
    {
        sum = 0.0;
        for (i = 0; i < n; i++)
            sum += foo (i);
        return sum;
    }
}


The space complexity of the above function is:
(A) O(1)
(B) O(n)
(C) O(n!)
(D) O(nn)


Answer: (B)

Explanation: Note that the function foo() is recursive. Space complexity is O(n) as there can be at most O(n) active functions (function call frames) at a time.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!