Challenge - 5 Problems
Palindrome Mastery
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 TypeScript code that checks if a string is a palindrome ignoring non-alphanumeric characters and case?
DSA Typescript
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) left++;
while (left < right && !isAlphaNumeric(s[right])) right--;
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
left++;
right--;
}
return true;
}
function isAlphaNumeric(c: string): boolean {
return /^[a-z0-9]$/i.test(c);
}
console.log(isPalindrome("A man, a plan, a canal: Panama"));Attempts:
2 left
💡 Hint
Check how non-alphanumeric characters are skipped and case is ignored.
✗ Incorrect
The code uses two pointers moving inward, skipping non-alphanumeric characters and comparing characters case-insensitively. The input is a classic palindrome phrase ignoring punctuation and spaces, so the function returns true.
❓ Predict Output
intermediateResult of Palindrome Check on Non-Palindrome String
What will the following TypeScript code print when checking if the string "race a car" is a palindrome?
DSA Typescript
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) left++;
while (left < right && !isAlphaNumeric(s[right])) right--;
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
left++;
right--;
}
return true;
}
function isAlphaNumeric(c: string): boolean {
return /^[a-z0-9]$/i.test(c);
}
console.log(isPalindrome("race a car"));Attempts:
2 left
💡 Hint
Check the characters compared after skipping spaces.
✗ Incorrect
The string "race a car" is not a palindrome because the characters 'e' and 'a' do not match when comparing from the ends inward.
🧠 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 the pointers move and what characters they compare.
✗ Incorrect
Two pointers start at the beginning and end of the string and move inward, skipping non-alphanumeric characters and comparing characters case-insensitively. This avoids extra space and checks in one pass.
🔧 Debug
advancedIdentify the Error in Palindrome Function
What error will this TypeScript code produce when checking if "No lemon, no melon" is a palindrome?
DSA Typescript
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) left++
while (left < right && !isAlphaNumeric(s[right])) right--
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false
left++
right--
}
return true
}
function isAlphaNumeric(c: string): boolean {
return /^[a-z0-9]$/i.test(c);
}
console.log(isPalindrome("No lemon, no melon"));Attempts:
2 left
💡 Hint
Check if missing semicolons cause errors in TypeScript.
✗ Incorrect
TypeScript allows optional semicolons; the code runs fine and returns true because the string is a palindrome ignoring punctuation and case.
🚀 Application
expertCount 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 of different lengths centered at each position.
✗ Incorrect
Expanding around each center with two pointers allows counting all palindromic substrings efficiently by checking odd and even length palindromes.
