Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the minimum cuts array with the maximum possible cuts.
DSA C#
int n = s.Length; int[] cuts = new int[n]; for (int i = 0; i < n; i++) { cuts[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i or i - 1 instead of n - 1 causes incorrect initialization.
Setting cuts[i] to 0 assumes no cuts needed initially.
✗ Incorrect
We initialize cuts[i] with the maximum cuts possible, which is n - 1 (cutting between every character).
2fill in blank
mediumComplete the code to mark single characters as palindromes in the DP table.
DSA C#
bool[,] dp = new bool[n, n]; for (int i = 0; i < n; i++) { dp[i, i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i, i] to false causes incorrect palindrome detection.
Using conditions like s[i] == s[i] is redundant.
✗ Incorrect
Every single character is a palindrome, so dp[i, i] should be true.
3fill in blank
hardFix the error in the palindrome check condition inside the nested loops.
DSA C#
for (int length = 2; length <= n; length++) { for (int start = 0; start <= n - length; start++) { int end = start + length - 1; if (s[start] == s[end] && (length == 2 || [1])) { dp[start, end] = true; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[start, end - 1] or dp[start + 1, end] checks wrong substring parts.
Using dp[start, end] causes infinite recursion or wrong results.
✗ Incorrect
For substrings longer than 2, check if the inner substring is palindrome using dp[start + 1, end - 1].
4fill in blank
hardFill both blanks to update the minimum cuts correctly when a palindrome substring is found.
DSA C#
for (int i = 0; i < n; i++) { if (dp[0, i]) { cuts[i] = 0; } else { for (int j = 0; j < i; j++) { if (dp[j + 1, i]) { cuts[i] = Math.Min(cuts[i], cuts[[1]] + [2]); } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j causes wrong indexing.
Adding 0 instead of 1 misses the new cut.
✗ Incorrect
We update cuts[i] by checking cuts[j] + 1 because we cut after position j.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each palindrome substring to its minimum cuts.
DSA C#
var result = new Dictionary<string, int>(); for (int start = 0; start < n; start++) { for (int end = start; end < n; end++) { if (dp[start, end]) { string substring = s.Substring(start, end - start + 1); result[[1]] = cuts[[2]]; } } } return result;
Attempts:
3 left
💡 Hint
Common Mistakes
Using start as key instead of substring.
Using cuts[start] instead of cuts[end].
✗ Incorrect
Use substring as key, cuts[end] as value, and start for clarity in code.
