LCS is the longest sequence that appears in the same relative order in two sequences but not necessarily contiguously.
Click to reveal answer
beginner
What is the main difference between Longest Common Subsequence and Longest Common Substring?
LCS allows characters to be non-contiguous but in order, while Longest Common Substring requires characters to be contiguous.
Click to reveal answer
intermediate
In LCS dynamic programming, what does the table cell dp[i][j] represent?
dp[i][j] stores the length of the LCS of the first i characters of string1 and first j characters of string2.
Click to reveal answer
intermediate
What is the time complexity of the classic dynamic programming solution for LCS?
The time complexity is O(m*n), where m and n are the lengths of the two input strings.
Click to reveal answer
advanced
How do you reconstruct the LCS string from the dp table after computing lengths?
Start from dp[m][n], move backwards: if characters match, add to LCS and move diagonally; else move in direction of larger dp value.
Click to reveal answer
What does the Longest Common Subsequence represent?
ALongest sequence appearing in order in both strings, not necessarily contiguous
BLongest sequence appearing contiguously in both strings
CShortest sequence common to both strings
DLongest sequence appearing in reverse order in both strings
✗ Incorrect
LCS is the longest sequence that appears in the same order in both strings but characters do not have to be next to each other.
In LCS dynamic programming, if characters at positions i and j match, what is dp[i][j]?
Amax(dp[i-1][j], dp[i][j-1])
Bdp[i-1][j-1] + 1
Cdp[i-1][j-1]
D0
✗ Incorrect
When characters match, we add 1 to the LCS length found by excluding these characters (dp[i-1][j-1] + 1).
What is the base case for the dp table in LCS?
Adp[0][j] = 0 and dp[i][0] = 0 for all i, j
Bdp[0][j] = j and dp[i][0] = i
Cdp[0][j] = 1 and dp[i][0] = 1
Ddp[0][j] = -1 and dp[i][0] = -1
✗ Incorrect
If one string is empty, LCS length is zero, so dp[0][j] and dp[i][0] are zero.
What is the time complexity of the classic LCS dynamic programming solution?
AO(m+n)
BO(m^2)
CO(m*n)
DO(n^2)
✗ Incorrect
The solution fills an m by n table, so time complexity is O(m*n).
Which of these is NOT true about LCS?
ALCS can be used to find similarity between strings
BLCS characters must appear in order in both strings
CLCS length can be zero if no common characters
DLCS characters must be contiguous in both strings
✗ Incorrect
LCS characters do not need to be contiguous, only in order.
Explain how to use dynamic programming to find the Longest Common Subsequence between two strings.
Think about how to build solutions for smaller prefixes of the strings.
You got /4 concepts.
Describe the process to reconstruct the actual LCS string after computing the dp table.
Trace back from the bottom-right corner of the dp table.
You got /4 concepts.
Practice
(1/5)
1. What does the Longest Common Subsequence (LCS) between two strings represent?
easy
A. The longest sequence of characters that appear in both strings in the same order, but not necessarily consecutively.
B. The longest substring that appears in both strings consecutively.
C. The total number of characters in both strings combined.
D. The shortest sequence of characters that can be removed to make the strings identical.
Solution
Step 1: Understand the definition of LCS
The LCS is a sequence that appears in both strings in the same order but not necessarily next to each other.
Step 2: Differentiate from substring and other concepts
Unlike substring, LCS does not require characters to be consecutive. It is about order and presence.
Final Answer:
The longest sequence of characters that appear in both strings in the same order, but not necessarily consecutively. -> Option A
Quick Check:
LCS = longest ordered common sequence [OK]
Hint: LCS is about order, not consecutive characters [OK]
Common Mistakes:
Confusing LCS with longest common substring
Thinking characters must be consecutive
Mixing LCS with string length or edits
2. Which of the following is the correct way to declare a 2D array in C for storing LCS lengths of two strings of length m and n?
easy
A. int lcs[m][n];
B. int lcs[m-1][n-1];
C. int lcs[m+1][n+1];
D. int lcs[m*n];
Solution
Step 1: Understand LCS table size requirements
We need a table of size (m+1) x (n+1) to include empty prefixes for both strings.
Step 2: Check array declaration syntax
int lcs[m+1][n+1]; correctly declares int lcs[m+1][n+1]; which fits the DP table size.
Final Answer:
int lcs[m+1][n+1]; -> Option C
Quick Check:
DP table size = (m+1) x (n+1) [OK]
Hint: Add 1 to lengths for DP table size [OK]
Common Mistakes:
Using m or n instead of m+1 or n+1
Declaring 1D array instead of 2D
Using multiplication instead of 2D array
3. What is the output of the following C code snippet that computes the length of LCS for strings "ABCBDAB" and "BDCABA"?
int main() {
char X[] = "ABCBDAB";
char Y[] = "BDCABA";
int m = 7, n = 6;
int lcs[8][7];
for (int i = 0; i <= m; i++) lcs[i][0] = 0;
for (int j = 0; j <= n; j++) lcs[0][j] = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (X[i-1] == Y[j-1])
lcs[i][j] = lcs[i-1][j-1] + 1;
else
lcs[i][j] = (lcs[i-1][j] > lcs[i][j-1]) ? lcs[i-1][j] : lcs[i][j-1];
}
}
printf("%d", lcs[m][n]);
return 0;
}
medium
A. 6
B. 3
C. 5
D. 4
Solution
Step 1: Understand the DP table filling
The code fills the LCS table using standard DP approach comparing characters of X and Y.
Step 2: Calculate LCS length for given strings
The LCS of "ABCBDAB" and "BDCABA" is "BCBA" or "BDAB" with length 4, but careful tracing shows length 4 is correct. However, the code prints lcs[m][n], which is 4.
Step 3: Re-evaluate LCS length
Actually, the longest common subsequence length is 4 or 5? Let's check carefully: - "BCBA" length 4 - "BDAB" length 4 - But "BCABA" is not a subsequence. Rechecking the classic example, the LCS length is 4. But the code output is 4, so 4 is correct.
Final Answer:
4 -> Option D
Quick Check:
LCS length for given strings = 4 [OK]
Hint: Trace DP table or recall classic LCS example [OK]
Common Mistakes:
Confusing LCS length with substring length
Off-by-one errors in indexing
Misreading the strings or code logic
4. Identify the error in the following C code snippet intended to compute the LCS length of two strings X and Y:
int lcs_length(char *X, char *Y, int m, int n) {
int lcs[m+1][n+1];
for (int i = 0; i <= m; i++) lcs[i][0] = 0;
for (int j = 0; j <= n; j++) lcs[0][j] = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (X[i] == Y[j])
lcs[i][j] = lcs[i-1][j-1] + 1;
else
lcs[i][j] = (lcs[i-1][j] > lcs[i][j-1]) ? lcs[i-1][j] : lcs[i][j-1];
}
}
return lcs[m][n];
}
medium
A. Indexing X[i] and Y[j] should be X[i-1] and Y[j-1].
B. Array size should be lcs[m+1][n+1] instead of lcs[m][n].
C. Initialization loops should start from 1, not 0.
D. Return statement should be lcs[m-1][n-1] instead of lcs[m][n].
Solution
Step 1: Check array size and initialization
The array size (m+1) x (n+1) and initialization loops are correct for the DP table.
Step 2: Check indexing of characters in strings
Strings in C are 0-indexed, but loops run from 1 to m/n, so characters should be accessed as X[i-1] and Y[j-1].
Final Answer:
Indexing X[i] and Y[j] should be X[i-1] and Y[j-1]. -> Option A
Quick Check:
String indexing off by one error [OK]
Hint: Remember C strings start at index 0 [OK]
Common Mistakes:
Using wrong indices for string characters
Ignoring array size for DP table
Incorrect initialization loop ranges
5. Given two strings X = "AGGTAB" and Y = "GXTXAYB", write a C function to print the actual Longest Common Subsequence (LCS) string, not just its length. Which approach correctly reconstructs the LCS from the DP table?
hard
A. Start from lcs[0][0], move right or down to find matching characters and build LCS forward.
B. Start from lcs[m][n], move diagonally up-left if characters match, else move in direction of larger value until reaching lcs[0][0].
C. Use recursion without memoization to print LCS directly from strings.
D. Print characters from X that appear anywhere in Y without order consideration.
Solution
Step 1: Understand LCS reconstruction from DP table
After filling the DP table, start from bottom-right cell lcs[m][n] and move backwards.
Step 2: Move diagonally if characters match, else move towards larger neighbor
If characters match, include that character and move diagonally up-left; else move to the cell with larger value (up or left).
Step 3: Continue until reaching lcs[0][0]
Collect characters in reverse order and then reverse them to get the LCS string.
Final Answer:
Start from lcs[m][n], move diagonally up-left if characters match, else move in direction of larger value until reaching lcs[0][0]. -> Option B
Quick Check:
Backtrack DP table from bottom-right to reconstruct LCS [OK]
Hint: Backtrack DP table from bottom-right to get LCS [OK]
Common Mistakes:
Trying to build LCS forward from top-left
Ignoring order of characters in reconstruction
Printing characters without checking DP table values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.