Machine-translated by Google. Questions are authored in English.
Top

Algorithm Analysis and Complexity Questions and Answers

1.

Given an integer array of size N, we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is

Exam Year(s)

GATE CS - 2024,

Answer: A

The worst-case time is both \(O(N)\) and \(\Omega(N)\), that is \(\Theta(N)\).

The algorithm makes a single pass, comparing each element with its neighbour, so it does at most N − 1 comparisons, which is O(N). In the worst case, a sorted array (or one whose first violation is at the very end), every adjacent pair must be checked before the algorithm can answer, so it needs N − 1 comparisons, which is \(\Omega(N)\).

Any correct algorithm must look at every element in the worst case, since an unread element could break the order. So the worst-case cost is linear from both sides, and the options denying either bound are wrong.

Please log in or register to report this question.

2.

Consider the following recurrence relation:

\[ T(n) = \begin{cases} \sqrt{n}\,T(\sqrt{n}) + n & \text{for } n \ge 1, \\ 1 & \text{for } n = 1. \end{cases} \]

Which one of the following options is CORRECT?

Exam Year(s)

GATE CS - 2024,

Answer: A

\(T(n) = \Theta(n \log \log n)\).

Divide the recurrence by n and set \(S(n) = T(n)/n\):

\[ \begin{aligned} \frac{T(n)}{n} &= \frac{T(\sqrt{n})}{\sqrt{n}} + 1 \\ S(n) &= S(\sqrt{n}) + 1 \end{aligned} \]

Each step takes a square root, and n reaches a constant after \(\log \log n\) square roots (write \(n = 2^{2^{k}}\); each step halves k). So \(S(n) = \Theta(\log \log n)\) and \(T(n) = \Theta(n \log \log n)\). \(n \log n\) would come from halving n at each level instead of square-rooting it, and the \(n^{2}\) options overcount the work per level, which is n at every level of the recursion.

Please log in or register to report this question.

3.

Consider the following recurrence relation:

\(T(n) = 2T(n - 1) + n2^{n}\) for \(n \gt 0\), \(T(0) = 1\).

Which ONE of the following options is CORRECT?

Exam Year(s)

GATE CS - 2025,

Answer: A

The solution is \(T(n) = \Theta(n^{2}2^{n})\).

Divide both sides by \(2^{n}\) and set \(S(n) = T(n)/2^{n}\):

\[ \begin{aligned} \frac{T(n)}{2^{n}} &= \frac{T(n-1)}{2^{n-1}} + n \\ S(n) &= S(n-1) + n \\ S(n) &= 1 + \frac{n(n+1)}{2} \end{aligned} \]

using \(S(0) = T(0) = 1\). So \(T(n) = 2^{n}\left(1 + n(n+1)/2\right) = \Theta(n^{2}2^{n})\).

\(\Theta(n2^{n})\) treats the added term as if it did not accumulate over the n levels, \((\log n)^{2}2^{n}\) grows too slowly, and \(4^{n}\) is far too fast, since the recurrence only doubles at each step.

Please log in or register to report this question.

4.

Consider the following recurrence relations:

For all \(n \gt 1\),

\[ \begin{aligned} T_{1}(n) &= 4T_{1}\left(\frac{n}{2}\right) + T_{2}(n) \\ T_{2}(n) &= 5T_{2}\left(\frac{n}{4}\right) + \Theta(\log_{2} n) \end{aligned} \]

Assume that for all \(n \le 1\), \(T_{1}(n) = 1\) and \(T_{2}(n) = 1\).

Which one of the following options is correct?

Exam Year(s)

GATE CS - 2026,

Answer: A

The answer is \(T_{1}(n) = \Theta(n^{2})\).

Solve the inner recurrence first with the master theorem. For \(T_{2}\), a = 5 and b = 4, so \(n^{\log_{4} 5} \approx n^{1.16}\), which grows faster than \(\log_{2} n\):

\[ T_{2}(n) = \Theta(n^{\log_{4} 5}) \]

For \(T_{1}\), a = 4 and b = 2, so the leaves contribute \(n^{\log_{2} 4} = n^{2}\). The added term \(T_{2}(n) = \Theta(n^{1.16})\) is polynomially smaller than \(n^{2}\), so the first case applies:

\[ T_{1}(n) = \Theta(n^{2}) \]

The extra \(\log_{2} n\) factor would appear only if the added term were \(\Theta(n^{2})\). The options with \(n^{\log_{4} 5}\) give the order of \(T_{2}\), not \(T_{1}\).

Please log in or register to report this question.

5.

The following function computes \(X^{Y}\) for positive integers X and Y.

int exp(int X, int Y) {
    int res = 1, a = X, b = Y;

    while ( b != 0 ){
        if ( b%2 == 0) { a = a*a; b = b/2; }
        else           { res = res*a; b = b-1; }
    }
    return res;
}

Which one of the following conditions is TRUE before every iteration of the loop?

Exam Year(s)

GATE CS - 2016,

Answer: C

The loop invariant is \(X^{Y} = \mathrm{res} * a^{b}\).

Check that it holds at the start and is preserved by each branch:

  • Initially \(\mathrm{res} = 1\), \(a = X\), \(b = Y\), so \(\mathrm{res} \cdot a^{b} = X^{Y}\).
  • If \(b\) is even, \(a\) becomes \(a^{2}\) and \(b\) becomes \(b/2\): \((a^{2})^{b/2} = a^{b}\), unchanged.
  • If \(b\) is odd, \(\mathrm{res}\) becomes \(\mathrm{res} \cdot a\) and \(b\) becomes \(b - 1\): \(\mathrm{res} \cdot a \cdot a^{b-1} = \mathrm{res} \cdot a^{b}\), unchanged.

When the loop ends, \(b = 0\), so \(X^{Y} = \mathrm{res}\), which is what the function returns.

\(X^{Y} = a^{b}\) fails after the first odd step, when part of the power has moved into \(\mathrm{res}\). \(X^{Y} = (\mathrm{res} * a)^{b}\) raises \(\mathrm{res}\) to the power \(b\) as well, and \((\mathrm{res} * a)^{Y} = (\mathrm{res} * X)^{b}\) holds at the start but fails after the first even step, when \(a\) becomes \(X^{2}\) while \(b\) halves.

Please log in or register to report this question.

6.

Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices. The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).

int first=0, second=0, len1=0, len2=0, maxlen=0;
for (int i=0; i \(\lt\) n; i++) { if (X[i] == first) { len2++; len1++; } else if (X[i] == second) { len2++; len1 = (P); second = first;
} else { len2 = (Q); len1 = 1; second = first; }
if (len2 \(\gt\) maxlen) { maxlen = len2; } first = X[i]; }

Which one of the following options gives the CORRECT missing expressions?
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)

Exam Year(s)

GATE CS - 2024,

Answer: B

The missing expressions are (P) 1 and (Q) len1+1.

At each step, first holds X[i-1] and second the other value in the current two-value window. If X[i] equals second, it differs from the previous element, so the run of equal values ending at X[i] is X[i] alone: len1 becomes 1, which is (P). The window still has only two values, so len2 grows by one.

If X[i] is a new third value, the longest two-value subarray ending at X[i] is the run of equal values ending at X[i-1] (length len1) followed by X[i], so len2 becomes len1+1, which is (Q). Then len1 resets to 1.

(P) len1+1 would extend a run across a change of value, and (Q) len2+1 would keep three distinct values in the window, so the other options fail.

Please log in or register to report this question.

7.

Consider an unordered list of \(N\) distinct integers.

What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?

Exam Year(s)

GATE CS - 2025,

Answer: A

One comparison is enough: 1.

Pick any two elements and compare them. Since all elements are distinct, one is strictly smaller than the other, and the smaller one cannot be the largest element of the list. So it is an answer, found with a single comparison. Zero comparisons cannot work, because without comparing anything no element is known not to be the maximum.

N - 1 is the number of comparisons needed to find the largest element itself, which the question does not ask for. N and 2N - 1 are more than even that.

Please log in or register to report this question.

8.

Consider the following functions, where \(n\) is a positive integer.

\(n^{1/3}\), \(\log(n)\), \(\log(n!)\), \(2^{\log(n)}\)

Which one of the following options lists the functions in increasing order of asymptotic growth rate?

Note: Assume the base of log to be 2.

Exam Year(s)

GATE CS - 2026,

Answer: A

The increasing order is \(\log(n)\), \(n^{1/3}\), \(2^{\log(n)}\), \(\log(n!)\).

Simplify each function first:

  • log(n) grows more slowly than any positive power of n;
  • n1/3 is a polynomial of degree 1/3;
  • 2log n with base-2 logarithms is exactly n;
  • log(n!) is Θ(n log n) by Stirling's approximation.

So the order is log n, n1/3, n, n log n. An order that puts n1/3 below log n reverses the rule that every power beats a logarithm. Putting 2log n first misses that it equals n, and putting it last ranks n above n log n.

Please log in or register to report this question.

9.

Consider an array \(A\) of integers of size \(n\). The indices of \(A\) run from 1 to \(n\). An algorithm is to be designed to check whether \(A\) satisfies the condition given below.

\(\forall i,j \in \{1,\ldots,n-1\}\) such that \(i \gt j\), \((A[i+1] - A[i]) \gt (A[j+1] - A[j])\)

Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?

Exam Year(s)

GATE CS - 2026,

Answer: A

The fastest algorithm takes Θ(n) in the worst case.

Write di = A[i+1] - A[i] for i = 1, ..., n - 1. The condition says di > dj whenever i > j: the sequence of differences is strictly increasing. Strict increase of a whole sequence is equivalent to strict increase between each pair of neighbours, di+1 > di, because the relation > is transitive. So one pass computing the n - 1 differences and comparing neighbours suffices: O(n).

No algorithm can do better, since changing any single element of A can break the condition, so every element must be read: Ω(n). Checking all pairs (i, j) would take Θ(n2), and Θ(log n) is impossible for the same reason.

Please log in or register to report this question.

10.

Consider the following C code. Assume that unsigned long int type length is 64 bits.

unsigned long int fun(unsigned long int n){
  unsigned long int i, j = 0, sum = 0;
  for (i = n; i \(\gt\) 1; i = i/2) j++;
  for ( ; j \(\gt\) 1; j = j/2) sum++;
  return(sum);
}

The value returned when we call fun with the input \(2^{40}\) is

Exam Year(s)

GATE CS - 2018,

Answer: B

The function returns 5.

The first loop halves i from \(2^{40}\) until it reaches 1, counting the halvings: j ends as 40, which is \(\log_{2} n\).

The second loop halves j while it is greater than 1, counting in sum: j goes 40, 20, 10, 5, 2, 1. The body runs for 40, 20, 10, 5 and 2, so sum is 5, which is \(\lfloor \log_{2} 40 \rfloor\).

40 is the value of j after the first loop, not the returned sum. 4 and 6 come from miscounting the halvings: integer division takes 5 to 2 and 2 to 1, so the body runs at 5 and at 2 but not at 1.

Please log in or register to report this question.

Loading…
Tags: Algorithm Analysis and Complexity Questions and Answers || Algorithm Analysis and Complexity MCQ Questions and Answers || Algorithm Analysis and Complexity GK Questions and Answers || Algorithm Analysis and Complexity GK MCQ Questions || Algorithm Analysis and Complexity Multiple Choice Questions and Answers || Algorithm Analysis and Complexity GK || GK on Algorithm Analysis and Complexity || Data Structures and Algorithms Questions and Answers || Data Structures and Algorithms MCQ Questions and Answers || Data Structures and Algorithms GK Questions and Answers || GK on Data Structures and Algorithms