Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA Java
boolean isPalindrome(String s, int start, int end) {
while (start < end) {
if (s.charAt(start) != s.charAt([1])) {
return false;
}
start++;
end--;
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using start + 1 or end - 1 instead of end index.
Comparing wrong indices causing incorrect palindrome check.
✗ Incorrect
To check palindrome, compare characters at start and end indices.
2fill in blank
mediumComplete the code to initialize the minimum cuts array with maximum cuts possible.
DSA Java
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
Initializing cuts with 0 or n-1 causes wrong minimum cuts calculation.
Using i-1 causes negative index issues.
✗ Incorrect
Initialize cuts[i] with i, meaning max cuts for substring s[0..i].
3fill in blank
hardFix the error in updating minimum cuts inside the nested loop.
DSA Java
for (int j = 0; j <= i; j++) { if (isPalindrome(s, j, i)) { cuts[i] = Math.min(cuts[i], [1]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using cuts[j] + 1 causes off-by-one error.
Using cuts[i] + 1 or cuts[i-1] + 1 causes incorrect updates.
✗ Incorrect
If substring s[j..i] is palindrome, cuts[i] can be updated by cuts[j-1] + 1.
4fill in blank
hardFill both blanks to handle the case when the palindrome starts at index 0.
DSA Java
for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (isPalindrome(s, j, i)) { if (j == 0) { cuts[i] = [1]; } else { cuts[i] = Math.min(cuts[i], cuts[[2]] + 1); } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting cuts[i] to 1 when j=0 causes extra cut.
Using cuts[j] instead of cuts[j-1] causes wrong indexing.
✗ Incorrect
If palindrome starts at 0, no cuts needed (0). Else use cuts[j-1] + 1.
5fill in blank
hardFill all three blanks to complete the palindrome partitioning minimum cuts function.
DSA Java
public int minCut(String s) {
int n = s.length();
int[] cuts = new int[n];
for (int i = 0; i < n; i++) {
cuts[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if ([1](s, j, i)) {
if (j == 0) {
cuts[i] = 0;
} else {
cuts[i] = Math.min(cuts[i], cuts[[2]] + [3]);
}
}
}
}
return cuts[n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function name or wrong indices.
Adding 0 instead of 1 causes incorrect cut count.
✗ Incorrect
Use isPalindrome to check substring, update cuts[i] with cuts[j-1] + 1.
