Challenge - 5 Problems
LCS Mastery Badge
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 C# code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA C#
using System; class Program { static int LCSLength(string s1, string s2) { int m = s1.Length, 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[i - 1] == s2[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]; } static void Main() { string a = "AGGTAB"; string b = "GXTXAYB"; Console.WriteLine(LCSLength(a, b)); } }
Attempts:
2 left
💡 Hint
Trace the matching characters between the two strings and count the longest sequence in order.
✗ Incorrect
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB" which has length 4. The code correctly returns 4, corresponding to option A.
❓ Predict Output
intermediateOutput of LCS string reconstruction
What is the output of the following C# code that reconstructs the Longest Common Subsequence (LCS) string between two strings?
DSA C#
using System; using System.Text; class Program { static string LCS(string s1, string s2) { int m = s1.Length, 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[i - 1] == s2[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 lcs = new StringBuilder(); int x = m, y = n; while (x > 0 && y > 0) { if (s1[x - 1] == s2[y - 1]) { lcs.Insert(0, s1[x - 1]); x--; y--; } else if (dp[x - 1, y] > dp[x, y - 1]) { x--; } else { y--; } } return lcs.ToString(); } static void Main() { string a = "ABCBDAB"; string b = "BDCABA"; Console.WriteLine(LCS(a, b)); } }
Attempts:
2 left
💡 Hint
Follow the dp table from bottom-right to top-left to reconstruct the LCS.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BDAB". The code reconstructs this sequence correctly.
🔧 Debug
advancedIdentify the error in LCS length code
What error does the following C# code produce when trying to calculate the LCS length?
DSA C#
using System; class Program { static int LCSLength(string s1, string s2) { int m = s1.Length, 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[i - 1] == s2[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]; } static void Main() { string a = "ABC"; string b = "AC"; Console.WriteLine(LCSLength(a, b)); } }
Attempts:
2 left
💡 Hint
Check how the dp array is declared and accessed with indices starting at 1.
✗ Incorrect
The dp array is declared with size [m, n], but the loops access dp[i, j] where i and j go up to m and n inclusive, causing index out of range.
🚀 Application
advancedNumber of distinct LCS sequences
Given two strings, how can you modify the LCS dynamic programming approach to count the number of distinct longest common subsequences?
Attempts:
2 left
💡 Hint
Think about tracking counts alongside lengths in the dp table.
✗ Incorrect
To count distinct LCS sequences, maintain a count dp array that updates counts when dp values increase or match, ensuring all distinct sequences are counted.
🧠 Conceptual
expertTime complexity of LCS and optimization
What is the time complexity of the classic dynamic programming solution for the Longest Common Subsequence problem, and which optimization can reduce space complexity without affecting time complexity?
Attempts:
2 left
💡 Hint
Consider the nested loops and how dp table rows can be reused.
✗ Incorrect
The classic LCS solution runs in O(m*n) time. Space can be optimized to O(min(m,n)) by storing only two rows of the dp table at a time.
