• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Top MCQs on Algorithms in DSA with Answers

Question 21

Consider the following C program C
main()
{
    int x, y, m, n;
    scanf (\"%d %d\", &x, &y);
    /* Assume x > 0 and y > 0  */
    m = x;
    n = y;
    while (m! = n)
    {
        if (m > n)
            m = m - n;
        else
            n = n - m;
    }
    print f (\"% d\", n);
}
The program computes
  • x ÷ y using repeated subtraction
  • x mod y using repeated subtraction
  • the greatest common divisor of x and y
  • the least common multiple of x and y

Question 22

What does the following algorithm approximate? C
    x = m;
    y = 1;
    while (x - y > e)
    {
        x = (x + y)/2;
        y = m/x;
    }
    print(x);
(Assume m > 1, e > 0).
  • log m
  • m2
  • m1/2
  • m1/3

Question 23

Huffman coding is a lossless data compression algorithm. The most frequent character gets the smallest code and the least frequent character gets the largest code. Consider the following statements regarding Huffman coding algorithm? 

S1 : The time complexity of the Huffman algorithm is O(nlogn). Using a heap to store the weight of each tree, each iteration requires O(logn) time to determine the cheapest weight and insert the new weight. There are O(n) iterations, one for each item. 

S2 : If the input array is sorted, there exists a linear time algorithm. 

S3 : A divide-and-conquer approach might have us asking which characters should appear in the left and right subtrees and trying to build the tree from the top down. As with the optimal binary search tree, this will lead to to an exponential time algorithm. 

Which of the following option is correct?

  • Statement S1 is correct, Statements S2 and S3 are not correct.

  • Statements S1 and S2 are correct and statement S3 is not correct.

  • Statements S2 and S3 are correct and statement S1 is not correct.

  • All statements S1, S2, and S3 are correct.

Question 24

In a permutation a1.....an of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj. If all permutations are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1.....n ?
  • n(n - 1)/2
  • n(n - 1)/4
  • n(n + 1)/4
  • 2n[log2 n]

Question 25

The following are the starting and ending times of activities A, B, C, D, E, F, G and H respectively in chronological order: "asbscsaedsceesfsbedegseefehsgehe" Here, xs denotes the starting time and xe denotes the ending time of activity X. W need to schedule the activities in a set of rooms available to us. An activity can be scheduled in a room only if the room is reserved for the activity for its entire duration. What is the minimum number of rooms required ?
  • 3
  • 4
  • 5
  • 6

Question 26

In the following C program fragment, j, k n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉ 

C
for (k = 3; k < = n; k++)
    A[k] = 0;
for (k = 2; k < = TwoLog_n; k++)
    for (j = k + 1; j < = n; j++)
        A[j] = A[j] || (j % k);
for (j = 3; j < = n; j++)
    if (!A[j]) printf(\"%d\", j);

The set of numbers printed by this program fragment is

  • {m | m ≤ n, (∃ i) [m = i!]} Here i! mean factorial of i

  • {m | m ≤ n, (∃ i) [m = i2]}

  • {m | m ≤ n, m is prime}

  • Last print never executes

Question 27

An n x n array v is defined as follows:
v[i, j] = i-j for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is
  • 0
  • n-1
  • n2 - 3n + 2
  • n2 (n+1)/2

Question 28

X, Y and Z are closed intervals of unit length on the real line. The overlap of X and Y is half a unit. The overlap of Y and Z is also half a unit. Let the overlap of X and Z be k units. Which of the following is true?
  • k must be 1
  • k must be 0
  • k can take any value between 0 and 1 (d) None of the above
  • None of the above

Question 29

Suppose you are given an array s[1..n] and a procedure reverse (s, i, j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 <= k <= n:
reverse(s, 1, k) ;
reverse(s, k + 1, n);
reverse(s, l, n); 
  • Rotates s left by k positions
  • Leaves s unchanged
  • Reverses all elements of s
  • None of the above

Question 30

Let swap() be a function that swaps two elements using their addresses. Consider the following C function. C
void fun(int arr[], int n)
{
    for (int i = 0; i < n; i+=2)
    {
        if (i>0 && arr[i-1] > arr[i] )
            swap(&arr[i], &arr[i-1]); 
        if (i<n-1 && arr[i] < arr[i+1] )
            swap(&arr[i], &arr[i + 1]);
    }
}
If an array {10, 20, 30, 40, 50, 60, 70, 80} is passed to the function, the array is changed to
  • {20, 10, 40, 30, 60, 50, 80, 70}
  • {10, 30, 20, 40, 60, 50, 80, 70}
  • {10, 20, 30, 40, 50, 60, 70, 80}
  • {80, 70, 60, 50, 40, 30, 20, 10}

There are 49 questions to complete.

Last Updated :
Take a part in the ongoing discussion