Dynamic Programming · Algorithms
15 questions · asked in 10 of 27 years (2000–2026)
MCQ 12 · NAT 31-mark 4 · 2-mark 11
2000Questions per year2026
Questions 6–10 of 15
Consider two strings A="qpqrr" and B="pqprqrp". Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x + 10y = ___.
Answer: 34
Let be the LCS length of the first i letters of A and the first j letters of B. If the two letters match, ; otherwise it is the larger of the cell above and the cell to the left. Columns are for B = p, q, p, r, q, r, p (j = 1 to 7):
i = 1 (q): 0 1 1 1 1 1 1
i = 2 (qp): 1 1 2 2 2 2 2
i = 3 (qpq): 1 2 2 2 3 3 3
i = 4 (qpqr): 1 2 2 3 3 4 4
i = 5 (qpqrr): 1 2 2 3 3 4 4
The last cell is 4, so x = 4. A has 5 letters and x is less than 5, so no common subsequence has length 5.
Every LCS of length 4 is A with exactly one letter deleted. Try each deletion and check whether the result is a subsequence of B = pqprqrp:
- delete the first q: pqrr, found in B (p, q, r, r)
- delete p: qqrr, not in B (after the two q's only one r is left)
- delete the second q: qprr, found in B
- delete either r: qpqr, found in B (same string both times)
The distinct longest common subsequences are pqrr, qprr and qpqr, so y = 3.
x + 10y = 4 + 10 3 = 34
Answer: 34
i = 1 (q): 0 1 1 1 1 1 1
i = 2 (qp): 1 1 2 2 2 2 2
i = 3 (qpq): 1 2 2 2 3 3 3
i = 4 (qpqr): 1 2 2 3 3 4 4
i = 5 (qpqrr): 1 2 2 3 3 4 4
The last cell is 4, so x = 4. A has 5 letters and x is less than 5, so no common subsequence has length 5.
Every LCS of length 4 is A with exactly one letter deleted. Try each deletion and check whether the result is a subsequence of B = pqprqrp:
- delete the first q: pqrr, found in B (p, q, r, r)
- delete p: qqrr, not in B (after the two q's only one r is left)
- delete the second q: qprr, found in B
- delete either r: qpqr, found in B (same string both times)
The distinct longest common subsequences are pqrr, qprr and qpqr, so y = 3.
x + 10y = 4 + 10 3 = 34
Answer: 34
Four matrices M1, M2, M3 and M4 are dimensions p x q, q x r, r x s and s x t respectively can be multiplied in several ways with different number of total scalar multiplications. For example When multiplied as the total number of scalar multiplications is pqr+rst+prt. When multiplied as , the total number of scalar multiplications is pqr+prs+pst. If p=10, q=100, r=20, s=5 and t=80, then the minimum number of scalar multiplications needed is
Answer: (C)
The matrices are , , , . Multiplying a matrix by a matrix costs scalar multiplications. There are 5 ways to bracket four matrices:
1. : 1010020 + 10205 + 10580 = 20000 + 1000 + 4000 = 25000
2. : 100205 + 101005 + 10580 = 10000 + 5000 + 4000 = 19000
3. : 20000 + 20580 + 102080 = 20000 + 8000 + 16000 = 44000
4. : 10000 + 100580 + 1010080 = 10000 + 40000 + 80000 = 130000
5. : 8000 + 1002080 + 1010080 = 8000 + 160000 + 80000 = 248000
The smallest is 19000, from .
Answer: (C)
1. : 1010020 + 10205 + 10580 = 20000 + 1000 + 4000 = 25000
2. : 100205 + 101005 + 10580 = 10000 + 5000 + 4000 = 19000
3. : 20000 + 20580 + 102080 = 20000 + 8000 + 16000 = 44000
4. : 10000 + 100580 + 1010080 = 10000 + 40000 + 80000 = 130000
5. : 8000 + 1002080 + 1010080 = 8000 + 160000 + 80000 = 248000
The smallest is 19000, from .
Answer: (C)
An algorithm to find the length of the longest monotonically increasing sequence of numbers in an array A[0 :n-1] is given below.
Let Li denote the length of the longest monotonically increasing sequence starting at index i in the array
Initialize .
For all such that
Finally the length of the longest monotonically increasing sequence is .
Which of the following statements is TRUE?
Let Li denote the length of the longest monotonically increasing sequence starting at index i in the array
Initialize .
For all such that
Finally the length of the longest monotonically increasing sequence is .
Which of the following statements is TRUE?
Answer: (A)
Going from right to left, we find by using , which was already computed and stored as a sub-result. This is the dynamic programming approach.
Answer: The algorithm uses dynamic programming paradigm
Answer: The algorithm uses dynamic programming paradigm
The weight of a sequence of real numbers is defined as A subsequence of a sequence is obtained by deleting some elements from the sequence, keeping the order of the remaining elements the same. Let X denote the maximum possible weight of a subsequence of and Y the maximum possible weight of a subsequence of . Then X is equal to
Answer: (B)
Y is the maximum weight of a subsequence of , that is, the sequence without .
A subsequence of either leaves out or keeps it.
Leaves out : it is a subsequence of with the same weights as in Y (there is the first element and has weight 1). The best value is Y.
Keeps : is first, so it adds . Every other chosen element sits one place further along than it would in a subsequence of , so its weight is halved. If those chosen elements have weight w on their own, the total is . This is largest when w is largest, that is , so the best value is .
X is the larger of the two cases: .
Answer: (B)
A subsequence of either leaves out or keeps it.
Leaves out : it is a subsequence of with the same weights as in Y (there is the first element and has weight 1). The best value is Y.
Keeps : is first, so it adds . Every other chosen element sits one place further along than it would in a subsequence of , so its weight is halved. If those chosen elements have weight w on their own, the total is . This is largest when w is largest, that is , so the best value is .
X is the larger of the two cases: .
Answer: (B)
A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n, respectively, with indexes of X and Y starting from 0.
We wish to find the length of the longest common sub-sequence (LCS) of x[m] and Y[n] as l(m,n), where an incomplete recursive definition for the function l(i,j) to compute the length of the LCS of x[m] and Y[n] is given below:
Which one of the following statements would be TRUE regarding the dynamic programming solution for the recursive definition of l(i,j)?
We wish to find the length of the longest common sub-sequence (LCS) of x[m] and Y[n] as l(m,n), where an incomplete recursive definition for the function l(i,j) to compute the length of the LCS of x[m] and Y[n] is given below:
l(i, j) = 0, if either i=0 or j=0
l(i, j) = expr1, if i,j>0 and x [i-1] = Y [j -1]
l(i, j) = expr2, if i,j>0 and x [i-1] != Y [j -1]The values of l(i,j) could be obtained by dynamic programming based on the correct recursive definition of l(i,j) of the form given above, using an array L[M,N], where M = m+1 and N=n+1, such that L[i,j] = l(i,j). Which one of the following statements would be TRUE regarding the dynamic programming solution for the recursive definition of l(i,j)?
Answer: (B)
When the last letters match, so . When they differ, the LCS leaves out or leaves out , so .
So depends only on , and .
- (A) is false. Only the first row and first column (where i = 0 or j = 0) must be 0. Every other cell is filled by the recurrence.
- (B) is true. In row-major order, the cells it needs lie in the previous row or earlier in the same row. In column-major order, they lie in the previous column or earlier in the same column. Both orders work.
- (C) is false, because (B) shows both orders work.
- (D) is false. It asks that comes before if or . For and this would need each to come before the other. The real need is only and .
Answer: (B)
So depends only on , and .
- (A) is false. Only the first row and first column (where i = 0 or j = 0) must be 0. Every other cell is filled by the recurrence.
- (B) is true. In row-major order, the cells it needs lie in the previous row or earlier in the same row. In column-major order, they lie in the previous column or earlier in the same column. Both orders work.
- (C) is false, because (B) shows both orders work.
- (D) is false. It asks that comes before if or . For and this would need each to come before the other. The real need is only and .
Answer: (B)
Keep practising