GATE 2005
203 questions
MCQ 2031-mark 203
Questions 111–115 of 203
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)
and
Which of the following statements is TRUE?
Answer: (C)
First, find this value: .
The given function is .
So holds for any real with . This means Case 1 of the Master theorem is satisfied.
So .
Answer: (C)
Which one of the following is FALSE?
Answer: (C)
Here , which is the same as . This is case 2, so .
Now check each option:
- (A) . True.
- (B) . True.
- (C) would need to grow at least as fast as . It does not, since . False.
- (D) . True.
So the false statement is (C).
Answer: (C)
What is the maximum profit earned?
Answer: (A)
Order by profit: T3 (30, d=5), T9 (25, d=3), T7 (23, d=2), T2 (20, d=2), T4 (18, d=3), T5 (18, d=4), T8 (16, d=7), T1 (15, d=7), T6 (10, d=5).
T3 goes to slot 5. T9 goes to slot 3. T7 goes to slot 2. T2 wants slot 2, which is taken, so it goes to slot 1.
T4 (d=3): slots 1, 2, 3 are all full, so it is skipped.
T5 goes to slot 4. T8 goes to slot 7. T1 wants slot 7, which is taken, so it goes to slot 6.
T6 (d=5): slots 1 to 5 are all full, so it is skipped.
Schedule: slot 1 T2, slot 2 T7, slot 3 T9, slot 4 T5, slot 5 T3, slot 6 T1, slot 7 T8.
Profit .
Answer: (A)