Asymptotic Notation · Algorithms
40 questions · asked in 21 of 27 years (2000–2026)
MCQ 37 · MSQ 2 · NAT 11-mark 27 · 2-mark 13
Questions 31–35 of 40
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:Answer: (B)
Each call of foo uses a fixed amount of stack for , and the return address. The code stores no array or table of earlier results, so the space needed is set by the number of calls alive at the same time, that is, the recursion depth.
The loop makes one call at a time, and each call returns before the next starts, so its space is reused. The depth is largest in the last pass of the loop: . That is calls at once.
In the figure, the loop runs for , so the root's last child is , and the labels along that last branch are .
Space . The running time is exponential, but time does not add up in space. O(1) is wrong because the depth grows with ; O(n!) and are far more than the stack ever holds.
Answer: (B)
Answer: (D)
Write the relation as an 0/1 matrix , with if is related to . Warshall's algorithm gives the transitive closure with three nested loops:
for = 1 to , for = 1 to , for = 1 to :
Each loop runs times and the work inside is constant, so the time is .
Faster methods use fast matrix multiplication (about ), and finding the closure is as hard as multiplying two Boolean matrices. No algorithm is known that does either in , or time. So of the given options, only is a bound known to be reachable.
Answer: (D)
Which one of the following statements is FALSE?
Answer: (D)
(A) . True.
(B) and , and is transitive, so . True.
(C) If , then , which contradicts . So . True.
(D) From , multiply both sides by : . So , and the statement "" is false.
Answer: (D)
int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1));
} Answer: (D)
int recursive (int n) {
if(n == 1) // constant time, say 'a'
return (1); // constant time, say 'a'
else
// takes T(n-1) + T(n-1) time
return (recursive (n-1) + recursive (n-1));
}
The recurrence from the code is . Here is the constant cost of the part of the function that is not recursive (the comparison and the addition).
Solve it by back substitution:
The base case is , so put , that is :
Answer: (D)
counter = 0;
for (i = 1; i <= n; i++)
{
if (A[i] == 1)
counter++;
else {
f(counter);
counter = 0;
}
} The complexity of this program fragment isAnswer: (C)
When , is the length of the run of 1s just before position , and then it is reset to 0. So the runs passed to never overlap, and their lengths add up to at most the number of 1s, which is at most . All calls to together cost .
So the total is . The loop always makes passes, so the cost is for every input. Hence the complexity is .
(A) and (B) claim more than linear growth (, ), which is false since the total is . (D) needs strictly slower than linear, but the cost is at least .
The reset is what keeps it linear. Without it, an array like 1,0,1,0,... would call with counter values up to , giving .
Answer: (C)
Keep practising