A palindrome is a word, phrase, number, or sequence that reads the same backward as forward, ignoring spaces, punctuation, and capitalization.
Click to reveal answer
beginner
How can you check if a string is a palindrome using two pointers?
Use two pointers: one at the start and one at the end of the string. Move both pointers towards the center, comparing characters. If all pairs match, the string is a palindrome.
Click to reveal answer
intermediate
Why do we ignore case and non-alphanumeric characters when detecting palindromes?
Ignoring case and non-alphanumeric characters ensures that the palindrome check focuses on meaningful characters only, making it more flexible and accurate for phrases and sentences.
Click to reveal answer
intermediate
What is the time complexity of checking a palindrome by comparing characters from both ends?
The time complexity is O(n), where n is the length of the string, because each character is checked at most once.
Click to reveal answer
beginner
Show a simple JavaScript function to detect if a string is a palindrome.
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;
}
Click to reveal answer
What does a palindrome string read the same as?
AOnly forward
BBackward and forward
COnly backward
DRandomly
✗ Incorrect
A palindrome reads the same backward and forward.
Which characters should be ignored when checking palindromes in phrases?
AOnly punctuation
BOnly spaces
CNo characters should be ignored
DSpaces and punctuation
✗ Incorrect
Spaces and punctuation are ignored to focus on meaningful characters.
What is the best approach to check palindrome efficiently?
ACompare characters from both ends moving inward
BReverse the string and compare
CSort the characters and compare
DCount characters
✗ Incorrect
Comparing characters from both ends moving inward is efficient and uses O(n) time.
What is the time complexity of palindrome detection using two pointers?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Each character is checked once, so time complexity is O(n).
In JavaScript, which method helps remove non-alphanumeric characters from a string?
Aslice()
Bsplit(' ')
Creplace(/[^a-z0-9]/g, '')
DtoUpperCase()
✗ Incorrect
The replace method with regex removes non-alphanumeric characters.
Explain how to detect if a string is a palindrome using the two-pointer technique.
Think about checking characters from both ends towards the center.
You got /6 concepts.
Describe why normalization (like ignoring case and non-alphanumeric characters) is important in palindrome detection.
Consider how phrases with spaces and punctuation can still be palindromes.
You got /4 concepts.
Practice
(1/5)
1. What does a palindrome string mean in simple terms?
easy
A. A string that contains only vowels
B. A string that reads the same forwards and backwards
C. A string with all unique characters
D. A string that ends with a special character
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards, like 'madam' or 'racecar'.
Step 2: Compare options with definition
Only A string that reads the same forwards and backwards matches this definition exactly.
Final Answer:
A string that reads the same forwards and backwards -> Option B
Quick Check:
Palindrome = same forwards and backwards [OK]
Hint: Palindrome means same forwards and backwards [OK]
Common Mistakes:
Confusing palindrome with unique characters
Thinking palindrome means only vowels
Assuming palindrome ends with special character
2. Which of the following JavaScript code snippets correctly checks if a string str is a palindrome?
easy
A. return str === str.split('').reverse().join('');
B. return str === str.toUpperCase();
C. return str.length === 0;
D. return str.indexOf('a') !== -1;
Solution
Step 1: Understand palindrome check logic
To check palindrome, reverse the string and compare with original.
Step 2: Analyze code snippets
return str === str.split('').reverse().join(''); reverses the string and compares it correctly. Others do unrelated checks.
Final Answer:
return str === str.split('').reverse().join(''); -> Option A
Quick Check:
Reverse and compare = return str === str.split('').reverse().join(''); [OK]
Hint: Reverse string and compare to original [OK]
Common Mistakes:
Using toUpperCase instead of reversing
Checking string length instead of content
Searching for a character instead of palindrome
3. What will be the output of the following code?
function isPalindrome(s) {
s = s.toLowerCase().replace(/[^a-z0-9]/g, '');
return s === s.split('').reverse().join('');
}
console.log(isPalindrome('A man, a plan, a canal: Panama'));
medium
A. false
B. undefined
C. SyntaxError
D. true
Solution
Step 1: Normalize string by removing non-alphanumeric and lowercasing
The string becomes 'amanaplanacanalpanama' after cleaning.
Step 2: Check if cleaned string equals its reverse
Reversing 'amanaplanacanalpanama' gives the same string, so returns true.
Final Answer:
true -> Option D
Quick Check:
Clean and compare reversed string = true [OK]
Hint: Ignore case and non-letters, then compare reversed [OK]
Common Mistakes:
Not removing spaces and punctuation
Ignoring case differences
Expecting false due to original formatting
4. Find the error in this palindrome check function:
function checkPalindrome(str) {
for(let i = 0; i < str.length; i++) {
if(str[i] !== str[str.length - i]) {
return false;
}
}
return true;
}
medium
A. Index for comparison should be str.length - i - 1
B. No error, function works correctly
C. Function should convert string to lowercase first
D. Loop should run only till half the string length
Solution
Step 1: Analyze index used for comparison
Index str.length - i is off by one because string indices start at 0.
Step 2: Correct index calculation
It should be str.length - i - 1 to compare correct characters from end.
Final Answer:
Index for comparison should be str.length - i - 1 -> Option A
Quick Check:
Off-by-one index error fixed by subtracting 1 [OK]
Hint: Remember last index is length - 1 [OK]
Common Mistakes:
Using full length instead of half for loop
Ignoring off-by-one in index
Assuming case sensitivity causes error here
5. You want to check if a sentence is a palindrome ignoring spaces, punctuation, and case. Which approach is best to implement in JavaScript?
hard
A. Check if string length is even or odd first
B. Compare original string with reversed string directly
C. Remove non-alphanumeric characters, convert to lowercase, then compare with reversed string
D. Count vowels and consonants and compare counts
Solution
Step 1: Understand requirements for ignoring spaces, punctuation, and case
We must clean the string by removing unwanted characters and normalize case.
Step 2: Choose approach that cleans and compares reversed string
Remove non-alphanumeric characters, convert to lowercase, then compare with reversed string correctly removes non-alphanumeric characters, converts to lowercase, then compares reversed string.
Final Answer:
Remove non-alphanumeric characters, convert to lowercase, then compare with reversed string -> Option C
Quick Check:
Clean and compare reversed string for sentence palindrome [OK]
Hint: Clean string before palindrome check for sentences [OK]
Common Mistakes:
Comparing raw strings with punctuation
Ignoring case differences
Using vowel/consonant counts which is unrelated
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.