Practice
1. Consider the following Python function implementing a space-optimized DP for longest common substring. What is the returned value when called with s1 = 'abc' and s2 = 'xbc'?
def longest_common_substring_space_optimized(s1, s2):
n, m = len(s1), len(s2)
dp = [0] * (m + 1)
max_len = 0
for i in range(1, n + 1):
for j in range(m, 0, -1):
if s1[i - 1] == s2[j - 1]:
dp[j] = dp[j - 1] + 1
if dp[j] > max_len:
max_len = dp[j]
else:
dp[j] = 0
return max_len
easy
Solution
Step 1: Trace dp array updates for i=1 (s1[0]='a')
Compare 'a' with 'c','b','x' in s2 backwards: no matches, dp remains zeros.Step 2: Trace dp updates for i=2 (s1[1]='b') and i=3 (s1[2]='c')
At i=2, j=2 matches 'b' == 'b', dp[2] = dp[1]+1=1; max_len=1. At i=3, j=3 matches 'c' == 'c', dp[3] = dp[2]+1=2; max_len=2.Final Answer:
Option A -> Option AQuick Check:
Longest common substring is 'bc' length 2 [OK]
Hint: Trace dp updates backward to avoid overwriting [OK]
Common Mistakes:
- Iterating dp forward overwrites needed values
- Off-by-one indexing errors
- Confusing max_len update timing
2. You are given two strings and need to find the shortest string that contains both as subsequences. Which algorithmic approach guarantees finding this shortest common supersequence efficiently?
easy
Solution
Step 1: Understand the problem requires minimal length supersequence containing both strings as subsequences
Greedy or sorting approaches fail because they do not consider subsequence order constraints.Step 2: Recognize that longest common subsequence (LCS) helps identify overlapping parts to minimize duplication
Dynamic programming using LCS length allows building the shortest common supersequence efficiently.Final Answer:
Option B -> Option BQuick Check:
LCS-based DP is the classic and optimal approach for shortest common supersequence [OK]
Hint: Shortest common supersequence uses LCS-based DP [OK]
Common Mistakes:
- Thinking greedy merging always works
- Confusing substring with subsequence
- Assuming sorting helps
3. Consider the following buggy code for the space-optimized edit distance. Which line contains the subtle bug that causes incorrect results for empty strings or boundary cases?
def minDistance(word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
dp = list(range(n + 1))
for i in range(1, m + 1):
prev = dp[0]
dp[0] = 0 # Bug here
for j in range(1, n + 1):
temp = dp[j]
if word1[i - 1] == word2[j - 1]:
dp[j] = prev
else:
dp[j] = min(dp[j] + 1, dp[j - 1] + 1, prev + 1)
prev = temp
return dp[n]medium
Solution
Step 1: Understand dp[0] initialization role
dp[0] represents cost of deleting all characters from word1 prefix to match empty word2 prefix, so it must be i, not 0.Step 2: Identify incorrect dp[0] reset
Setting dp[0] = 0 inside loop resets deletion cost incorrectly, breaking base case for empty prefixes.Final Answer:
Option C -> Option CQuick Check:
Correct dp[0] update is dp[0] = i [OK]
Hint: dp[0] must track deletions, so dp[0] = i each iteration [OK]
Common Mistakes:
- Resetting dp[0] to zero
- Confusing prev and temp
- Misindexing dp array
4. The following code attempts to implement the space-optimized DP for maximum uncrossed lines. Which line contains a subtle bug that can cause incorrect results?
medium
Solution
Step 1: Understand role of prev variable
In space-optimized DP, dp[j] depends on prev (dp[j-1] from previous iteration), not dp[j-1] of current iteration.Step 2: Identify incorrect dp update
Line 9 uses dp[j - 1] from current iteration, which may have been updated already, causing incorrect results. It should use prev instead.Final Answer:
Option D -> Option DQuick Check:
Using dp[j-1] instead of prev breaks correctness in space-optimized DP [OK]
Hint: Use prev to avoid overwriting dp[j-1] before use [OK]
Common Mistakes:
- Using dp[j-1] instead of prev in update
- Not preserving previous dp[j] value before update
5. Suppose you want to count palindromic substrings in a string where characters can be replaced any number of times (reusable characters). Which modification to the expand around center approach correctly handles this variant?
hard
Solution
Step 1: Understand the variant
Allowing character replacements means palindrome checks must consider substitutions, not exact matches.Step 2: Evaluate approaches
Expand around center relies on exact matches; skipping mismatches breaks palindrome definition. Precomputing all variants is exponential. DP with memoization can track allowed replacements efficiently.Final Answer:
Option D -> Option DQuick Check:
DP can handle flexible palindrome definitions with substitutions [OK]
Hint: Exact match expansion fails when replacements allowed [OK]
Common Mistakes:
- Assuming original approach works without changes
