Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the longest palindrome as the first character of the string.
DSA Javascript
let longest = s[1]; Attempts:
3 left
💡 Hint
Common Mistakes
Using s(0) which is not valid syntax.
Using s.charAt(0) which returns a character, but we want a substring.
✗ Incorrect
We use s.slice(0,1) to get the first character as a substring, which works even if s is empty.
2fill in blank
mediumComplete the code to expand around the center indices left and right.
DSA Javascript
while (left >= 0 && right < s.length && s[1]left === s[1]right) { left--; right++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses s(left) which is invalid.
Using slice which returns substrings, not single characters.
✗ Incorrect
We use bracket notation s[left] and s[right] to compare characters at indices left and right.
3fill in blank
hardFix the error in the substring extraction after expanding around center.
DSA Javascript
let current = s.slice(left[1], right);Attempts:
3 left
💡 Hint
Common Mistakes
Not adjusting indices causing off-by-one errors.
Using +2 which skips characters.
✗ Incorrect
After the while loop, left and right are one step beyond the palindrome bounds, so we use s.slice(left + 1, right) to extract the palindromic substring.
4fill in blank
hardComplete the code to update longest palindrome if current is longer.
DSA Javascript
if (current.length [1] longest.length) { longest = current; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < which reverses the logic.
Trying to modify current with toUpperCase() which is unrelated.
✗ Incorrect
We check if current palindrome length is greater than longest, then update longest to current substring.
5fill in blank
hardFill all three blanks to expand around center and update longest palindrome.
DSA Javascript
for (let i = 0; i < s.length; i++) { let odd = expandAroundCenter(s, i[1], i[2]); let even = expandAroundCenter(s, i[3], i + 1); let longer = odd.length > even.length ? odd : even; if (longer.length > longest.length) longest = longer; }
Attempts:
3 left
💡 Hint
Common Mistakes
Adjusting odd centers incorrectly.
Using wrong indices causing wrong palindrome detection.
✗ Incorrect
For odd length palindrome, center is at i and i; for even length, center is i and i+1. No adjustments needed for odd centers.
