Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of longest palindrome substring length
What is the output of the following JavaScript code that finds the length of the longest palindromic substring?
DSA Javascript
function longestPalindromeLength(s) {
let maxLen = 1;
for (let i = 0; i < s.length; i++) {
let left = i, right = i;
while (left >= 0 && right < s.length && s[left] === s[right]) {
maxLen = Math.max(maxLen, right - left + 1);
left--;
right++;
}
left = i;
right = i + 1;
while (left >= 0 && right < s.length && s[left] === s[right]) {
maxLen = Math.max(maxLen, right - left + 1);
left--;
right++;
}
}
return maxLen;
}
console.log(longestPalindromeLength("babad"));Attempts:
2 left
💡 Hint
Check palindromes centered at each character and between characters.
✗ Incorrect
The longest palindromic substring in "babad" is either "bab" or "aba", both length 3.
❓ Predict Output
intermediateOutput of longest palindrome substring
What is the output of this code that returns the longest palindromic substring itself?
DSA Javascript
function longestPalindrome(s) {
if (s.length < 1) return "";
let start = 0, end = 0;
for (let i = 0; i < s.length; i++) {
let len1 = expandAroundCenter(s, i, i);
let len2 = expandAroundCenter(s, i, i + 1);
let len = Math.max(len1, len2);
if (len > end - start) {
start = i - Math.floor((len - 1) / 2);
end = i + Math.floor(len / 2);
}
}
return s.substring(start, end + 1);
}
function expandAroundCenter(s, left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
return right - left - 1;
}
console.log(longestPalindrome("cbbd"));Attempts:
2 left
💡 Hint
Look for the longest substring that reads the same forwards and backwards.
✗ Incorrect
The longest palindromic substring in "cbbd" is "bb".
🧠 Conceptual
advancedTime complexity of longest palindromic substring algorithm
What is the time complexity of the expand-around-center approach to find the longest palindromic substring in a string of length n?
Attempts:
2 left
💡 Hint
Consider that for each character you expand outwards checking palindrome.
✗ Incorrect
For each of the n centers, expanding can take up to n steps, resulting in O(n^2) time.
🔧 Debug
advancedIdentify the error in palindrome substring code
What error will this code produce when run?
DSA Javascript
function longestPalindrome(s) {
let maxLen = 0;
let start = 0;
for (let i = 0; i < s.length; i++) {
let left = i, right = i;
while (left >= 0 && right < s.length && s[left] === s[right]) {
if (right - left + 1 > maxLen) {
maxLen = right - left + 1;
start = left;
}
left--;
right++;
}
}
return s.substring(start, start + maxLen);
}
console.log(longestPalindrome(null));Attempts:
2 left
💡 Hint
Check what happens if input is null.
✗ Incorrect
The code tries to access s.length but s is null, causing a TypeError.
🚀 Application
expertLongest palindromic substring in a large string
Given a string of length 100000 consisting of only 'a' characters, what is the length of the longest palindromic substring?
Attempts:
2 left
💡 Hint
All characters are the same, so the entire string is a palindrome.
✗ Incorrect
A string with all identical characters is itself a palindrome, so the longest palindromic substring is the whole string.
