Design-And-Analysis-Of-Algorithms (Set 3)
Design-And-Analysis-Of-Algorithms (Set 3)
3 of 5 sets
Explanation:- the modern formulation of floyd-warshall algorithm as three nested for- loops was
proposed by peter ingerman in the year 1962.
203. Using logical operator’s instead arithmetic operators saves time and space.
A. true
B. false
Answer:A
Explanation:- in computers, logical operations on single bit values execute faster than arithmetic
operations on integer words of data.
204. The time taken to compute the transitive closure of a graph is Theta(n2).
A. true
B. false
Answer:B
Explanation:- the time taken to compute the transitive closure of a graph is theta(n3).
205. If a problem can be broken into subproblems which are reused several times,
the problem possesses property.
A. overlapping subproblems
B. optimal substructure
C. memoization
D. greedy
Answer:A
206. When dynamic programming is applied to a problem, it takes far less time as
compared to other methods that don’t take advantage of overlapping subproblems.
A. true
B. false
Answer:A
Explanation:- dynamic programming calculates the value of a subproblem only once, while other
methods that don’t take advantage of the overlapping subproblems property may calculate the value
of the same subproblem several times. so, dynamic programming saves the time of recalculation
and takes far less time as compared to other methods that don’t take advantage of the overlapping
subproblems property.
207. A greedy algorithm can be used to solve all the dynamic programming
problems.
A. true
B. false
Answer:B
Explanation:- a greedy algorithm gives optimal solution for all subproblems, but when these locally
optimal solutions are combined it may not result into a globally optimal solution. hence, a greedy
algorithm cannot be used to solve all the dynamic programming problems.
Explanation:- the top-down approach uses the memoization technique which stores the previously
calculated values. due to this, the time complexity is decreased but the space complexity is
increased.
210. Which of the following problems is NOT solved using dynamic programming?
A. 0/1 knapsack problem
B. matrix chain multiplication problem
C. edit distance problem
D. fractional knapsack problem
Answer:D
Explanation:- the longest common subsequence problem has both, optimal substructure and
overlapping subproblems. hence, dynamic programming should be used the solve this problem.
212. What is the time complexity of the recursive implementation used to find the
nth fibonacci term?
Explanation:- the recurrence relation is given by fibo(n) = fibo(n – 1) + fibo(n – 2). so, the time
complexity is given by:
213. What is the space complexity of the recursive implementation used to find the
nth fibonacci term?
A. o(1)
B. o(n)
C. o(n2)
D. o(n3)
Answer:A
Explanation:- the recursive implementation doesn’t store any values and calculates every value from
scratch. so, the space complexity is o(1).
Explanation:- to calculate the nth term, the for loop runs (n – 1) times and each time a for loop is
run, it takes a constant time.
215. You are given infinite coins of denominations v1, v2, v3,…..,vn and a sum S.
The coin change problem is to find the minimum number of coins required to get
the sum S. This problem can be solved using
A. greedy algorithm
B. dynamic programming
C. divide and conquer
D. backtracking
Answer:B
216. Suppose you have coins of denominations 1,3 and 4. You use a greedy
algorithm, in which you choose the largest denomination coin which is not greater
than the remaining sum. For which of the following sums, will the algorithm
produce an optimal answer?
A. 14
B. 10
C. 6
D. 100
Answer:D
Explanation:- using the greedy algorithm, three coins {4,1,1} will be selected to make a sum of 6.
but, the optimal answer is two coins {3,3}. similarly, four coins {4,4,1,1} will be selected to make a
sum of 10. but, the optimal answer is three coins {4,3,3}. also, five coins {4,4,4,1,1} will be selected
to make a sum of 14. but, the optimal answer is four coins {4,4,3,3}. for a sum of 100, twenty-five
coins {all 4’s} will be selected and the optimal answer is also twenty-five coins {all 4’s}.
217. You are given infinite coins of N denominations v1, v2, v3,…..,vn and a sum S.
The coin change problem is to find the minimum number of coins required to get
the sum S. What is the time complexity of a dynamic programming
implementation used to solve the coin change problem?
A. o(n)
B. o(s)
C. o(n2)
D. o(s*n)
Answer:D
218. You are given infinite coins of denominations 1, 3, 4. What is the minimum
number of coins required to achieve a sum of 7?
A. 1
B. 2
C. 3
D. 4
Answer:B
Explanation:- the divide and conquer algorithm uses a constant space. so, the space complexity is
o(1).
Explanation:- kadane’s algorithm is used to find the maximum sub-array sum for a given array.
222. For which of the following inputs would Kadane’s algorithm produce a
WRONG output?
A. {1,0,-1}
B. {-1,-2,-3}
C. {1,2,3}
D. {0,0,0}
Answer:B
Explanation:- kadane’s algorithm doesn’t work for all negative numbers. so, the answer is {-1,-2,-3}.
Explanation:- the time complexity of kadane’s algorithm is o(n) because there is only one for loop
which scans the entire array exactly once.
Explanation:- kadane’s algorithm uses a constant space. so, the space complexity is
225. The longest increasing subsequence problem is a problem to find the length of
a subsequence from a sequence of array elements such that the subsequence is
sorted in increasing order and it’s length is maximum. This problem can be solved
using
A. recursion
B. dynamic programming
C. brute force
D. recursion, dynamic programming, brute force
Answer:D
Explanation:- the longest increasing subsequence problem can be solved using all of the mentioned
methods.
226. For any given sequence, there will ALWAYS be a unique increasing
subsequence with the longest length.
A. true
B. false
Answer:B
Explanation:- for a given sequence, it is possible that there is more than one subsequence with the
longest length.
Explanation:- brute force, dynamic programming and recursion can be used to solve the rod cutting
problem.
228. Consider the brute force implementation of the rod cutting problem in which
all the possible cuts are found and the maximum value is calculated. What is the
time complexity of this brute force implementation?
A. o(n2)
B. o(n3)
C. o(nlogn)
D. o(2n)
Answer:D
Explanation:- the brute force implementation finds all the possible cuts. this takes o(2n) time.
229. For every rod cutting problem there will be a unique set of pieces that give the
maximum price.
A. true
B. false
Answer:B
Explanation:- consider a rod of length 3. the prices are {2,3,6} for lengths {1,2,3} respectively. the
pieces {1,1,1} and {3} both give the maximum value of 6.
230. For a given array, there can be multiple ways to reach the end of the array
using minimum number of jumps.
A. true
B. false
Answer:A
231. For any array, given that at most one element is non-zero, it is ALWAYS
possible to reach the end of the array using minimum jumps.
A. true
B. false
Answer:B
Explanation:- the space complexity of the above dynamic programming implementation is o(n).
234. Which of the following problems is equivalent to the 0-1 Knapsack problem?
A. you are given a bag that can carry a maximum weight of w. you are given n items which have a
weight of {w1, w2, w3,…., wn} and a value of {v1, v2, v3,…., vn}. you can break the items into
smaller pieces. choose the items in such a way that you get the maximum value
B. you are studying for an exam and you have to study n questions. the questions take {t1, t2,
t3,…., tn} time(in hours) and carry {m1, m2, m3,…., mn} marks. you can study for a maximum of t
hours. you can either study a question or leave it. choose the questions in such a way that your
score is maximized
Explanation:- in this case, questions are used instead of items. each question has a score which is
same as each item having a value.
235. The 0-1 Knapsack problem can be solved using Greedy algorithm.
A. true
B. false
Answer:B
Explanation:- the knapsack problem cannot be solved using the greedy algorithm.
236. Which of the following methods can be used to solve the matrix chain
multiplication problem?
A. dynamic programming
B. brute force
C. recursion
D. dynamic programming, brute force, recursion
Answer:D
Explanation:- dynamic programming, brute force, recursion methods can be used to solve the matrix
chain multiplication problem.
Explanation:- the minimum number of multiplications are 18000. this is the case when the matrices
are parenthesized as (p*q)*r.
Explanation:- the time complexity of finding all the possible ways of multiplying a set of n matrices is
given by (n-1)th catalan number which is exponential.
239. Which of the following methods can be used to solve the longest common
subsequence problem?
A. recursion
B. dynamic programming
C. both recursion and dynamic programming
D. greedy algorithm
Answer:C
Explanation:- both recursion and dynamic programming can be used to solve the longest
subsequence problem.
241. Which of the following problems can be solved using the longest subsequence
problem?
A. longest increasing subsequence
B. longest palindromic subsequence
C. longest bitonic subsequence
D. longest decreasing subsequence
Explanation:- to find the longest palindromic subsequence in a given string, reverse the given string
and then find the longest common subsequence in the given string and the reversed string.
243. What is the time complexity of the brute force algorithm used to find the
longest common subsequence?
A. o(n)
B. o(n2)
C. o(n3)
D. o(2n)
Answer:D
Explanation:- the time complexity of the brute force algorithm used to find the longest common
subsequence is o(2n).
244. Which of the following is the longest common subsequence between the strings
“hbcfgmnapq” and “cbhgrsfnmq” ?
A. hgmq
B. cfnq
C. bfmq
D. fgmna
Answer:D
Explanation:- the length of the longest common subsequence is 4. but ‘fgmna’ is not the longest
common subsequence as its length is 5.
245. Which of the following methods can be used to solve the longest palindromic
subsequence problem?
A. dynamic programming
B. recursion
Explanation:- dynamic programming, recursion, brute force can be used to solve the longest
palindromic subsequence problem.
247. For which of the following, the length of the string is not equal to the length of
the longest palindromic subsequence?
A. a string that is a palindrome
B. a string of length one
C. a string that has all the same letters(e.g. aaaaaa)
D. some strings of length two
Answer:D
248. What is the length of the longest palindromic subsequence for the string
“ababcdabba”?
A. 6
B. 7
C. 8
D. 9
Answer:B
249. What is the time complexity of the brute force algorithm used to find the
length of the longest palindromic subsequence?
A. o(1)
Explanation:- in the brute force algorithm, all the subsequences are found and the length of the
longest palindromic subsequence is calculated. this takes exponential time.
250. For every non-empty string, the length of the longest palindromic subsequence
is at least one.
A. true
B. false
Answer:A
Explanation:- a single character of any string can always be considered as a palindrome and its
length is one.
252. Which of the following methods can be used to solve the edit distance
problem?
A. recursion
B. dynamic programming
C. both dynamic programming and recursion
D. greedy algorithm
Answer:C
Explanation:- both dynamic programming and recursion can be used to solve the edit distance
problem.
253. The edit distance satisfies the axioms of a metric when the costs are non-
negative.
A. true
Explanation:- d(s,s) = 0, since each string can be transformed into itself without any change. d(s1,
s2) > 0 when s1 != s2, since the transformation would require at least one operation.
254. Suppose each edit (insert, delete, replace) has a cost of one. Then, the
maximum edit distance cost between the two strings is equal to the length of the
larger string.
A. true
B. false
Answer:A
Explanation:- consider the strings “abcd” and “efghi”. the string “efghi” can be converted to “abcd” by
deleting “i” and converting “efgh” to “abcd”. the cost of transformation is 5, which is equal to the
length of the larger string.
255. Consider the strings “monday” and “tuesday”. What is the edit distance
between the two strings?
A. 3
B. 4
C. 5
D. 6
Answer:B
Explanation:- “monday” can be converted to “tuesday” by replacing “m” with “t”, “o” with “u”, “n” with
“e” and inserting “s” at the appropriate position. so, the edit distance is 4.
256. Consider the two strings “”(empty string) and “abcd”. What is the edit
distance between the two strings?
A. 0
B. 4
C. 2
D. 3
Answer:B
Explanation:- the empty string can be transformed into “abcd” by inserting “a”, “b”, “c” and “d” at
appropriate positions. thus, the edit distance is 4.
259. Which of the following methods can be used to find the nth Catalan number?
A. recursion
B. binomial coefficients
C. dynamic programming
D. recursion, binomial coefficients, dynamic programming
Answer:D
Explanation:- all of the mentioned methods can be used to find the nth catalan number.
260. Which of the following implementations of Catalan numbers has the smallest
time complexity?
A. dynamic programming
B. binomial coefficients
C. recursion
D. all have equal time complexity
Answer:B
Explanation:- all of the above mentioned methods can be used to solve the assembly line
scheduling problem.
262. What is the time complexity of the brute force algorithm used to solve the
assembly line scheduling problem?
A. o(1)
B. o(n)
C. o(n2)
D. o(2n)
Answer:D
Explanation:- in the brute force algorithm, all the possible ways are calculated which are of the order
of 2n.
Explanation:- in the dynamic programming implementation of the assembly line scheduling problem,
2 lookup tables are required one for storing the minimum time and the other for storing the assembly
line number.
Explanation:- the time complexity of the above dynamic programming implementation of the
assembly line scheduling problem is o(n).
265. Given a string, you have to find the minimum number of characters to be
inserted in the string so that the string becomes a palindrome. Which of the
following methods can be used to solve the problem?
A. greedy algorithm
B. recursion
C. dynamic programming
D. both recursion and dynamic programming
Answer:D
Explanation:- dynamic programming and recursion can be used to solve the problem.
Explanation:- in string of length one, string with all same characters and a palindromic string the no
of insertions is zero since the strings are already palindromes. to convert a non-palindromic string to
a palindromic string, the minimum length of string to be added is 1 which is greater than all the other
above cases. hence the minimum no of insertions to form palindrome is maximum in non-
palindromic strings.
267. In the worst case, the minimum number of insertions to be made to convert
the string into a palindrome is equal to the length of the string.
A. true
B. false
C. answer: b
D. explanation: in the worst case, the minimum number of insertions to be made to convert the
string into a palindrome is equal to length of the string minus one. for example, consider the string
“abc”. the string can be converted to “abcba” by inserting “a” and “b”. the number of insertions is
Explanation:- the string can be converted to “efgfe” by inserting “f” or to “egfge” by inserting “g”.
thus, only one insertion is required.
268. Consider the string “abbccbba”. What is the minimum number of insertions
required to make the string a palindrome?
A. 0
B. 1
C. 2
D. 3
Answer:A
Explanation:- the given string is already a palindrome. so, no insertions are required.
269. Which of the following problems can be used to solve the minimum number of
insertions to form a palindrome problem?
A. minimum number of jumps problem
B. longest common subsequence problem
C. coin change problem
D. knapsack problems
Answer:B
Explanation:- a variation of longest common subsequence can be used to solve the minimum
number of insertions to form a palindrome problem.
270. Given a 2D matrix, find a submatrix that has the maximum sum. Which of the
following methods can be used to solve this problem?
A. brute force
B. recursion
C. dynamic programming
D. brute force, recursion, dynamic programming
Answer:D
Explanation:- brute force, recursion and dynamic programming can be used to find the submatrix
that has the maximum sum.
271. In which of the following cases, the maximum sum rectangle is the 2D matrix
itself?
Explanation:- when all the elements of a matrix are positive, the maximum sum rectangle is the 2d
matrix itself.
272. Consider the 2×3 matrix {{1,2,3},{1,2,3}}. What is the sum of elements of the
maximum sum rectangle?
A. 3
B. 6
C. 12
D. 18
Answer:C
Explanation:- since all the elements of the 2×3 matrix are positive, the maximum sum rectangle is
the matrix itself and the sum of elements is 12.
273. Consider the 2×2 matrix {{-1,-2},{-3,-4}}. What is the sum of elements of the
maximum sum rectangle?
A. 0
B. -1
C. -7
D. -12
Answer:B
Explanation:- since all the elements of the 2×2 matrix are negative, the maximum sum rectangle is {-
1}, a 1×1 matrix containing the largest element. the sum of elements of the maximum sum rectangle
is -1.
275. Given an array, check if the array can be divided into two subsets such that
the sum of elements of the two subsets is equal. This is the balanced partition
problem. Which of the following methods can be used to solve the balanced
partition problem?
A. dynamic programming
B. recursion
C. brute force
D. dynamic programming, recursion, brute force
Answer:D
Explanation:- all of the mentioned methods can be used to solve the balanced partition
276. In which of the following cases, it is not possible to have two subsets with equal
sum?
A. when the number of elements is odd
B. when the number of elements is even
C. when the sum of elements is odd
D. when the sum of elements is even
Answer:C
Explanation:- when the sum of all the elements is odd, it is not possible to have two subsets with
equal sum.
277. What is the time complexity of the brute force algorithm used to solve the
balanced partition problem?
A. o(1)
B. o(n)
C. o(n2)
D. o(2n)
Answer:D
Explanation:- in the brute force implementation, all the possible subsets will be formed. this takes
exponential time.
278. What is the sum of each of the balanced partitions for the array {5, 6, 7, 10, 3,
1}?
A. 16
Explanation:- the sum of all the elements of the array is 32. so, the sum of all the elements of each
partition should be 16.
279. You are given n dice each having f faces. You have to find the number of ways
in which a sum of S can be achieved. This is the dice throw problem. Which of the
following methods can be used to solve the dice throw problem?
A. brute force
B. recursion
C. dynamic programming
D. brute force, recursion and dynamic programming
Answer:D
Explanation:- brute force, recursion and dynamic programming can be used to solve the dice throw
problem.
280. You have n dice each having f faces. What is the number of permutations that
can be obtained when you roll the n dice together?
A. n*n*n…f times
B. f*f*f…n times
C. n*n*n…n times
D. f*f*f…f times
Answer:B
Explanation:- each die can take f values and there are n dice. so, the total number of permutations
is f*f*f…n times.
281. You have 3 dice each having 6 faces. What is the number of permutations that
can be obtained when you roll the 3 dice together?
A. 27
B. 36
C. 216
D. 81
Answer:C
Explanation:- the sum of 11 can be achieved when the dice show {6, 5} or {5, 6}.
283. There are n dice with f faces. The faces are numbered from 1 to f. What is the
minimum possible sum that can be obtained when the n dice are rolled together?
A. 1
B. f
C. n
D. n*f
Answer:C
Explanation:- the sum will be minimum when all the faces show a value 1. the sum in this case will
be n.
284. There are n dice with f faces. The faces are numbered from 1 to f. What is the
maximum possible sum that can be obtained when the n dice are rolled together?
A. 1
B. f*f
C. n*n
D. n*f
Answer:D
Explanation:- the sum will be maximum when all the faces show a value f. the sum in this case will
be n*f.
285. There are 10 dice having 5 faces. The faces are numbered from 1 to 5. What is
the number of ways in which a sum of 4 can be achieved?
A. 0
B. 2
C. 4
D. 8
Answer:A
286. Consider the expression T & F ? T. What is the number of ways in which the
expression can be parenthesized so that the output is T (true)?
A. 0
B. 1
C. 2
D. 3
Answer:C
Explanation:- the expression can be parenthesized as (t & f) ? t or t & (f ? t), so that the output is t.
287. Which of the following gives the total number of ways of parenthesizing an
expression with n + 1 terms?
A. n factorial
B. n square
C. n cube
D. nth catalan number
Answer:D
288. What is the maximum number of ways in which a boolean expression with n +
1 terms can be parenthesized, such that the output is true?
A. nth catalan number
B. n factorial
C. n cube
D. n square
Answer:A
Explanation:- the number of ways will be maximum when all the possible
290. Consider the graph shown below. Which of the following are the edges in the
MST of the given graph?
A. (a-c)(c-d)(d-b)(d-b)
B. (c-a)(a-d)(d-b)(d-e)
C. (a-d)(d-c)(d-b)(d-e)
D. (c-a)(a-d)(d-c)(d-b)(d-e)
Answer:C
Explanation:- the minimum spanning tree of the given graph is shown below. it has cost 56.
Explanation:- greedy algorithm is used to solve this problem. we first sort items according to their
value/weight ratio and then add item with highest ratio until we cannot add the next item as a whole.
at the end, we add the next item as much as we can.
Explanation:- the objective is to fill the knapsack of some given volume with different materials such
that the value of selected items is maximized.
294. Which of the following statement about 0/1 knapsack and fractional knapsack
problem is correct?
A. in 0/1 knapsack problem items are divisible and in fractional knapsack items are indivisible
B. both are the same
C. 0/1 knapsack is solved using a greedy algorithm and fractional knapsack is solved using
dynamic programming
D. in 0/1 knapsack problem items are indivisible and in fractional knapsack items are divisible
Answer:D
Explanation:- in fractional knapsack problem we can partially include an item into the knapsack
whereas in 0/1 knapsack we have to either include or exclude the item wholly.
Explanation:- as the main time taking a step is of sorting so it defines the time complexity of our
code. so the time complexity will be o(n log n) if we use quick sort for sorting.
Explanation:- it is possible to solve the problem in o(n) time by adapting the algorithm for finding
weighted medians.
298. The result of the fractional knapsack is greater than or equal to 0/1 knapsack.
A. true
B. false
Answer:A
Explanation:- as fractional knapsack gives extra liberty to include the object partially which is not
possible with 0/1 knapsack, thus
Explanation:- the main time taking step is to sort the items according to their value/weight ratio. it
defines the time complexity of the code.
300. Find the maximum value output assuming items to be divisible and
nondivisible respectively.
A. 100, 80
B. 110, 70
C. 130, 110
D. 110, 80
Answer:D
Explanation:- assuming items to be divisible- the value/weight ratio are {3, 2, 4}.so we include third
and first items wholly. so, now only 15 units of volume are left for second item. so we include it
partially.