Challenge - 5 Problems
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of sorting-based anagram check
What is the output of this TypeScript code that checks if two strings are anagrams by sorting their characters?
DSA Typescript
function areAnagrams(s1: string, s2: string): boolean {
return s1.split('').sort().join('') === s2.split('').sort().join('');
}
console.log(areAnagrams('listen', 'silent'));
console.log(areAnagrams('hello', 'bello'));
Attempts:
2 left
💡 Hint
Think about what sorting the characters in both strings does before comparing.
✗ Incorrect
Sorting both strings puts their characters in order. If they are anagrams, sorted strings match exactly. 'listen' and 'silent' are anagrams, so true. 'hello' and 'bello' differ, so false.
❓ Predict Output
intermediateOutput of frequency map anagram check
What is the output of this TypeScript code that checks if two strings are anagrams using character frequency maps?
DSA Typescript
function areAnagrams(s1: string, s2: string): boolean {
if (s1.length !== s2.length) return false;
const freq: {[key: string]: number} = {};
for (const ch of s1) freq[ch] = (freq[ch] || 0) + 1;
for (const ch of s2) {
if (!freq[ch]) return false;
freq[ch]--;
}
return true;
}
console.log(areAnagrams('triangle', 'integral'));
console.log(areAnagrams('apple', 'pale'));
Attempts:
2 left
💡 Hint
Check if character counts match exactly for both strings.
✗ Incorrect
The frequency map counts characters in s1, then subtracts counts for s2. If any count goes below zero or missing, not an anagram. 'triangle' and 'integral' match, 'apple' and 'pale' do not.
🔧 Debug
advancedIdentify the error in this anagram check code
This TypeScript function is intended to check if two strings are anagrams using a frequency map. What error will it cause when run?
DSA Typescript
function areAnagrams(s1: string, s2: string): boolean {
if (s1.length !== s2.length) return false;
const freq: {[key: string]: number} = {};
for (const ch of s1) freq[ch] += 1;
for (const ch of s2) {
if (!freq[ch]) return false;
freq[ch]--;
}
return true;
}
console.log(areAnagrams('abc', 'cab'));
Attempts:
2 left
💡 Hint
Look at how freq[ch] is updated before initialization.
✗ Incorrect
The code outputs 'false' incorrectly. freq[ch] is undefined initially, so freq[ch] += 1 sets it to NaN. NaN is falsy, so !NaN is true and it returns false early even for anagrams.
🧠 Conceptual
advancedWhich method is more efficient for large strings?
Between sorting both strings and using a frequency map to check for anagrams, which method is generally more efficient for very large strings and why?
Attempts:
2 left
💡 Hint
Consider the time complexity of sorting vs counting characters.
✗ Incorrect
Sorting takes O(n log n) time, while frequency map counting takes O(n) time, making frequency map faster for large inputs.
🚀 Application
expertFind all anagram groups in a list of words
Given a list of words, which TypeScript code correctly groups all anagrams together as arrays inside a result array?
DSA Typescript
const words = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'];
Attempts:
2 left
💡 Hint
Group words by their sorted character string as key, accumulating all anagrams in arrays.
✗ Incorrect
Option A correctly sorts each word, uses the sorted string as key, initializes array if needed, and pushes the word. Option A reverses instead of sorting, A groups by original word (no grouping), D overwrites groups each time losing previous words.
