Practice
1. You are given a string and need to count all substrings that read the same forwards and backwards. Which approach guarantees an optimal time and space solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The problem requires counting all palindromic substrings efficiently, not just longest palindromes.Step 2: Evaluate approaches
Brute force is correct but inefficient (O(n³)). LCS DP finds longest palindrome but not count of all substrings. Greedy fails on nested palindromes. Expanding around centers counts all palindromes in O(n²) time and O(1) space.Final Answer:
Option C -> Option CQuick Check:
Expanding around centers covers odd and even length palindromes efficiently [OK]
Hint: Expanding centers counts all palindromes in O(n²) time [OK]
Common Mistakes:
- Confusing longest palindrome with counting all palindromic substrings
2. You are given three strings
s1, s2, and s3. Determine if s3 can be formed by interleaving the characters of s1 and s2 while preserving the order of characters in each string. Which algorithmic approach guarantees an optimal solution for this problem?easy
Solution
Step 1: Understand problem constraints
The problem requires preserving the order of characters from boths1ands2to forms3. This rules out greedy and sorting approaches.Step 2: Identify suitable algorithm
Dynamic Programming with a 2D table tracks whether prefixes ofs1ands2can form prefixes ofs3, guaranteeing an optimal solution.Final Answer:
Option C -> Option CQuick Check:
DP ensures all prefix combinations are checked systematically [OK]
Hint: Interleaving requires DP to track prefix matches [OK]
Common Mistakes:
- Assuming greedy works because characters match sequentially
- Thinking sorting helps preserve order
3. Consider the following Python code implementing wildcard matching using space-optimized DP. What is the value of
prev[3] after processing i=2 (second character of s='abc') and j=3 (third character of p='a*c')?easy
Solution
Step 1: Trace dp arrays for i=2, j=3
At i=2 (s[1]='b'), j=3 (p[2]='c'), since p[j-1] != '*' and not '?', check if s[i-1]==p[j-1]. 'b' != 'c', so curr[3] = False from prev[2].Step 2: Evaluate curr[3]
At j=2, p[1]='*', so curr[2] can be True if curr[1] or prev[2] is True. However, curr[3] depends on prev[2], which is False at this point, so curr[3] remains False.Final Answer:
Option D -> Option DQuick Check:
DP correctly updates curr[3] to False at i=2, j=3 [OK]
Hint: Check character matches and '*' propagation carefully [OK]
Common Mistakes:
- Off-by-one indexing
- Confusing curr and prev arrays
- Ignoring '*' propagation
4. The following code attempts to implement the space-optimized DP for longest common substring. Identify the line containing the subtle bug that causes incorrect results on some inputs.
def longest_common_substring_space_optimized_buggy(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(1, m + 1): # Note: iterating forward
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
medium
Solution
Step 1: Understand dp array update dependency
dp[j] depends on dp[j-1] from previous iteration of inner loop, so iterating forward overwrites dp[j-1] before use.Step 2: Identify iteration order bug
Iterating j forward causes dp[j-1] to be updated before dp[j], corrupting the chain of matching lengths.Final Answer:
Option D -> Option DQuick Check:
Changing inner loop to iterate backward fixes the bug [OK]
Hint: DP update depends on previous dp[j-1], so iterate backward [OK]
Common Mistakes:
- Updating dp forward overwrites needed values
- Incorrect max_len update timing
- Wrong dp initialization size
5. Examine the following buggy code snippet for wildcard matching using space-optimized DP. Which line contains the subtle bug that causes incorrect results on empty strings or patterns?
medium
Solution
Step 1: Analyze initialization of prev array for '*' characters
Line 6 sets prev[j] = True unconditionally when p[j-1] == '*', which incorrectly assumes all prefixes match.Step 2: Correct initialization logic
It should propagate previous state: prev[j] = prev[j - 1], ensuring '*' can match empty sequence only if previous prefix matched.Final Answer:
Option C -> Option CQuick Check:
Incorrect initialization causes false positives on empty strings [OK]
Hint: Initialization must propagate previous True states for '*' [OK]
Common Mistakes:
- Setting prev[j] = True blindly
- Ignoring empty string edge cases
- Confusing '*' handling in initialization
