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 Java code that finds the length of the longest palindromic substring in the input string?
DSA Java
public class Solution { public static int longestPalindromeLength(String s) { int maxLen = 1; int n = s.length(); for (int i = 0; i < n; i++) { // Odd length palindrome int left = i, right = i; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } // Even length palindrome left = i; right = i + 1; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } } return maxLen; } public static void main(String[] args) { System.out.println(longestPalindromeLength("babad")); } }
Attempts:
2 left
💡 Hint
Check palindromes centered at each character and between characters.
✗ Incorrect
The longest palindromic substring in "babad" is "bab" or "aba", both length 3.
❓ Predict Output
intermediateOutput of longest palindromic substring for even length palindrome
What is the output of the following Java code when the input string is "cbbd"?
DSA Java
public class Solution { public static int longestPalindromeLength(String s) { int maxLen = 1; int n = s.length(); for (int i = 0; i < n; i++) { int left = i, right = i; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } left = i; right = i + 1; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } } return maxLen; } public static void main(String[] args) { System.out.println(longestPalindromeLength("cbbd")); } }
Attempts:
2 left
💡 Hint
Look for even length palindromes.
✗ Incorrect
The longest palindromic substring in "cbbd" is "bb", length 2.
🧠 Conceptual
advancedTime complexity of longest palindromic substring algorithm
What is the time complexity of the standard expand-around-center approach to find the longest palindromic substring in a string of length n?
Attempts:
2 left
💡 Hint
Consider that for each center, expansion can take up to n steps.
✗ Incorrect
For each of the n centers (characters and gaps), expanding can take up to n steps, resulting in O(n^2) time.
🔧 Debug
advancedIdentify the bug in longest palindromic substring code
What error will the following Java code produce when run with input "abc"?
DSA Java
public class Solution { public static int longestPalindromeLength(String s) { int maxLen = 0; int n = s.length(); for (int i = 0; i <= n; i++) { int left = i, right = i; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } left = i; right = i + 1; while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) { maxLen = Math.max(maxLen, right - left + 1); left--; right++; } } return maxLen; } public static void main(String[] args) { System.out.println(longestPalindromeLength("abc")); } }
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The for loop runs with i <= n, so s.charAt(i) will cause StringIndexOutOfBoundsException when i == n.
🚀 Application
expertLongest palindromic substring in a large string
Given a string of length 100000 consisting of only 'a' and 'b', what is the length of the longest palindromic substring if the string is "a" repeated 50000 times followed by "b" repeated 50000 times?
Attempts:
2 left
💡 Hint
Consider the longest palindrome in each uniform segment.
✗ Incorrect
The longest palindrome is the entire block of 'a's or 'b's, each length 50000.
