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 JavaScript code that checks if a string is a palindrome?
DSA Javascript
function isPalindrome(str) { const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); return cleaned === cleaned.split('').reverse().join(''); } console.log(isPalindrome('Racecar'));
Attempts:
2 left
💡 Hint
Think about how the string is cleaned and reversed before comparison.
✗ Incorrect
The function converts the string to lowercase and removes non-alphanumeric characters. 'Racecar' becomes 'racecar', which is the same forwards and backwards, so the function returns true.
❓ Predict Output
intermediateResult of Palindrome Check on Phrase with Spaces and Punctuation
What will the following code print when checking if the phrase is a palindrome?
DSA Javascript
function isPalindrome(str) { const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); return cleaned === cleaned.split('').reverse().join(''); } console.log(isPalindrome('A man, a plan, a canal: Panama'));
Attempts:
2 left
💡 Hint
Consider how non-letter characters are removed before checking.
✗ Incorrect
After cleaning, the phrase becomes 'amanaplanacanalpanama', which reads the same forwards and backwards, so the output is true.
❓ Predict Output
advancedOutput of Palindrome Check with Unicode Characters
What is the output of this code that checks if a string with Unicode characters is a palindrome?
DSA Javascript
function isPalindrome(str) { const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); return cleaned === cleaned.split('').reverse().join(''); } console.log(isPalindrome('Noël Léon'));
Attempts:
2 left
💡 Hint
Think about how the regex removes characters outside a-z and 0-9.
✗ Incorrect
The accented characters 'ë' and 'é' are removed by the regex, resulting in 'nollon', which is a palindrome, so the output is true.
🧠 Conceptual
advancedUnderstanding Palindrome Detection Logic
Which step is NOT necessary when checking if a string is a palindrome ignoring case and non-alphanumeric characters?
Attempts:
2 left
💡 Hint
Think about what palindrome means and what sorting does.
✗ Incorrect
Sorting characters changes their order and does not help check if the string reads the same forwards and backwards, so it is unnecessary.
❓ Predict Output
expertOutput of Palindrome Check Using Two-Pointer Technique
What is the output of this JavaScript code that uses two pointers to check palindrome?
DSA Javascript
function isPalindrome(str) { const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); let left = 0; let right = cleaned.length - 1; while (left < right) { if (cleaned[left] !== cleaned[right]) { return false; } left++; right--; } return true; } console.log(isPalindrome('Was it a car or a cat I saw?'));
Attempts:
2 left
💡 Hint
Check how the two pointers move and compare characters.
✗ Incorrect
After cleaning, the string is 'wasitacaroracatisaw', which reads the same forwards and backwards, so the function returns true.
