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 11–15 of 15
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:
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
= expr1, if i,j>0 and x [i-1] = Y [j -1]
= expr2, if i,j>0 and x [i-1] != Y [j -1] Which one of the following options is correct?Answer: (C)
Here is the LCS length of the first i letters of X and the first j letters of Y. The letters compared are and .
If they match, that letter ends an LCS, so . If they differ, the LCS leaves out or leaves out , so . This is option (C).
The others fail:
- (A) : for X = aa and Y = a it gives , but the LCS length is 1.
- (B) : for X = a and Y = a it gives 0, but the LCS length is 1.
- (D) : it uses to define itself.
Answer: (C)
If they match, that letter ends an LCS, so . If they differ, the LCS leaves out or leaves out , so . This is option (C).
The others fail:
- (A) : for X = aa and Y = a it gives , but the LCS length is 1.
- (B) : for X = a and Y = a it gives 0, but the LCS length is 1.
- (D) : it uses to define itself.
Answer: (C)
The subset-sum problem is defined as follows. Given a set of n positive integers,
S={} and positive integer W, is there a subset of S whose elements sum to W? A dynamic program for solving this problem uses a 2-dimensional Boolean array, X, with n rows and W+1 columns. X[i,j], , , is TRUE if and only if there is a subset of {}whose elements sum to j.
Which entry of the array X, if TRUE, implies that there is a subset whose elements sum to W?
Which entry of the array X, if TRUE, implies that there is a subset whose elements sum to W?
Answer: (C)
is TRUE exactly when some subset of the first i elements has sum j. We want a subset of the whole set S with sum W, so we need the row that uses all n elements and the column for the sum W. That entry is .
The other entries do not answer the question:
- uses only .
- is about sum 0, which the empty subset gives; it says nothing about W.
- leaves out and asks for sum n, not W.
Answer: (C)
The other entries do not answer the question:
- uses only .
- is about sum 0, which the empty subset gives; it says nothing about W.
- leaves out and asks for sum n, not W.
Answer: (C)
The subset-sum problem is defined as follows. Given a set of n positive integers,
S={} and positive integer W, is there a subset of S whose elements sum to W? A dynamic program for solving this problem uses a 2-dimensional Boolean array, X, with n rows and W+1 columns. X[i,j], , , is TRUE if and only if there is a subset of {}whose elements sum to j.
Which of the following is valid for and ?
Which of the following is valid for and ?
Answer: (B)
Look at the element when . A subset of with sum j either leaves out or uses it once.
- Leave out : the sum j must come from , which is .
- Use : the rest must give from , which is .
Either case is enough, so we take the OR:
- (A) uses , which may already contain . So it lets be used more than once, which a subset cannot do.
- (C) and (D) use AND, which would need both cases to hold together.
Answer: (B)
- Leave out : the sum j must come from , which is .
- Use : the rest must give from , which is .
Either case is enough, so we take the OR:
- (A) uses , which may already contain . So it lets be used more than once, which a subset cannot do.
- (C) and (D) use AND, which would need both cases to hold together.
Answer: (B)
Suppose that a robot is placed on the Cartesian plane. At each step it is allowed to move either one unit up or one unit right, i.e., if it is at (i,j) then it can move to either (i+1,j) or (i,j+1).
Suppose that the robot is not allowed to traverse the line segment from (4,4) to (5,4). With this constraint, how many distinct paths are there for the robot to reach (10,10) starting from (0,0)?
Suppose that the robot is not allowed to traverse the line segment from (4,4) to (5,4). With this constraint, how many distinct paths are there for the robot to reach (10,10) starting from (0,0)?
Answer: (D)
Every path from (0,0) to (10,10) is an arrangement of 10 moves right (r) and 10 moves up (u). So the number of paths with no restriction is
Now remove the paths that use the segment from (4,4) to (5,4). Such a path has three parts:
- (0,0) to (4,4): 4 moves right and 4 up, so ways.
- The step from (4,4) to (5,4): exactly 1 way.
- (5,4) to (10,10): 5 moves right and 6 up, so ways.
So the number of paths using the segment is .
Allowed paths =
Answer: (D)
Now remove the paths that use the segment from (4,4) to (5,4). Such a path has three parts:
- (0,0) to (4,4): 4 moves right and 4 up, so ways.
- The step from (4,4) to (5,4): exactly 1 way.
- (5,4) to (10,10): 5 moves right and 6 up, so ways.
So the number of paths using the segment is .
Allowed paths =
Answer: (D)
Suppose that a robot is placed on the Cartesian plane. At each step it is allowed to move either one unit up or one unit right, i.e., if it is at (i,j) then it can move to either (i+1,j) or (i,j+1).
How many distinct paths are there for the robot to reach the point (10,10) starting from the initial position (0,0)?
How many distinct paths are there for the robot to reach the point (10,10) starting from the initial position (0,0)?
Answer: (A)
Each path from to has exactly 10 right moves and 10 up moves, 20 moves in all. Every different order of these moves is a different path, and any such order ends at .
A path is fixed by choosing which 10 of the 20 positions are right moves (the other 10 are up moves). The count is .
(B) counts all strings of 20 moves, including those that do not end at . (C) is far too small.
Answer: (A)
A path is fixed by choosing which 10 of the 20 positions are right moves (the other 10 are up moves). The count is .
(B) counts all strings of 20 moves, including those that do not end at . (C) is far too small.
Answer: (A)
Keep practising