📖 Scenario: You are building a simple text checker that tells if a word or phrase reads the same forwards and backwards, ignoring spaces, punctuation, and letter case. This is called a palindrome.For example, "Race car" is a palindrome because if you ignore spaces and case, it reads the same backwards.
🎯 Goal: Build a program that uses two pointers to check if a given string is a valid palindrome. The program should ignore non-alphanumeric characters and letter case.
📋 What You'll Learn
Create a string variable with the exact value "A man, a plan, a canal: Panama"
Create two number variables called left and right to point to the start and end of the string
Use a while loop to move left and right pointers towards the center
Skip characters that are not letters or numbers
Compare characters ignoring case
Print true if the string is a palindrome, otherwise false
💡 Why This Matters
🌍 Real World
Palindrome checks are used in text processing, data validation, and coding challenges.
💼 Career
Understanding two pointer techniques helps in solving string and array problems efficiently in software development.
Progress0 / 4 steps
1
Create the input string
Create a string variable called input with the exact value "A man, a plan, a canal: Panama"
DSA Typescript
Hint
Use const input = "A man, a plan, a canal: Panama"; to create the string.
2
Set up two pointers
Create two number variables called left and right. Set left to 0 and right to input.length - 1
DSA Typescript
Hint
Use let left = 0; and let right = input.length - 1; to set pointers.
3
Use two pointers to check palindrome
Write a while loop that runs while left < right. Inside the loop, skip characters at left and right if they are not letters or numbers. Compare characters ignoring case. Move left forward and right backward if characters match. If characters don't match, break the loop.
DSA Typescript
Hint
Use nested while loops to skip non-alphanumeric characters. Compare characters with toLowerCase().
4
Print the result
After the loop, print true if left >= right, otherwise print false
DSA Typescript
Hint
Use console.log(left >= right); to print the result.
Practice
(1/5)
1. What is the main idea behind the two-pointer technique to check if a string is a palindrome?
easy
A. Reverse the string and compare it with the original string
B. Count the frequency of each character and check if all are even
C. Use two pointers starting at both ends and move towards the center comparing characters
D. Sort the characters and check if the sorted string equals the original
Solution
Step 1: Understand two-pointer approach
Two pointers start at the beginning and end of the string and move inward.
Step 2: Compare characters at pointers
If characters at both pointers match, move pointers closer; otherwise, it's not a palindrome.
Final Answer:
Use two pointers starting at both ends and move towards the center comparing characters -> Option C
Quick Check:
Two-pointer palindrome check = Use two pointers starting at both ends and move towards the center comparing characters [OK]
Hint: Compare characters from both ends moving inward [OK]
Common Mistakes:
Reversing string instead of using pointers
Checking only half the string without pointers
Ignoring case or non-alphanumeric characters
2. Which of the following TypeScript code snippets correctly initializes two pointers for palindrome checking?
easy
A. let left = 0; let right = s.length - 1;
B. let left = 1; let right = s.length;
C. let left = 0; let right = s.length;
D. let left = 1; let right = s.length - 1;
Solution
Step 1: Identify pointer positions
Left pointer should start at index 0 (start of string), right pointer at last index (length - 1).
Step 2: Check code correctness
let left = 0; let right = s.length - 1; correctly sets left = 0 and right = s.length - 1, valid indices for string.
Final Answer:
let left = 0; let right = s.length - 1; -> Option A
Quick Check:
Pointers start at 0 and length-1 = let left = 0; let right = s.length - 1; [OK]
Hint: Start left at 0 and right at length minus one [OK]
Common Mistakes:
Setting right pointer to s.length (out of bounds)
Starting left pointer at 1 instead of 0
Confusing length with last index
3. What will be the output of the following TypeScript code?
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
console.log(isPalindrome('racecar'));
console.log(isPalindrome('hello'));
medium
A. true false
B. false true
C. true true
D. false false
Solution
Step 1: Trace 'racecar' input
Characters match from both ends moving inward, so returns true.
Step 2: Trace 'hello' input
Characters 'h' and 'o' differ at ends, returns false immediately.
Final Answer:
true false -> Option A
Quick Check:
'racecar' palindrome = true, 'hello' not = false [OK]
Hint: Check characters from ends; mismatch means false [OK]
Common Mistakes:
Assuming 'hello' is palindrome
Returning true without checking all pairs
Off-by-one errors in pointer movement
4. Identify the bug in this TypeScript palindrome checker:
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
medium
A. left pointer should start at 1
B. right pointer initialized to s.length instead of s.length - 1
C. Comparison should be s[left] === s[right]
D. Loop condition should be left <= right
Solution
Step 1: Check pointer initialization
Right pointer is set to s.length, which is out of string index range (0 to length-1).
Step 2: Understand impact of bug
Accessing s[s.length] causes undefined comparison or runtime error.
Final Answer:
right pointer initialized to s.length instead of s.length - 1 -> Option B
Quick Check:
Right pointer must be last index = length-1 [OK]
Hint: Right pointer must be last index, not length [OK]
Common Mistakes:
Using s.length as index causes out-of-bounds
Changing loop condition incorrectly
Confusing equality and inequality in comparison
5. Given the string s = "A man, a plan, a canal: Panama", which modification is needed in the two-pointer palindrome check to correctly identify it as a palindrome?
hard
A. Convert string to uppercase and compare all characters
B. Reverse the string and compare directly
C. Only compare characters at even indices
D. Ignore case and skip non-alphanumeric characters during comparison
Solution
Step 1: Understand input complexity
String contains spaces, commas, and mixed case letters which must be ignored for palindrome check.
Step 2: Modify two-pointer logic
Skip non-alphanumeric characters and compare characters case-insensitively while moving pointers inward.
Final Answer:
Ignore case and skip non-alphanumeric characters during comparison -> Option D
Quick Check:
Skip non-letters and ignore case for valid palindrome [OK]
Hint: Skip non-letters and compare lowercase chars [OK]
Common Mistakes:
Comparing characters without ignoring punctuation
Not converting characters to same case
Assuming direct comparison works on raw string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.