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 this Go code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA Go
package main import "fmt" func lcsLength(a, b string) int { m, n := len(a), len(b) dp := make([][]int, m+1) for i := range dp { dp[i] = make([]int, n+1) } for i := 1; i <= m; i++ { for j := 1; j <= n; j++ { if a[i-1] == b[j-1] { dp[i][j] = dp[i-1][j-1] + 1 } else { if dp[i-1][j] > dp[i][j-1] { dp[i][j] = dp[i-1][j] } else { dp[i][j] = dp[i][j-1] } } } } return dp[m][n] } func main() { a := "AGGTAB" b := "GXTXAYB" fmt.Println(lcsLength(a, b)) }
Attempts:
2 left
💡 Hint
Trace the matching characters between the two strings and count the longest sequence.
✗ Incorrect
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB" which has length 4.
❓ Predict Output
intermediateOutput of LCS sequence reconstruction
What is the output of this Go code that reconstructs and prints the Longest Common Subsequence (LCS) string between two strings?
DSA Go
package main import "fmt" func lcsSequence(a, b string) string { m, n := len(a), len(b) dp := make([][]int, m+1) for i := range dp { dp[i] = make([]int, n+1) } for i := 1; i <= m; i++ { for j := 1; j <= n; j++ { if a[i-1] == b[j-1] { dp[i][j] = dp[i-1][j-1] + 1 } else { if dp[i-1][j] > dp[i][j-1] { dp[i][j] = dp[i-1][j] } else { dp[i][j] = dp[i][j-1] } } } } i, j := m, n lcs := []byte{} for i > 0 && j > 0 { if a[i-1] == b[j-1] { lcs = append([]byte{a[i-1]}, lcs...) i-- j-- } else if dp[i-1][j] > dp[i][j-1] { i-- } else { j-- } } return string(lcs) } func main() { a := "ABCBDAB" b := "BDCABA" fmt.Println(lcsSequence(a, b)) }
Attempts:
2 left
💡 Hint
Follow the dp table backwards to find matching characters.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BDAB".
The code reconstructs this sequence by moving backwards through the dp table.
🔧 Debug
advancedIdentify the bug in LCS length calculation
This Go code is intended to calculate the length of the Longest Common Subsequence (LCS) between two strings. What is the bug causing incorrect results?
DSA Go
package main import "fmt" func lcsLength(a, b string) int { m, n := len(a), len(b) dp := make([][]int, m) for i := range dp { dp[i] = make([]int, n) } for i := 0; i < m; i++ { for j := 0; j < n; j++ { if a[i] == b[j] { if i > 0 && j > 0 { dp[i][j] = dp[i-1][j-1] + 1 } else { dp[i][j] = 1 } } else { if i > 0 && j > 0 { if dp[i-1][j] > dp[i][j-1] { dp[i][j] = dp[i-1][j] } else { dp[i][j] = dp[i][j-1] } } else if i > 0 { dp[i][j] = dp[i-1][j] } else if j > 0 { dp[i][j] = dp[i][j-1] } } } } return dp[m-1][n-1] } func main() { a := "ABC" b := "AC" fmt.Println(lcsLength(a, b)) }
Attempts:
2 left
💡 Hint
Check how dp array is sized and accessed with indices.
✗ Incorrect
The dp array should have size (m+1) x (n+1) to safely access dp[i-1][j-1] when i or j is 1.
Current code uses m x n which causes index out of range or incorrect values.
🧠 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 table of size m by n and fills each cell once.
Thus, time complexity is O(m * n).
🚀 Application
expertNumber of distinct LCS sequences
Given two strings, the code below calculates the number of distinct Longest Common Subsequences (LCS) they have. What is the output of this code for strings "ABCBDAB" and "BDCABA"?
DSA Go
package main import "fmt" func countLCS(a, b string) int { m, n := len(a), len(b) dp := make([][]int, m+1) count := make([][]int, m+1) for i := range dp { dp[i] = make([]int, n+1) count[i] = make([]int, n+1) for j := range count[i] { count[i][j] = 1 } } for i := 1; i <= m; i++ { for j := 1; j <= n; j++ { if a[i-1] == b[j-1] { dp[i][j] = dp[i-1][j-1] + 1 count[i][j] = count[i-1][j-1] } else { if dp[i-1][j] > dp[i][j-1] { dp[i][j] = dp[i-1][j] count[i][j] = count[i-1][j] } else if dp[i][j-1] > dp[i-1][j] { dp[i][j] = dp[i][j-1] count[i][j] = count[i][j-1] } else { dp[i][j] = dp[i-1][j] count[i][j] = count[i-1][j] + count[i][j-1] } } } } return count[m][n] } func main() { a := "ABCBDAB" b := "BDCABA" fmt.Println(countLCS(a, b)) }
Attempts:
2 left
💡 Hint
Count how many different sequences have the maximum length.
✗ Incorrect
The longest common subsequence length is 4.
There are 5 distinct LCS sequences of length 4 between "ABCBDAB" and "BDCABA".
