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 Function
What is the output of the following C++ code that checks if a string is a palindrome?
DSA C++
#include <iostream> #include <string> bool isPalindrome(const std::string& s) { int left = 0, right = s.size() - 1; while (left < right) { if (s[left] != s[right]) return false; left++; right--; } return true; } int main() { std::string test = "racecar"; if (isPalindrome(test)) std::cout << "Palindrome" << std::endl; else std::cout << "Not Palindrome" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards.
✗ Incorrect
The function compares characters from start and end moving towards the center. Since "racecar" is the same forwards and backwards, it returns true and prints "Palindrome".
🧠 Conceptual
intermediateUnderstanding Palindrome Detection Logic
Which statement best describes the logic used in a two-pointer palindrome detection approach?
Attempts:
2 left
💡 Hint
Think about how two pointers can check characters efficiently.
✗ Incorrect
The two-pointer method compares characters from both ends moving inward, stopping early if characters differ, which is efficient for palindrome detection.
❓ Predict Output
advancedOutput 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++
#include <iostream> #include <string> #include <cctype> bool isPalindrome(const std::string& s) { int left = 0, right = s.size() - 1; while (left < right) { while (left < right && !std::isalnum(s[left])) left++; while (left < right && !std::isalnum(s[right])) right--; if (std::tolower(s[left]) != std::tolower(s[right])) return false; left++; right--; } return true; } int main() { std::string test = "A man, a plan, a canal, Panama"; if (isPalindrome(test)) std::cout << "Palindrome" << std::endl; else std::cout << "Not Palindrome" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Ignore spaces and case when comparing characters.
✗ Incorrect
The code skips non-alphanumeric characters and compares letters ignoring case. The phrase "A man, a plan, a canal, Panama" is a palindrome under these rules.
🔧 Debug
advancedIdentify the Error in Palindrome Function
What error will this C++ code produce when checking if a string is a palindrome?
DSA C++
bool isPalindrome(const std::string& s) { int left = 0, right = s.size(); while (left < right) { if (s[left] != s[right]) return false; left++; right--; } return true; }
Attempts:
2 left
💡 Hint
Check how the right pointer is initialized and used.
✗ Incorrect
The right pointer is initialized to s.size(), which is one past the last valid index, causing out-of-range access and runtime error.
🚀 Application
expertCount Palindromic Substrings in a String
Given the following C++ code, what is the value of variable count after execution?
DSA C++
#include <iostream> #include <string> int countPalindromicSubstrings(const std::string& s) { int n = s.size(); int count = 0; for (int center = 0; center < 2*n - 1; ++center) { int left = center / 2; int right = left + center % 2; while (left >= 0 && right < n && s[left] == s[right]) { count++; left--; right++; } } return count; } int main() { std::string test = "aaa"; int count = countPalindromicSubstrings(test); std::cout << count << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Count all palindromic substrings including single characters and overlapping ones.
✗ Incorrect
The string "aaa" has palindromic substrings: "a", "a", "a", "aa", "aa", "aaa" totaling 6.
