Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of longest palindromic substring length
What is the output of this C code that finds the length of the longest palindromic substring in the string "babad"?
DSA C
#include <stdio.h> #include <string.h> int longestPalindromeLength(char* s) { int n = strlen(s); int maxLen = 1; int start = 0; 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) { maxLen = right - left + 1; start = left; } left--; right++; } left = i; right = i + 1; while (left >= 0 && right < n && s[left] == s[right]) { if (right - left + 1 > maxLen) { maxLen = right - left + 1; start = left; } left--; right++; } } return maxLen; } int main() { char s[] = "babad"; printf("%d\n", longestPalindromeLength(s)); 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.
🧠 Conceptual
intermediate1:30remaining
Understanding palindrome expansion
In the approach to find the longest palindromic substring by expanding around centers, why do we check two centers for each index?
Attempts:
2 left
💡 Hint
Think about palindrome lengths like 'aba' vs 'abba'.
✗ Incorrect
Odd length palindromes have a single center character, even length palindromes have two center characters.
🔧 Debug
advanced2:00remaining
Identify the error in palindrome substring code
What error will this code produce when run, and why?
char* longestPalindrome(char* s) {
int n = strlen(s);
int maxLen = 1, start = 0;
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) {
maxLen = right - left + 1;
start = left;
}
left--;
right++;
}
}
s[start + maxLen] = '\0';
return s + start;
}
int main() {
char s[] = "cbbd";
printf("%s\n", longestPalindrome(s));
return 0;
}
Attempts:
2 left
💡 Hint
Look at how the string is modified before returning.
✗ Incorrect
The code inserts a null character inside the original string to truncate it, which changes the input string unexpectedly.
❓ Predict Output
advanced1:30remaining
Output of Manacher's algorithm partial array
Given the input string "abacdfgdcaba", what is the value of the palindrome radius array P at index 5 after running Manacher's algorithm?
DSA C
/* Manacher's algorithm partial snippet */ int P[] = {0,1,0,3,0,1,0,1,0,3,0,1,0}; // Index 5 corresponds to character 'f' in the string // What is P[5]?
Attempts:
2 left
💡 Hint
P[i] stores the radius of palindrome centered at i including i.
✗ Incorrect
At index 5 ('f'), the palindrome radius is 1 meaning only the character itself is palindrome.
🚀 Application
expert2:30remaining
Longest palindromic substring in a large string
You have a string of length 1,000,000 characters. Which approach is best to find the longest palindromic substring efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity for very large input.
✗ Incorrect
Manacher's algorithm finds longest palindromic substring in linear time, suitable for large strings.
