Challenge - 5 Problems
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Consider only alphanumeric characters and ignore cases.
✗ Incorrect
The code uses two pointers moving inward, skipping non-alphanumeric characters and comparing lowercase characters. The input string is a classic palindrome ignoring spaces and punctuation.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check characters ignoring spaces and punctuation carefully.
✗ Incorrect
The string "race a car" is not a palindrome when ignoring spaces and punctuation because 'e' and 'a' do not match at the corresponding positions.
🧠 Conceptual
advanced1:30remaining
Why Two Pointers Work for Palindrome Check
Why is the two-pointer technique efficient for checking if a string is a palindrome?
Attempts:
2 left
💡 Hint
Think about how palindrome characters relate from start and end.
✗ Incorrect
Two pointers start from the beginning and end and move inward, comparing characters directly without extra space or full string reversal.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the index of the last character in the string.
✗ Incorrect
strlen(s) returns the length, but valid indices go from 0 to length-1. Using length as index causes out-of-bounds access.
🚀 Application
expert3:00remaining
Count Palindromic Substrings Using Two Pointers
Given a string, which approach correctly counts all palindromic substrings using the two-pointer technique?
Attempts:
2 left
💡 Hint
Think about how to find palindromes centered at each position.
✗ Incorrect
Expanding around centers with two pointers allows counting all palindromic substrings efficiently without extra space.
