Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of Two Pointer Palindrome Check
What is the output of the following C++ code that checks if a string is a palindrome ignoring non-alphanumeric characters and case?
DSA C++
#include <iostream> #include <string> #include <cctype> using namespace std; bool isPalindrome(string s) { int left = 0, right = s.size() - 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() { string test = "A man, a plan, a canal: Panama"; cout << (isPalindrome(test) ? "true" : "false") << endl; return 0; }
Attempts:
2 left
๐ก Hint
Focus on how non-alphanumeric characters are skipped and case is ignored.
โ Incorrect
The code uses two pointers moving inward, skipping non-alphanumeric characters and comparing lowercase characters. The input string is a well-known palindrome phrase ignoring punctuation and case, so the output is true.
โ Predict Output
intermediateResult of Palindrome Check on Mixed Characters
What will be the output of this C++ code snippet?
DSA C++
#include <iostream> #include <string> #include <cctype> using namespace std; bool isPalindrome(string s) { int left = 0, right = s.size() - 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() { string test = "race a car"; cout << (isPalindrome(test) ? "true" : "false") << endl; return 0; }
Attempts:
2 left
๐ก Hint
Check if the characters match after skipping spaces and ignoring case.
โ Incorrect
The string "race a car" is not a palindrome when ignoring spaces and case because 'e' and 'a' do not match at the corresponding positions.
๐ง Debug
advancedIdentify the Error in Palindrome Function
What error will this C++ code produce when checking if a string is a palindrome?
DSA C++
#include <iostream> #include <string> #include <cctype> using namespace std; bool isPalindrome(string s) { int left = 0, right = s.size() - 1; while (left <= right) { if (!isalnum(s[left])) left++; if (!isalnum(s[right])) right--; if (tolower(s[left]) != tolower(s[right])) return false; left++; right--; } return true; }
Attempts:
2 left
๐ก Hint
Check how the pointers move when skipping non-alphanumeric characters.
โ Incorrect
The code increments left and right pointers without proper checks after skipping non-alphanumeric characters, which can cause out-of-bounds access when left or right moves beyond limits.
๐ง Conceptual
advancedWhy Use Two Pointers for Palindrome Check?
Why is the two-pointer technique efficient for checking if a string is a palindrome ignoring non-alphanumeric characters and case?
Attempts:
2 left
๐ก Hint
Think about how two pointers move and what they compare.
โ Incorrect
Two pointers start from the beginning and end, moving inward while skipping non-alphanumeric characters, allowing efficient comparison without extra space or sorting.
โ Predict Output
expertOutput of Palindrome Check with Unicode Characters
What is the output of this C++ code that checks palindrome for a string containing Unicode characters?
DSA C++
#include <iostream> #include <string> #include <cctype> using namespace std; bool isPalindrome(string s) { int left = 0, right = s.size() - 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() { string test = "รbรงรงbร"; cout << (isPalindrome(test) ? "true" : "false") << endl; return 0; }
Attempts:
2 left
๐ก Hint
Consider how tolower and isalnum behave with Unicode characters in standard C++.
โ Incorrect
Standard C++ functions like tolower and isalnum do not handle Unicode characters correctly, so the comparison fails and the function returns false.
