Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum length of palindrome found so far.
DSA Java
int maxLength = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxLength to 1 initially, which assumes a palindrome exists before checking.
Using negative values which are invalid for length.
✗ Incorrect
We start with maxLength = 0 because initially no palindrome substring is found.
2fill in blank
mediumComplete the code to expand around the center and return the length of palindrome.
DSA Java
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } return [1] - [2] - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using right - left without subtracting 1, resulting in off-by-one error.
Using left - right which gives negative length.
✗ Incorrect
After the loop, left and right are one step beyond the palindrome bounds, so length is right - left - 1.
3fill in blank
hardFix the error in the code to update the start index of the longest palindrome substring.
DSA Java
if (len > maxLength) { start = [1]; maxLength = len; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for start index.
Not subtracting 1 before division causing incorrect start.
✗ Incorrect
The start index is calculated by moving left from center i by half the palindrome length minus one.
4fill in blank
hardFill both blanks to correctly find the longest palindrome by expanding around centers.
DSA Java
for (int i = 0; i < s.length(); i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, [1]); int len = Math.max(len1, [2]); if (len > maxLength) { start = i - (len - 1) / 2; maxLength = len; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 instead of i + 1 for even length center.
Using len1 instead of len2 in Math.max causing wrong length.
✗ Incorrect
We check both odd length (i, i) and even length (i, i+1) palindromes and take the max length.
5fill in blank
hardFill all three blanks to return the longest palindromic substring from the input string.
DSA Java
return s.[1](start, start + [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or indexOf instead of maxLength for substring end.
Using indexOf which returns an integer, not a substring.
✗ Incorrect
We use substring(start, start + maxLength) to extract the longest palindrome substring.
