Skip to content
Official PYQ · 1999

GATE CS 1999 Q2.24 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Consider the following CC function definition

int Trial (int a, int b, int c)
{
    if ((a>=b) && (c<b)) return b;
    else if (a>=b) return Trial(a, c, b);
    else return Trial(b, a, c);
}

The functional Trial:

Choose one option
View worked solution

Answer: D

Let us analyze the behavior of the function Trial(a, b, c):

  • Base Case: The condition (a >= b) && (c < b) represents the ordering a≥b>ca \ge b > c. In this case, bb is the middle (median) element, and the function correctly returns bb.
  • Recursive Step 1: If a<ba < b, the function calls Trial(b, a, c) to swap aa and bb so that the first argument is greater than or equal to the second.
  • Recursive Step 2: If a≥ba \ge b but c≥bc \ge b, the function calls Trial(a, c, b) to swap bb and cc.

However, the function fails to terminate for certain inputs where b=cb = c. For example, consider Trial(2, 1, 1):

  1. Since a≥ba \ge b (2≥12 \ge 1) is true but c<bc < b (1<11 < 1) is false, the base condition is not met.
  2. The else if (a >= b) condition (2≥12 \ge 1) is true, so it calls Trial(a, c, b), which is Trial(2, 1, 1).
  3. This results in an infinite recursion (stack overflow).

Since the function does not terminate for all inputs, it does not correctly find the middle number. Thus, the correct option is D.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions