Design & Analysis of Algorithm logo

DAA Unit 1 Part 1 MCQs

Unit 1

Father of Algorithm, What is Algorithm, Need of an algorithm, Algorithm Vs Program, Good Algorithm, Analysis of Algorithm, Comparison of running time, Asymptotic notation, Recurrence relation, Substitution method, Iterative method, Recurrence Tree Method, Master Theorem, Extended Master Theorem.

Q1 – Which of the following shows the correct relationship among complexities?
  1. O(√n) < O(logn) < O(n) < O(nlogn) < O(n3) < O(2n)
  2. O(logn) < O(√n) < O(n) < O(nlogn) < O(2n) < O(n3)
  3. O(logn) < O(√n) < O(n) < O(nlogn) < O(n3) < O(2n)
  4. O(logn) < O(n) < O(nlogn) < O(√n) < O(n3) < O(2n)

Ans – (3)

Explanation – Let n = 64

Q2 – An algorithm is made up of 2 modulus M1 and M2. If order of M1 is f(n) and M2 is g(n) then the order of the algorithm is
  1. max(f(n), g(n))
  2. min(f(n), g(n))
  3. f(n) + g(n)
  4. f(n)*g(n)

Ans – (1)

Explanation – This is because when we analyze the time complexity of an algorithm, we are interested in the most significant term in the expression that describes the running time as a function of the input size n. In this case, since the two modules are independent and executed in sequence, the running time of the overall algorithm would be dictated by the module that takes longer to run.

 

Q3 – Consider the following three claims:
(i) (n + k)m = Ɵ(nm), where k and m are constant
(ii) 2n+1 = O(2n)
(iii) 22n+1 = O(2n)
Which of the following claim is correct?
  1. i and iii
  2. i and ii
  3. ii and iii
  4. i, ii and iii

Ans – (2)

Explanation – Claim (i) is true. To see this, let k=1 and m=2

Claim (ii) is true. To see this, note that 2^(n+1) = 2 * 2^n, and so 2^(n+1) is always at most a constant multiple (namely, 2) of 2^n. Thus, we have 2^(n+1) = O(2^n).

Claim (iii) is not true. To see this, note that 2^(2n+1) = 2 * 2^(2n) = 2 * (2^n)^2. Thus, 2^(2n+1) grows faster than any constant multiple of 2^n, and so it is not in O(2^n).

Q4 – Let T(n)  be defined by T(1) = 7 and T(n+1) = 3n + T(n) for all integers n ≥ 1. Which of the following represents the order of growth of T(n) as a function of ‘n’? (RPSC-2021)
  1. Ɵ(n)
  2. Ɵ(nlogn)
  3. Ɵ(n2)
  4. Ɵ(2n)

Ans – (3)

Explanation

Let’s use recursion:

T(n+1) = 3n + T(n)

T(n) = 3(n-1) + T(n-1) = 3(n-1) + 3(n-2) + T(n-2) = 3(n-1) + 3(n-2) + … + 3(1) + T(1)

= 3(1 + 2 + … + (n-1)) + 7

= 3(n-1)(n)/2 + 7

= 3/2 n^2 – 3/2 n + 7

Therefore, the order of growth of T(n) is Ɵ(n^2), since the highest order term is n^2.

Q5 – The recurrence relation an+1 – 7an = 0 with a0 = 7, n ≥ 0 is equivalent to
  1. an =7n-1 n ≥ 0
  2. an =7n+1 n ≥ 0
  3. an =7n-1 n ≥ 1
  4. an =7n+1 n ≥ 1

Ans – (2)

Explanation

Given a0 = 7n-1

Then, a1 = 49 = 72

Also, a2 = 243 = 73

Q6 – if T(C) = C for some constant,
otherwise, T(n) = 5/2T(2n/3) + na
The result of the above question is T(n) = Ɵ(n3) then find the value of a?
  1. 1.5
  2. 2.5
  3. 3
  4. 4

Ans – (3)

Explanation

We use master method. In our case, we have a = 5/2, b = 3/2, and f(n) = n^a. We can compute logb(a) as follows:

 

Logb(a) = log(5/2)(3/2) ≈ 1.378

The result is n^3, So f(n) wins and a = 3.

Q7 – What is an algorithm?
  1. A mathematical problem solution
  2. The set of instructions applied on the input to get the output
  3. Well defined set of output
  4. A step-by-step procedure for solving a specific type of problem

Ans – (4)

Explanation

It is not a mathematical solution or a set. The definition of the set is “collection of elements” and the definition of an element is “any one of the distinct objects that belong to that set.” The algorithm has several steps, and there are many duplicates of the same step.

An algorithm is a systematic approach to problem-solving that can be executed by either a human or a computer.

Q8 – Who is the father of algorithm?
  1. Muhammad ibn Musa al-Khwarizmi
  2. David Erwin Knuth
  3. Hamiltonian
  4. Euler

Ans – (1)

Explanation

Muhammad ibn Musa al-Khwarizmi was a Persian mathematician and scholar who lived during the Islamic Golden Age in the 9th century. Al-Khwarizmi was a pioneer in the field of algorithm development, particularly in his work on the Hindu-Arabic numeral system and the development of algorithms for arithmetic operations using these numerals. While al-Khwarizmi is often credited with the development of algorithms, the concept of an algorithm existed long before his time, and the term “algorithm” itself is derived from the Latinized form of his name, Algoritmi.

Al-Khwarizmi is often referred to as the “father of algebra” for his contributions to the development of algebraic concepts.

https://compgeek.co.in/father-of-algorithm/ to know more

Q9 – Who is the father of ‘Analysis of Algorithm’?
  1. Muhammad ibn Musa al-Khwarizmi
  2. David Erwin Knuth
  3. Hamiltonian
  4. Euler

Ans – (2)

Explanation

Donald Ervin Knuth was born on January 10, 1938, and is a computer scientist, mathematician, and professor emeritus at Stanford University. Knuth is known as the “father of algorithmic analysis.”

https://compgeek.co.in/what-is-algorithm/ to know more

Q10 – Characteristics of an algorithm by Donald Ervin Knuth are
  1. Input, output, definite, finite, connective.
  2. input, output, definite, finite, effective.
  3. input, output, reflective, finite, effective.
  4. input, output, reflective, finite, connective.

Ans – (2)

Explanation

Input – There can be zero or more inputs such that there is no input in the program of hello world.

Output – At least one output must come.

Definiteness – Every instruction should be very clear and unambiguous. There should be no doubt or misunderstanding in the instruction.

Effectiveness – Every instruction should be very basic and possible and result-giving.

Finiteness – The algorithmic instruction must end after a finite number and also number of operations are finite. That is why we write the ‘End’ after the algorithm is over.

https://compgeek.co.in/what-is-algorithm/ to know more

Q11 – ‘The Art of Programming’ is written by
  1. Hamiltonian
  2. Kruskal
  3. Donald Erwin Knuth
  4. Donald L. Shell

Ans – (3)

Q12 – “Suppose computer were infinitely fast and computer memory was free”. Would you have any reason to study algorithm?
  1. No, life is so easy
  2. Yes, to demonstrate the algorithm was correct.
  3. Yes, to demonstrate that the algorithm will stop after a finite instructions.
  4. Both 3 & 4.

Ans – (4)

Q13 – If an Algorithm A1 has time complexity O(n) and space complexity O(n) and an algorithm A2 has time complexity O(nlogn) and space complexity O(1) for a problem, then which one would you choose?
  1. A1
  2. A2
  3. Any one
  4. No one

Ans – (1)

Explanation – We have a limited memory and time, but A1 has less time complexity than A2, so you should choose A1. Space is important, but time is more important.

Q 14 – When an algorithm is written in the form of a programming language, it becomes a _________
  1. Flowchart
  2. Program
  3. Pseudo code
  4. Syntax

Ans – (2)

Explanation – When an algorithm is completely written in the form of a programming language, it becomes a program.

Q15 – Define pseudocode?
  1. Definite and terminates after a number of steps
  2. Combination of Native & programming language
  3. Every sentence is effective
  4. None of these

Ans – (2)

Explanation

Pseudocode is written only in a combination of native language and programming language.

Every other option 1 and 3 are related to algorithm.

Q16 – Arrange the following complexity of the algorithm in ascending order. (DSSSB PGT 2021)
O(2n), O(n), O(log2n), O(nlog2n), O(n2)
  1. O(n), O(log2n), O(nlog2n), O(n2), O(2n)
  2. O(n), O(n2), O(log2n), O(nlog2n), O(2n)
  3. O(log2n), O(n), O(nlog2n), O(n2), O(2n)
  4. O(log2n), O(nlog2n), O(n), O(n2), O(2n)

Ans – (3)

Explanation – if n = 8, then you will get the ascending order. Write and calculate.

Q17 – What is the time complexity of selection sort algorithms? (DSSSB PGT 2021)
  1. O(2n)
  2. O(nlog2n)
  3. O(n)
  4. O(n2)

Ans – (4)

Explanation

When we keep the value of index 1 at a minimum, then the number of operations or comparisons will be n-1.

keep index 2 minimum, then comparisons will be n-2.

keep index 3 as minimum then comparisons will be n-3.

Similarly, there are number of operations or comparisons in the entire selection sort.

= (n-1) + (n-2) + (n-3) + … + 3 + 2 + 1

= n(n-1)/2 = O(n2).

https://compgeek.co.in/selection-sort/ to know more.

Q18 – Two-way merge sort algorithm is used to sort the following elements in ascending order.
200, 470, 150, 80, 90, 40, 400, 300, 120, 70.
What is the order of these elements after second pass of the merge sort algorithm? (DSSSB PGT 2021)
  1. 40, 70, 80, 90, 120, 150, 200, 300, 400, 470
  2. 80, 150, 200, 470, 40, 90, 300, 400, 70, 120
  3. 40, 80, 90, 150, 200, 300, 400, 470, 70, 120
  4. 200, 470, 80, 150, 40, 90, 300, 400, 70, 120

Ans – (2)

Explanation

Individual elements

200, 470, 150, 80, 90, 40, 400, 300, 120, 70

1st pass – Array contains 2 elements sorted

200, 470, 80, 150, 40, 90, 300, 400, 70, 120

2nd pass – Array contains 4 elements sorted

80, 150, 200, 470, 40, 90, 300, 400, 70, 120

Q19 – What is the complexity of the following code?
Sum = 0;
    for(i =1; i <= n; i*=2)
         for(j = 1; j <= n; j++)
               sum++:
Which of the following is not a valid string? (ISRO Scientist 2020)
  1. O(n2)
  2. O(nlogn)
  3. O(n)
  4. O(nlognlogn)

Ans – (Please read explanation)

Explanation

This question was not included to result as this question have an ambiguity.

Before going to another question, I will help you out with the problem it has. The inner loop executes n times and the outer loop executes logn times. So, total complexity for this code will be O(nlogn).

But it says “not a valid string” it means 1, 3 and 4 will be the answer because it contains extra space.

If the question says “What is the time complexity of this code” then the answer will be O(nlogn).

Q20 – Consider product of three matrices M1, M2 and M3 having w rows and x columns, x rows and y columns, and y rows and z columns. Under what condition will it take less time to compute the product as (M1M2)M3 than to compute M1(M2M3)? (ISRO Scientist 2020)
  1. Always take the same time
  2. (1/x + 1/z) < (1/w + 1/y)
  3. x > y
  4. (w+x) > (y+z)

Ans – (2)

Explanation

This question is related to dynamic programming of matrix multiplication.

M1 = w*x

M2 = x*y

M3 = y*z

M1M2 = w*x*y

(M1M2)M3 = w*y*z

Time taken = w*x*y + w*y*z

 

M2M3 = x*y*z

M1(M2M3) = w*x*z

Time taken = x*y*z + w*x*z

Under what condition will it take less time to compute the product as (M1M2)M3 than to compute M1(M2M3)

=> w*x*y + w*y*z < x*y*z + w*x*z

Dividing whole equation by wxyz

=> (1/z + 1/x) < (1/w + 1/y)

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha