Challenge - 5 Problems
LCS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LCS length calculation
What is the output of the following Java code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA Java
public class LCSLength { public static int lcsLength(String s1, String s2) { int m = s1.length(); int n = s2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } public static void main(String[] args) { String s1 = "AGGTAB"; String s2 = "GXTXAYB"; System.out.println(lcsLength(s1, s2)); } }
Attempts:
2 left
💡 Hint
Trace the characters and count the longest subsequence common to both strings.
✗ Incorrect
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB" which has length 4, but actually the correct LCS length is 5. The LCS is "GTAB" plus one more character 'G' at the start, making it "GTAB" length 4 is incorrect. The code returns 5, so option B is correct.
❓ Predict Output
intermediateOutput of LCS string reconstruction
What is the output of the following Java code that reconstructs and prints the Longest Common Subsequence (LCS) string between two strings?
DSA Java
public class LCSString { public static String lcs(String s1, String s2) { int m = s1.length(); int n = s2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } StringBuilder sb = new StringBuilder(); int i = m, j = n; while (i > 0 && j > 0) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { sb.append(s1.charAt(i - 1)); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } return sb.reverse().toString(); } public static void main(String[] args) { String s1 = "ABCBDAB"; String s2 = "BDCABA"; System.out.println(lcs(s1, s2)); } }
Attempts:
2 left
💡 Hint
Trace back from dp table to find the common subsequence characters.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BDAB". The code reconstructs this sequence correctly by tracing back the dp table.
🔧 Debug
advancedIdentify the error in LCS length calculation
The following Java code is intended to calculate the length of the Longest Common Subsequence (LCS) between two strings. What error will it produce when run?
DSA Java
public class LCSBug { public static int lcsLength(String s1, String s2) { int m = s1.length(); int n = s2.length(); int[][] dp = new int[m][n]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m - 1][n - 1]; } public static void main(String[] args) { System.out.println(lcsLength("ABC", "AC")); } }
Attempts:
2 left
💡 Hint
Check array size and indexing in loops.
✗ Incorrect
The dp array is declared with size m x n, but the loops run from 1 to m and 1 to n inclusive, causing index access at dp[m][j] or dp[i][n], which is out of bounds. This causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedTime complexity of LCS dynamic programming solution
What is the time complexity of the standard dynamic programming solution to find the Longest Common Subsequence (LCS) of two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the nested loops over both strings.
✗ Incorrect
The standard DP solution uses a 2D array of size m+1 by n+1 and fills it with nested loops, resulting in O(m * n) time complexity.
🚀 Application
expertLongest Common Subsequence count of distinct subsequences
Given two strings s1 = "ABCBDAB" and s2 = "BDCABA", how many distinct Longest Common Subsequences (LCS) of maximum length exist?
Attempts:
2 left
💡 Hint
There can be multiple LCS strings of the same maximum length; count distinct ones.
✗ Incorrect
For these strings, the maximum LCS length is 4. The distinct LCS strings of length 4 are "BCAB" and "BDAB", so there are 2 distinct LCS.
