Challenge - 5 Problems
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Palindrome Check for a Simple String
What is the output of the following C# code that checks if a string is a palindrome?
DSA C#
using System; class Program { static bool IsPalindrome(string s) { int left = 0, right = s.Length - 1; while (left < right) { if (s[left] != s[right]) return false; left++; right--; } return true; } static void Main() { string test = "radar"; Console.WriteLine(IsPalindrome(test)); } }
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards.
✗ Incorrect
The string "radar" is the same when reversed, so the function returns true.
❓ Predict Output
intermediateOutput of Palindrome Check with Mixed Case and Spaces
What is the output of this C# code that checks if a string is a palindrome ignoring case and spaces?
DSA C#
using System; using System.Text.RegularExpressions; class Program { static bool IsPalindrome(string s) { s = Regex.Replace(s.ToLower(), "[^a-z0-9]", ""); int left = 0, right = s.Length - 1; while (left < right) { if (s[left] != s[right]) return false; left++; right--; } return true; } static void Main() { string test = "A man a plan a canal Panama"; Console.WriteLine(IsPalindrome(test)); } }
Attempts:
2 left
💡 Hint
Ignore spaces and case when checking palindrome.
✗ Incorrect
After removing non-alphanumeric characters and ignoring case, the string reads the same forwards and backwards.
🔧 Debug
advancedIdentify the Error in Palindrome Function
What error will this C# code produce when checking if a string is a palindrome?
DSA C#
using System; class Program { static bool IsPalindrome(string s) { int left = 0, right = s.Length; while (left < right) { if (s[left] != s[right]) return false; left++; right--; } return true; } static void Main() { string test = "level"; Console.WriteLine(IsPalindrome(test)); } }
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer.
✗ Incorrect
The right pointer is set to s.Length which is out of bounds for indexing, causing an exception.
❓ Predict Output
advancedOutput of Palindrome Check Using Recursion
What is the output of this recursive C# palindrome check for the input "deified"?
DSA C#
using System; class Program { static bool IsPalindrome(string s, int left, int right) { if (left >= right) return true; if (s[left] != s[right]) return false; return IsPalindrome(s, left + 1, right - 1); } static void Main() { string test = "deified"; Console.WriteLine(IsPalindrome(test, 0, test.Length - 1)); } }
Attempts:
2 left
💡 Hint
Recursion checks characters from outside to inside.
✗ Incorrect
The string "deified" is a palindrome, so the recursive function returns true.
🧠 Conceptual
expertMinimum Number of Changes to Make a String Palindrome
Given a string, what is the minimum number of character changes needed to make it a palindrome? For example, for "abcd", the answer is 2 because changing 'c' to 'b' and 'd' to 'a' makes "abba" which is a palindrome. Which approach correctly computes this?
Attempts:
2 left
💡 Hint
Focus on pairs of characters from both ends.
✗ Incorrect
Only pairs of mismatched characters need one change each to match, so counting mismatches with two pointers gives the minimum changes.
