Step 1: Normalize strings by lowercasing, removing spaces, sorting
'Listen' and 'Silent' normalize to 'eilnst', 'Hello' and 'Olelh' normalize to 'ehllo', 'Test' and 'Taste' normalize to 'estt' and 'aestt' respectively.
Step 2: Compare normalized strings
'Listen' vs 'Silent' match (true), 'Hello' vs 'Olelh' match (true), 'Test' vs 'Taste' do not match (false).
Final Answer:
true true false -> Option B
Quick Check:
Normalized strings equal means true [OK]
Hint: Normalize strings before comparing for anagram check [OK]
Common Mistakes:
Ignoring case differences
Not removing spaces
Not sorting characters
4. Identify the bug in this TypeScript function that checks anagrams:
B. Missing toLowerCase() for case-insensitive check
C. Comparing arrays directly instead of strings
D. Using split() without arguments
Solution
Step 1: Understand array comparison in TypeScript
Arrays compared with === check reference, not content, so two sorted arrays won't be equal even if same elements.
Step 2: Fix by joining arrays to strings before comparing
Joining sorted arrays to strings allows content comparison, fixing the bug.
Final Answer:
Comparing arrays directly instead of strings -> Option C
Quick Check:
Arrays need join() before compare [OK]
Hint: Join sorted arrays before comparing for equality [OK]
Common Mistakes:
Comparing arrays with ===
Ignoring case sensitivity
Not trimming spaces
5. Given two strings, how can you efficiently check if they are anagrams using a character count map in TypeScript? Which code snippet correctly implements this?
hard
A. function isAnagram(a: string, b: string): boolean {
if (a.length !== b.length) return false;
const count: Record = {};
for (const char of a) count[char] = (count[char] || 0) + 1;
for (const char of b) {
if (!count[char]) return false;
count[char]--;
}
return true;
}
B. function isAnagram(a: string, b: string): boolean {
return a.split('').sort().join('') === b.split('').sort().join('');
}
C. function isAnagram(a: string, b: string): boolean {
return a.length === b.length;
}
D. function isAnagram(a: string, b: string): boolean {
const mapA = new Map();
for (const c of a) mapA.set(c, (mapA.get(c) || 0) + 1);
for (const c of b) mapA.set(c, (mapA.get(c) || 0) - 1);
return [...mapA.values()].every(v => v === 0);
}
Solution
Step 1: Understand character count map approach
Count characters in first string, subtract counts using second string, then check all counts zero.
Step 2: Analyze each option
function isAnagram(a: string, b: string): boolean {
if (a.length !== b.length) return false;
const count: Record = {};
for (const char of a) count[char] = (count[char] || 0) + 1;
for (const char of b) {
if (!count[char]) return false;
count[char]--;
}
return true;
} misses checking if count goes negative; function isAnagram(a: string, b: string): boolean {
return a.split('').sort().join('') === b.split('').sort().join('');
} uses sorting (correct but not map); function isAnagram(a: string, b: string): boolean {
return a.length === b.length;
} only checks length; function isAnagram(a: string, b: string): boolean {
const mapA = new Map();
for (const c of a) mapA.set(c, (mapA.get(c) || 0) + 1);
for (const c of b) mapA.set(c, (mapA.get(c) || 0) - 1);
return [...mapA.values()].every(v => v === 0);
} correctly uses Map to count and verify zero counts.
Final Answer:
Option D correctly implements character count map with Map and zero check -> Option D
Quick Check:
Map counts increment/decrement, all zero means anagram [OK]
Hint: Use map to count chars, decrement, then check all zero [OK]
Common Mistakes:
Not checking for negative counts
Only comparing lengths
Using sorting instead of counting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.