Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of expanding around center approach
What is the output of the following C# code that finds the longest palindromic substring by expanding around each center?
DSA C#
using System; class Program { static string LongestPalindrome(string s) { if (string.IsNullOrEmpty(s)) return ""; int start = 0, maxLen = 1; for (int i = 0; i < s.Length; i++) { int len1 = ExpandFromCenter(s, i, i); int len2 = ExpandFromCenter(s, i, i + 1); int len = Math.Max(len1, len2); if (len > maxLen) { maxLen = len; start = i - (len - 1) / 2; } } return s.Substring(start, maxLen); } static int ExpandFromCenter(string s, int left, int right) { while (left >= 0 && right < s.Length && s[left] == s[right]) { left--; right++; } return right - left - 1; } static void Main() { Console.WriteLine(LongestPalindrome("babad")); } }
Attempts:
2 left
💡 Hint
Check how the center expansion chooses the palindrome when multiple have the same length.
✗ Incorrect
The code expands around each character and between characters to find palindromes. For input "babad", both "bab" and "aba" are palindromes of length 3. The code returns the first longest palindrome found, which is "bab" starting at index 0.
🧠 Conceptual
intermediateUnderstanding palindrome expansion indices
In the expand around center method for longest palindromic substring, what does the returned value represent?
Attempts:
2 left
💡 Hint
Look at how the while loop expands and what is returned after it stops.
✗ Incorrect
The method expands while characters at left and right are equal, then returns the length of the palindrome found, calculated as right - left - 1.
🔧 Debug
advancedIdentify the error in this palindrome substring code
What error will occur when running this code snippet to find the longest palindromic substring?
DSA C#
string s = "cbbd"; int start = 0, maxLen = 1; for (int i = 0; i < s.Length; i++) { int len1 = Expand(s, i, i); int len2 = Expand(s, i, i + 1); int len = Math.Max(len1, len2); if (len > maxLen) { maxLen = len; start = i - (len / 2); } } string result = s.Substring(start, maxLen); int Expand(string str, int left, int right) { while (left >= 0 && right < str.Length && str[left] == str[right]) { left--; right++; } return right - left - 1; }
Attempts:
2 left
💡 Hint
Check how start index is calculated when length is even.
✗ Incorrect
The start index calculation uses integer division len / 2 which causes incorrect start index for even length palindromes, possibly negative or out of range, causing ArgumentOutOfRangeException in Substring.
🚀 Application
advancedLongest palindromic substring length for a given input
What is the length of the longest palindromic substring in the string "forgeeksskeegfor"?
Attempts:
2 left
💡 Hint
Look for the longest palindrome inside the string, it is "geeksskeeg".
✗ Incorrect
The longest palindromic substring is "geeksskeeg" which has length 10.
🧠 Conceptual
expertTime complexity of longest palindromic substring algorithms
Which of the following statements about the time complexity of longest palindromic substring algorithms is correct?
Attempts:
2 left
💡 Hint
Consider how many centers are expanded and how much space is used.
✗ Incorrect
Expand around center checks 2n-1 centers, each expansion takes O(n), total O(n^2) time with constant extra space. Manacher's algorithm runs in O(n) time. DP approach is O(n^2) time and space. Brute force is O(n^3).
