Bird
Raised Fist0
DSA C#programming~20 mins

Palindrome Detection in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
    }
}
ATrue
BFalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards.
❓ Predict Output
intermediate
2:00remaining
Output 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));
    }
}
ACompilation error due to Regex
BFalse
CTrue
DRuntime exception
Attempts:
2 left
💡 Hint
Ignore spaces and case when checking palindrome.
🔧 Debug
advanced
2:00remaining
Identify 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));
    }
}
AIndexOutOfRangeException
BReturns false
CReturns true
DCompilation error
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer.
❓ Predict Output
advanced
2:00remaining
Output 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));
    }
}
AStackOverflowException
BFalse
CCompilation error
DTrue
Attempts:
2 left
💡 Hint
Recursion checks characters from outside to inside.
🧠 Conceptual
expert
3:00remaining
Minimum 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?
ASort the string and count characters that appear an odd number of times.
BUse two pointers from start and end, increment count when characters differ, then move pointers inward.
CReverse the string and count all positions where characters differ.
DCount all unique characters and divide by two.
Attempts:
2 left
💡 Hint
Focus on pairs of characters from both ends.

Practice

(1/5)
1. What is a palindrome in the context of strings?
easy
A. A string that contains only vowels
B. A string that reads the same forwards and backwards
C. A string with all unique characters
D. A string that is empty

Solution

  1. Step 1: Understand palindrome definition

    A palindrome is a word or phrase that reads the same forwards and backwards, ignoring spaces and punctuation.
  2. Step 2: Apply definition to options

    A string that reads the same forwards and backwards matches this definition exactly, while others describe different string properties.
  3. Final Answer:

    A string that reads the same forwards and backwards -> Option B
  4. Quick Check:

    Palindrome = Same forwards and backwards [OK]
Hint: Check if string reversed equals original [OK]
Common Mistakes:
  • Confusing palindrome with unique characters
  • Thinking palindrome means only vowels
  • Assuming empty string is always palindrome
2. Which of the following C# code snippets correctly checks if a string s is a palindrome?
easy
A. return s.Length == 0;
B. return s == s.ToUpper();
C. return s == new string(s.Reverse().ToArray());
D. return s[0] == s[s.Length];

Solution

  1. Step 1: Understand palindrome check logic

    To check palindrome, compare string with its reversed version.
  2. Step 2: Analyze each option

    return s == new string(s.Reverse().ToArray()); reverses string and compares correctly. return s == s.ToUpper(); compares uppercase, not palindrome. return s.Length == 0; checks empty string only. return s[0] == s[s.Length]; accesses invalid index (out of range).
  3. Final Answer:

    return s == new string(s.Reverse().ToArray()); -> Option C
  4. Quick Check:

    Reverse and compare = return s == new string(s.Reverse().ToArray()); [OK]
Hint: Reverse string and compare to original [OK]
Common Mistakes:
  • Using wrong index causing out of range error
  • Comparing uppercase instead of reversed string
  • Checking only empty string
3. What will be the output of the following C# code?
string s = "level";
bool isPalindrome = s == new string(s.Reverse().ToArray());
Console.WriteLine(isPalindrome);
medium
A. True
B. False
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Reverse the string "level"

    Reversed string is "level", same as original.
  2. Step 2: Compare original and reversed strings

    Since both are equal, isPalindrome is true, so Console.WriteLine prints True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    "level" reversed = "level" [OK]
Hint: Check if reversed string equals original [OK]
Common Mistakes:
  • Assuming output is False for palindromes
  • Confusing compilation error due to Reverse()
  • Thinking runtime exception occurs
4. Identify the error in this palindrome check code and select the fix:
string s = "test";
bool isPalindrome = true;
for (int i = 0; i <= s.Length / 2; i++)
{
    if (s[i] != s[s.Length - i])
    {
        isPalindrome = false;
        break;
    }
}
Console.WriteLine(isPalindrome);
medium
A. Remove the break statement
B. Change loop condition to i < s.Length
C. Initialize isPalindrome to false
D. Change s.Length - i to s.Length - i - 1

Solution

  1. Step 1: Identify index error in character comparison

    String indices go from 0 to s.Length - 1, so s[s.Length - i] is out of range when i=0.
  2. Step 2: Fix index to s.Length - i - 1

    This correctly accesses the mirrored character from the end.
  3. Final Answer:

    Change s.Length - i to s.Length - i - 1 -> Option D
  4. Quick Check:

    Correct index fixes out of range error [OK]
Hint: Remember last index is length minus one [OK]
Common Mistakes:
  • Using s.Length - i instead of s.Length - i - 1
  • Looping beyond half the string length
  • Removing break causing unnecessary checks
5. You want to detect if a string is a palindrome ignoring case and spaces. Which code snippet correctly implements this in C#?
hard
A. string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray());
B. string clean = s.Replace(" ", ""); bool isPal = clean == clean.ToUpper();
C. bool isPal = s == new string(s.Reverse().ToArray());
D. bool isPal = s.ToLower() == s.ToUpper();

Solution

  1. Step 1: Clean string by removing non-letter/digit and convert to lowercase

    string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); uses Where with char.IsLetterOrDigit and ToLower to normalize string.
  2. Step 2: Reverse cleaned string and compare

    string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); reverses cleaned string and compares correctly for palindrome ignoring case and spaces.
  3. Final Answer:

    string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); -> Option A
  4. Quick Check:

    Normalize then reverse and compare = string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); [OK]
Hint: Remove spaces and lowercase before checking palindrome [OK]
Common Mistakes:
  • Not removing spaces or punctuation
  • Comparing original string without normalization
  • Using uppercase comparison incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard