☰
DAA MCQ
Algorithm
The algorithm is a step-by-step process to solve any problem and is a sequence of instructions that act
on some input data to produce some output in a finite number of steps. The algorithm is independent of
any programming language.
Why analysis of algorithms?
For a given problem, there are many ways to design algorithms for it
Analysis of algorithms to determine which algorithm should be chosen to solve the problem.
The complexity of algorithm on performance analysis of algorithm:
Time complexity: time complexity of an algorithm is the total time required by the program to run till its
completion
Space complexity: Space complexity is the total space required by an algorithm to run till its
completion.
Time and space complexity depends on lots of things like hardware, OS, processes, etc.
The analysis is of two types:
Posteriori analysis: In Posteriori analysis, Algorithm is implemented and executed on certain fixed
hardware and software. Then the algorithm is selected which takes the least amount of time to execute.
Hence, the time used is given in time units like ms, ns, etc.
Priori analysis: In Priori analysis, The time of the algorithm is found prior to implementation. Here time is
not in terms of any such time units. Instead, it represents the number of operations that are carried out
while executing the algorithm.
Asymptotic notations:
Asymptotic notations are used to represent the complexity of an algorithm
With the help of asymptotic notations, we can analyze the time performance of the algorithm.
There are three types of asymptotic notations:
Theta notation
Omega notation
Big Oh Notation
Design and Analysis of Algorithms MCQ
1.
Dijkstra’s algorithm is used to solve __________ problems?
Network lock
Single source shortest path
All pair shortest path
Sorting
Show Explanation
Wrong Answer
Answer - B) Dijkstra’s algorithm is used to solve single source shortest path problems.
2.
The Bellmann Ford Algorithm returns __________ value?
String
Boolean
Double
Integer
Show Explanation
Wrong Answer
Answer - B) The Bellmann Ford Algorithm returns a boolean value.
3.
Which of the following is used for solving the N Queens Problem?
Greedy algorithm
Dynamic programming
Backtracking
Sorting
Show Explanation
Wrong Answer
Answer - C) Backtracking is used for solving the N Queens Problem.
4.
Which of the following statements is true about AVL Trees?
The difference between the heights of left and right nodes cannot be more than 1.
The height of an AVL Tree always remains of the order of O(logn)
AVL Trees are a type of self-balancing Binary Search Trees.
All of the above.
Show Explanation
Wrong Answer
Answer - D) All the above options are applicable for an AVL Tree.
5.
Representation of data structure in memory is known as?
Storage structure
File structure
Recursive
Abstract Data Type
Show Explanation
Wrong Answer
Answer - D) Representation of data structure in memory is known as Abstract Data Type.
6.
In what time complexity can we find the diameter of a binary tree optimally?
O(V + E)
O(V)
O(E)
O(V * logE)
Show Explanation
Wrong Answer
Answer - A) We can compute the diameter of a binary tree using a single DFS, which takes the time of
O(V + E).
7.
To main measures of the efficiency of an algorithm are?
Time and space complexity
Data and space
Processor and memory
Complexity and capacity
Show Explanation
Wrong Answer
Answer - A) Time and space complexity are the main measures of the efficiency of an algorithm.
8.
Which of the following sorting algorithms provide the best time complexity in the worst-case scenario?
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
Show Explanation
Wrong Answer
Answer - A) Merge Sort will always have a time complexity of O(n * logn) which is the best in the worst
case among these algorithms.
9.
Which of the following is a Divide and Conquer algorithm?
Bubble Sort
Selection Sort
Heap Sort
Merge Sort
Show Explanation
Wrong Answer
Answer - D) Merge Sort is a Divide and Conquer algorithm.
10.
Which of the following data structure is used to perform recursion?
Linked list
Array
Queue
Stack
Show Explanation
Wrong Answer
Answer - C) Stack is used for performing recursion.
11.
Identify the best case time complexity of selection sort?
O(nlogn)
O(n^2)
O(n)
O(1)
Show Explanation
Wrong Answer
Answer - B) The best case time complexity of selection sort is O(n^2).
12.
Another name of the fractional knapsack is?
Non-continuous knapsack problem
Divisible knapsack problem
0/1 knapsack problem
Continuous Knapsack Problem
Show Explanation
Wrong Answer
Answer - D) Fractional knapsack is also known as the continuous knapsack problem.
13.
Identify the approach followed in Floyd Warshall’s algorithm?
Linear programming
Dynamic Programming
Greedy Technique
Backtracking
Show Explanation
Wrong Answer
Answer - B) The approach followed in Floyd Warshall’s algorithm is dynamic programming.
14.
Hamiltonian path problem is _________?
NP problem
P class problem
NP-complete problem
N class problem
Show Explanation
Wrong Answer
Answer - C) Hamiltonian path problem is an NP-complete problem.
15.
What is the time complexity of the following code snippet in C++?
void solve() {
string s = "scaler";
int n = s.size();
for(int i = 0; i < n; i++) {
s = s + s[i];
cout << s << endl;
O(n)
O(n^2)
O(1)
O(log n)
Show Explanation
Wrong Answer
Answer - B) The s = s + s[i] line first makes a copy of the original string and then appends the new
character in it, leading to each operation being O(n). So the total time complexity is O(n^2).
16.
When a pop() operation is called on an empty queue, what is the condition called?
Overflow
Underflow
Syntax Error
Garbage Value
Show Explanation
Wrong Answer
Answer - B) pop() on an empty queue causes Underflow.
17.
What is the time complexity of the binary search algorithm?
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
Answer - C) The time complexity of the binary search algorithm is O(log2n).
18.
What will be the best sorting algorithm, given that the array elements are small (<= 1e6)?
Bubble Sort
Merge Sort
Counting Sort
Heap Sort
Show Explanation
Wrong Answer
Answer - C) Counting sort sorts an array in O(n) time complexity, taking up an extra space complexity of
O(max(a[i])).
19.
What is the time complexity of the Sieve of Eratosthenes to check if a number is prime?
O(nlog(logn)) Precomputation, O(1) for check.
O(n) Precomputation, O(1) for the check.
O(n * logn) Precomputation, O(logn) for check.
O(n) Precomputation, O(logn) for check.
Show Explanation
Wrong Answer
Answer - A) The Sieve of Eratosthenes checks if a number is prime in O(1) in the range [1, n] by using an
O(nlog(logn)) precomputation and O(n) space complexity.
20.
The worst-case time complexity of Quicksort is?
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
Answer - D) The worst-case time complexity of Quicksort is O(n^2).
21.
What is the technique called in which it does not require extra memory for carrying out the sorting
procedure?
Stable
Unstable
In-place
In-partition
Show Explanation
Wrong Answer
Answer - C) The technique which does not require extra memory for carrying out the sorting procedure
is in-place.
22.
Identify the slowest sorting technique among the following?
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
Show Explanation
Wrong Answer
Answer - C) Bubble sort is the slowest sorting technique.
23.
Select the correct recurrence relation for Tower of Hanoi?
T(N) = 2T(N-1)+1
T(N) = 2T(N/2)+1
T(N) = 2T(N-1)+N
T(N) = 2T(N-2)+2
Show Explanation
Wrong Answer
Answer - A) The recurrence relation for Tower of Hanoi is T(N)=2T(N-1)+1;
24.
Identify the sorting technique which compares adjacent elements in a list and switches whenever
necessary?
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
Show Explanation
Wrong Answer
Answer - C) The sorting technique is bubble sort.
25.
Among the following options which is the best sorting algorithm when the list is already sorted?
Merge Sort
Insertion Sort
Bubble Sort
Selection Sort
Show Explanation
Wrong Answer
Answer - B) Insertion Sort has a time complexity of O(N) when the array is always sorted.
26.
What is the best case time complexity of the binary search algorithm?
O(1)
O(n)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
Answer - A) The best-case time complexity occurs when the target element occurs exactly at the middle
of the array and the algorithm terminates in 1 move.
27.
Which of the following algorithms are used to find the shortest path from a source node to all other
nodes in a weighted graph?
BFS
Djikstra’s Algorithm
Prims Algorithm
Kruskal’s Algorithm
Show Explanation
Wrong Answer
Answer - B) Djikstra’s algorithm is used to find the shortest path from a source node to all other nodes in
a weighted graph.
28.
Which of the following are applications of Topological Sort of a graph?
Sentence Ordering
Course Scheduling
OS Deadlock Detection
All of the above
Show Explanation
Wrong Answer
Answer - D) All the above options are applicable.
29.
What is the time complexity in decreasing the node value in a binomial heap?
O(1)
O(N)
O(logN)
O(NlogN)
Show Explanation
Wrong Answer
Answer - C) Time complexity and reducing the node value in Binomial heap is O(logN).
30.
An algorithm is __________?
A problem
A procedure for solving a problem
A real-life mathematical problem
None of the above
Show Explanation
Wrong Answer
Answer - B) An algorithm is a procedure for solving a problem.
31.
Which of the following is incorrect? Algorithms can be represented:
As programs
As flow charts
As syntax
As pseudo-codes
Show Explanation
Wrong Answer
Answer - C) Ayurvedam can be represented as Syntax is incorrect.
32.
What is the time complexity to insert an element to the front of a LinkedList(head pointer given)?
O(n)
O(1)
O(logn)
O(n * logn)
Show Explanation
Wrong Answer
Answer - B) We set the next node to the head of the list, and then return that node as the new head.
33.
What should be considered when designing an algorithm?
If this software is used correctly
In the hardware is used correctly
If there is more than one way to solve the problem
All of the above are correct
Show Explanation
Wrong Answer
Answer - C) While designing an algorithm we must check if there is more than one way to solve the
problem.
34.
Which of the following is known to be not an NP-Hard Problem?
Vertex Cover Problem
0/1 Knapsack Problem
Maximal Independent Set Problem
Travelling Salesman Problem
Show Explanation
Wrong Answer
Answer - B) The 0/1 Knapsack is not an NP-Hard problem.
35.
The worst-case time complexity of Selection Exchange Sort is?
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
Answer - D) The worst-case time complexity of Selection Exchange Sort is O(n^2).
36.
Heap is a _____________?
Tree structure
Complete binary tree
Binary tree
None of the above
Show Explanation
Wrong Answer
Answer - B) Heap is a complete binary tree.
37.
What is the maximum number of swaps that can be performed in the Selection Sort algorithm?
n-1
n-2
Show Explanation
Wrong Answer
Answer - A) n - 1 swap are performed at max to sort any array by Selection Sort.
38.
Worst-case time complexity to access an element in a BST can be?
O(n)
O(n * logn)
O(1)
O(logn)
Show Explanation
Wrong Answer
Answer - A) In the worst case, we might need to visit all the nodes in the BST.
39.
In a graph of n nodes and n edges, how many cycles will be present?
Exactly 1
At most 1
At most 2
Depending on the graph
Show Explanation
Wrong Answer
Answer - A) A tree contains by definition n nodes and n - 1 edge, and it is an acyclic graph. When we add
1 edge to the tree, we can form exactly one cycle by adding this edge.
40.
Kruskal’s Algorithm for finding the Minimum Spanning Tree of a graph is a kind of a?
DP Problem
Greedy Algorithm
Adhoc Problem
None of the above
Show Explanation
Wrong Answer
Answer - B) Kruskal’s Algorithm works on the greedy algorithm of taking the lowest weight edges in the
MST of a graph unless it forms a cycle.
41.
Which of the following algorithms are used for string and pattern matching problems??
Z Algorithm
Rabin Karp Algorithm
KMP Algorithm
All of the above
Show Explanation
Wrong Answer
Answer - D) All the above algorithms are used for string and pattern matching.
42.
The time complexity for travel Singh all nodes in a binary search tree with n nodes and printing them in
order is?
O(n)
O(1)
O(nlog2n)
O(n^2)
Show Explanation
Wrong Answer
Answer - A) The time complexity for travel Singh all nodes in a binary search tree with n nodes and
printing them in order is O(n).
43.
Identify the function of the stack that returns the top data element of the stack?
pop()
peek()
push()
findTop()
Show Explanation
Wrong Answer
Answer - B) The function to return the top data element of the stack is peek()
44.
What is the best time complexity we can achieve to precompute all-pairs shortest paths in a weighted
graph?
O(n^3)
O(n^2)
O(n)
O(n^4)
Show Explanation
Wrong Answer
Answer - A) The Floyd-Warshall Algorithm computes All Pairs Shortest Paths in a weighted graph, in
O(n^3).
45.
Which of the following functions provides the maximum asymptotic complexity?
f1(n) = n^(3/2)
f2(n) = n^(logn)
f3(n) = nlogn
f4(n) = 2^n.
Show Explanation
Wrong Answer
Answer - D) f4(n) = 2^n has exponential time complexity which is the maximum.
46.
The time complexity to find the longest common subsequence of two strings of length M and N is?
O(N)
O(M * N)
O(M)
O(log N)
Show Explanation
Wrong Answer
Answer - B) The time complexity to find Longest common subsequence is O(M*N) is using the Dynamic
programming approach.