Challenge - 5 Problems
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of longest palindromic substring length
What is the output of the following C++ code that finds the length of the longest palindromic substring?
DSA C++
int longestPalindromeLength(const std::string& s) { int n = s.size(); if (n == 0) return 0; int maxLen = 1; for (int i = 0; i < n; ++i) { int left = i, right = i; while (left >= 0 && right < n && s[left] == s[right]) { maxLen = std::max(maxLen, right - left + 1); --left; ++right; } left = i; right = i + 1; while (left >= 0 && right < n && s[left] == s[right]) { maxLen = std::max(maxLen, right - left + 1); --left; ++right; } } return maxLen; } #include <iostream> int main() { std::string s = "babad"; std::cout << longestPalindromeLength(s) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check palindromes centered at each character and between characters.
✗ Incorrect
The longest palindromic substrings in "babad" are "bab" and "aba", both length 3.
❓ Predict Output
intermediateOutput of longest palindromic substring
What is the output of this C++ code that returns the longest palindromic substring itself?
DSA C++
std::string longestPalindrome(const std::string& s) {
int n = s.size();
if (n == 0) return "";
int start = 0, maxLen = 1;
for (int i = 0; i < n; ++i) {
int left = i, right = i;
while (left >= 0 && right < n && s[left] == s[right]) {
if (right - left + 1 > maxLen) {
start = left;
maxLen = right - left + 1;
}
--left;
++right;
}
left = i; right = i + 1;
while (left >= 0 && right < n && s[left] == s[right]) {
if (right - left + 1 > maxLen) {
start = left;
maxLen = right - left + 1;
}
--left;
++right;
}
}
return s.substr(start, maxLen);
}
#include <iostream>
int main() {
std::string s = "cbbd";
std::cout << longestPalindrome(s) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Look for the longest substring that reads the same forward and backward.
✗ Incorrect
The longest palindromic substring in "cbbd" is "bb".
🔧 Debug
advancedIdentify the runtime error in palindrome code
What runtime error will this C++ code produce when run with input "abc"?
DSA C++
int longestPalindromeLength(const std::string& s) { int n = s.size(); int maxLen = 1; for (int i = 0; i <= n; ++i) { int left = i, right = i; while (left >= 0 && right < n && s[left] == s[right]) { maxLen = std::max(maxLen, right - left + 1); --left; ++right; } } return maxLen; } #include <iostream> int main() { std::string s = "abc"; std::cout << longestPalindromeLength(s) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The for loop runs with i <= n, so when i == n, s[i] accesses out of range causing runtime error.
🧠 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 how many centers and expansions happen.
✗ Incorrect
There are 2n-1 centers and each expansion can take O(n) in worst case, so total O(n^2).
🚀 Application
expertLongest palindromic substring after one character removal
Given a string, which approach best finds the longest palindromic substring after removing at most one character?
Attempts:
2 left
💡 Hint
Consider how to handle the removal of one character in palindrome checks.
✗ Incorrect
Dynamic programming can be adapted to allow skipping one character while checking substrings, ensuring all cases are covered.
