DAA1
DAA1
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.
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.
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.
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:
With the help of asymptotic notations, we can analyze the time performance of the algorithm.
Theta notation
Omega notation
Big Oh Notation
1.
Network lock
Sorting
Show Explanation
Wrong Answer
Answer - B) Dijkstra’s algorithm is used to solve single source shortest path problems.
2.
String
Boolean
Double
Integer
Show Explanation
Wrong Answer
3.
Greedy algorithm
Dynamic programming
Backtracking
Sorting
Show Explanation
Wrong Answer
4.
The difference between the heights of left and right nodes cannot be more than 1.
Show Explanation
Wrong Answer
Answer - D) All the above options are applicable for an AVL Tree.
5.
File structure
Recursive
Show Explanation
Wrong Answer
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.
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.
Bubble Sort
Selection Sort
Heap Sort
Merge Sort
Show Explanation
Wrong Answer
10.
Array
Queue
Stack
Show Explanation
Wrong Answer
11.
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.
Show Explanation
Wrong Answer
13.
Linear programming
Dynamic Programming
Greedy Technique
Backtracking
Show Explanation
Wrong Answer
14.
NP problem
P class problem
NP-complete problem
N class problem
Show Explanation
Wrong Answer
15.
string s = "scaler";
int n = s.size();
s = s + s[i];
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
17.
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
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?
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.
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
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.
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
Show Explanation
Wrong Answer
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
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
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.
Sentence Ordering
Course Scheduling
OS Deadlock Detection
Show Explanation
Wrong Answer
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
Show Explanation
Wrong Answer
31.
As flow charts
As syntax
As pseudo-codes
Show Explanation
Wrong Answer
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.
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.
Show Explanation
Wrong Answer
35.
O(n)
O(1)
O(log2n)
O(n^2)
Show Explanation
Wrong Answer
36.
Heap is a _____________?
Tree structure
Binary tree
Show Explanation
Wrong Answer
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.
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.
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
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
KMP Algorithm
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.
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.