Top MCQs on Algorithms in DSA with Answers

Last Updated : 27 Sep, 2023

The word Algorithm means “A set of rules to be followed in calculations or other problem-solving operations” Or “A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive operations “.
More on Algorithms

Algorithms Quiz

Algorithms Quiz


Question 1

In a complete k-ary tree, every internal node has exactly k children. The number of leaves in such a tree with n internal nodes is: 

Cross

nk

Cross

(n – 1) k+ 1

Tick

n( k – 1) + 1

Cross

n( k – 1)



Question 1-Explanation: 

For an k-ary tree where each node has k children or no children, following relation holds L = (k-1)*n + 1 Where L is the number of leaf nodes and n is the number of internal nodes. Let us see following for example

k = 3
Number of internal nodes n = 4
Number of leaf nodes = (k-1)*n  + 1
                     = (3-1)*4 + 1
                     = 9 

Question 2
Given 8 identical coins out of which one coin is heavy and a pan balance. How many minimum number of measurements are needed to find the heavy coin?
Tick
2
Cross
3
Cross
4
Cross
7


Question 2-Explanation: 
Divide the coins into three groups and name the coins according to there group:
A: A1, A2, A3
B: B1, B2, B3
C: C1, C2

Measure group A and group B. Two cases arise:
1. They are equal. One more measurement is needed to find the heavy 
   coin in group C. Total two measurements needed in this case.
2. They are not equal. Find the heavy group, say A. Pick any two coins
   from this group,  say A1 and A3. Measure A1 and A3 in the pan balance. 
   Two cases arise:
   2.1 They are equal. A2 is the heavy coin. Total two measurements 
       needed.
   2.2 They are not equal. It is known which of A1 or A3 is heavy. 
       Total two measurements needed.
So, the above observations says that in any case, 2 measurements are enough
to find the heavy coin.

Follow up:
Generalize the minimum number of measurements for n coins 
with one coin heavy.
Question 3
In a village, people build houses in the same side of the road. A thief plans to loot the village. He wants maximum amount of money without having any risk of getting caught. By some means, the villagers know that their adjacent house is being looted or not and thus they become alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of money stored in each house and the road is straight and there is no turning, which is the most efficient algorithmic strategy to solve this problem?
Cross
Brute-force
Tick
Dynamic Programming
Cross
Backtracking
Cross
Divide and Conquer


Question 3-Explanation: 
If we take a closer look, the problem boils down to:
Given an array with some finite size where each element represents 
a positive number, find the maximum sum such that no two elements 
are adjacent.
Dynamic Programming is the efficient technique to solve this. 
The algorithm can be given as follows:
Maintain an auxiliary array loot.
loot[0] = arr[0]
loot[1] = arr[1]
loot[i] = max(loot[i - 1], loot[i - 2] + arr[i]),  2 <= i < n
loot[n - 1] gives the maximum amount of money the thief can take away.
Question 4
Which of the following is not an in-place algorithm?
Cross
Insertion sort
Cross
Selection sort
Tick
Merge sort
Cross
Heap sort


Question 4-Explanation: 
An in-place algorithm is an algorithm which uses a constant amount of extra space apart from input. Merge sort uses an extra O(n) space in the merging part.
Question 5

A set X can be represented by an array x[n] as follows: 

 



Consider the following algorithm in which x,y, and z are Boolean arrays of size n: 

C

algorithm zzz(x[] , y[], z [])
{
   int i;
   for (i=O; i<n; ++i)
     z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i]) }

The set Z computed by the algorithm is:

Cross

(X Intersection Y)

Cross

(X Union Y)

Cross

(X-Y) Intersection (Y-X)

Tick

(X-Y) Union (Y-X)



Question 5-Explanation: 

The expression x[i] ^ ~y[i]) results the only 1s in x where corresponding entry in y is 0. An array with these set bits represents set X – Y The expression ~x[i] ^ y[i]) results the only 1s in y where corresponding entry in x is 0. An array with these set bits represents set Y – X. The operator “V” results in Union of the above two sets.

Question 6

The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is 

Cross

θ(n)

Tick

θ(logn)

Cross

θ(nlogn)

Cross

θ(1)



Question 6-Explanation: 

whenever there exists an element which is present in the array : more than n/2 times, then definitely it will be present at the middle index position; in addition to that it will also be present at anyone of the neighbourhood indices namely i−1 and i+1 
No matter how we push that stream of More than n/2 times of elements of same value around the Sorted Array, it is bound to be present at the middle index + atleast anyone of its neighbourhood once we got the element which should have occurred more that n/2 times we count its total occurences in O(log⁡n) time.

Question 7
An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array (GATE CS 2006)
Cross
Solves it in linear time using a left to right pass of the array
Tick
Solves it in linear time using a right to left pass of the array
Cross
Solves it using divide and conquer in time 8(nlogn)
Cross
Solves it in time 8(n2)


Question 7-Explanation: 
Please see this for explanation.
Question 8
Consider the following C function.
float f(float x, int y) 
{ 
  float p, s; int i; 
  for (s=1, p=1, i=1; i < y; i ++) 
  { 
    p*= x/i; 
    s+=p; 
  } 
  return s; 
}   
For large values of y, the return value of the function f best approximates (GATE CS 2003)
Cross
x^y
Tick
e^x
Cross
ln(1 + x)
Cross
x^x


Question 8-Explanation: 
The function f() is implementation of Taylor\'s Series to calculates e^x
   e^x = 1 + x + x^2/2! + x^3/3! + ---
More is the value of y more precise value of e^x will be returned by f()
Question 9
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, 1, n);
(GATE CS 2000)
Tick
Rotates s left by k positions
Cross
Leaves s unchanged
Cross
Reverses all elements of s
Cross
None of the above


Question 9-Explanation: 
Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. Please see this post for details. If we rotate an array n times for k = 1 to n, we get the same array back.
Question 10
An inversion in a an array A[] is a pair (A[i], A[j]) such that A[i] > A[j] and i < j. An array will have maximum number of inversions if it is:
Cross
Sorted in increasing order
Tick
Sorted in decreasing order
Cross
Sorted in alternate fashion
Cross
Both A and B


Question 10-Explanation: 
If the array is sorted in decreasing order, each pair will be an inversion. e.g. 5, 4, 3, 2, 1 Maximum number of inversions possible = n * (n - 1) / 2 where n is the size of the array.
There are 49 questions to complete.


Share your thoughts in the comments

Similar Reads