Bird
0
0
DSA Cprogramming~20 mins

Valid Palindrome Two Pointer in DSA C - Practice Problems & Challenges

Choose your learning style9 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 Two Pointer Palindrome Check
What is the output of the following code when checking if the string "A man, a plan, a canal: Panama" is a valid palindrome?
DSA C
bool isPalindrome(char * s) {
    int left = 0, right = strlen(s) - 1;
    while (left < right) {
        while (left < right && !isalnum(s[left])) left++;
        while (left < right && !isalnum(s[right])) right--;
        if (tolower(s[left]) != tolower(s[right])) return false;
        left++;
        right--;
    }
    return true;
}

int main() {
    char s[] = "A man, a plan, a canal: Panama";
    printf("%s\n", isPalindrome(s) ? "true" : "false");
    return 0;
}
ARuntime error
Btrue
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Consider only alphanumeric characters and ignore cases.
Predict Output
intermediate
2:00remaining
Output for Non-Palindrome String
What will the output be when the code checks if the string "race a car" is a valid palindrome?
DSA C
bool isPalindrome(char * s) {
    int left = 0, right = strlen(s) - 1;
    while (left < right) {
        while (left < right && !isalnum(s[left])) left++;
        while (left < right && !isalnum(s[right])) right--;
        if (tolower(s[left]) != tolower(s[right])) return false;
        left++;
        right--;
    }
    return true;
}

int main() {
    char s[] = "race a car";
    printf("%s\n", isPalindrome(s) ? "true" : "false");
    return 0;
}
Afalse
Btrue
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check characters ignoring spaces and punctuation carefully.
🧠 Conceptual
advanced
1:30remaining
Why Two Pointers Work for Palindrome Check
Why is the two-pointer technique efficient for checking if a string is a palindrome?
AIt converts the string to uppercase before checking.
BIt sorts the string first to check for palindrome properties.
CIt uses extra memory to store reversed string for comparison.
DIt compares characters from both ends simultaneously, reducing the number of comparisons.
Attempts:
2 left
💡 Hint
Think about how palindrome characters relate from start and end.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Palindrome Code
What is the bug in this palindrome checking code snippet?
DSA C
bool isPalindrome(char * s) {
    int left = 0, right = strlen(s);
    while (left < right) {
        if (tolower(s[left]) != tolower(s[right])) return false;
        left++;
        right--;
    }
    return true;
}
Aright should be initialized to strlen(s) - 1 to avoid out-of-bounds access.
Btolower should not be used on characters.
Cleft and right pointers should both start at 0.
DThe loop condition should be while (left <= right).
Attempts:
2 left
💡 Hint
Check the index of the last character in the string.
🚀 Application
expert
3:00remaining
Count Palindromic Substrings Using Two Pointers
Given a string, which approach correctly counts all palindromic substrings using the two-pointer technique?
AUse a hash map to store all substrings and check each for palindrome.
BReverse the string and compare substrings directly.
CExpand around each center (single and between characters) using two pointers to count palindromes.
DSort the string and count repeated characters as palindromes.
Attempts:
2 left
💡 Hint
Think about how to find palindromes centered at each position.