Challenge - 5 Problems
Palindrome Master
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 Go code that finds the length of the longest palindromic substring?
DSA Go
package main import "fmt" func longestPalindromeLength(s string) int { n := len(s) if n == 0 { return 0 } maxLen := 1 for i := 0; i < n; i++ { // Odd length palindrome l, r := i, i for l >= 0 && r < n && s[l] == s[r] { length := r - l + 1 if length > maxLen { maxLen = length } l-- r++ } // Even length palindrome l, r = i, i+1 for l >= 0 && r < n && s[l] == s[r] { length := r - l + 1 if length > maxLen { maxLen = length } l-- r++ } } return maxLen } func main() { fmt.Println(longestPalindromeLength("babad")) }
Attempts:
2 left
💡 Hint
Check palindromes like "bab" or "aba" inside the string.
✗ Incorrect
The longest palindromic substrings in "babad" are "bab" and "aba", both length 3.
🧠 Conceptual
intermediateUnderstanding palindrome expansion technique
Why does the algorithm check both odd and even length palindromes separately when finding the longest palindromic substring?
Attempts:
2 left
💡 Hint
Think about palindrome centers and symmetry.
✗ Incorrect
Odd length palindromes have one center character, even length palindromes have two center characters. Both must be checked to find all palindromes.
❓ Predict Output
advancedOutput of longest palindromic substring
What is the output of this Go program that returns the longest palindromic substring itself?
DSA Go
package main import "fmt" func longestPalindrome(s string) string { n := len(s) if n == 0 { return "" } start, maxLen := 0, 1 for i := 0; i < n; i++ { // Odd length l, r := i, i for l >= 0 && r < n && s[l] == s[r] { if r-l+1 > maxLen { start = l maxLen = r - l + 1 } l-- r++ } // Even length l, r = i, i+1 for l >= 0 && r < n && s[l] == s[r] { if r-l+1 > maxLen { start = l maxLen = r - l + 1 } l-- r++ } } return s[start : start+maxLen] } func main() { fmt.Println(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".
🔧 Debug
advancedIdentify the error in palindrome substring code
What error will this Go code produce when run?
DSA Go
package main import "fmt" func longestPalindrome(s string) string { n := len(s) start, maxLen := 0, 1 for i := 0; i < n; i++ { l, r := i, i for l >= 0 && r < n && s[l] == s[r] { if r-l+1 > maxLen { start = l maxLen = r - l + 1 } l-- r++ } } return s[start : start+maxLen] } func main() { fmt.Println(longestPalindrome("")) }
Attempts:
2 left
💡 Hint
What happens when input string is empty and you slice it?
✗ Incorrect
When input is empty, maxLen is 1 but slicing s[0:1] causes out of range error.
🚀 Application
expertLongest palindromic substring count in a large string
Given a string of length 1000 consisting of random letters, which approach is best to find the longest palindromic substring efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity for large input strings.
✗ Incorrect
Manacher's algorithm finds longest palindromic substring in linear time, best for large strings.
