Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Anagram Check Techniques in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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'));
Atrue\ntrue
Bfalse\nfalse
Ctrue\nfalse
Dfalse\ntrue
Attempts:
2 left
💡 Hint
Think about what sorting the characters in both strings does before comparing.
❓ Predict Output
intermediate
2:00remaining
Output 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'));
Atrue\nfalse
Bfalse\ntrue
Ctrue\ntrue
Dfalse\nfalse
Attempts:
2 left
💡 Hint
Check if character counts match exactly for both strings.
🔧 Debug
advanced
2:00remaining
Identify 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'));
ASyntaxError
BTypeError: Cannot read property '+' of undefined
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Look at how freq[ch] is updated before initialization.
🧠 Conceptual
advanced
2:00remaining
Which 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?
ASorting is more efficient because it uses less memory than frequency maps.
BFrequency map is more efficient because it runs in linear time O(n), while sorting is O(n log n).
CBoth have the same efficiency because they both process all characters.
DSorting is more efficient because it uses built-in optimized sort functions.
Attempts:
2 left
💡 Hint
Consider the time complexity of sorting vs counting characters.
🚀 Application
expert
3:00remaining
Find 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'];
A
const groups: {[key: string]: string[]} = {};
for (const word of words) {
  const sorted = word.split('').sort().join('');
  if (!groups[sorted]) groups[sorted] = [];
  groups[sorted].push(word);
}
console.log(Object.values(groups));
B
const groups: {[key: string]: string[]} = {};
for (const word of words) {
  const sorted = word.split('').reverse().join('');
  if (!groups[sorted]) groups[sorted] = [];
  groups[sorted].push(word);
}
console.log(Object.values(groups));
C
const groups: {[key: string]: string[]} = {};
for (const word of words) {
  if (!groups[word]) groups[word] = [];
  groups[word].push(word);
}
console.log(Object.values(groups));
D
const groups: {[key: string]: string[]} = {};
for (const word of words) {
  const sorted = word.split('').sort().join('');
  groups[sorted] = [word];
}
console.log(Object.values(groups));
Attempts:
2 left
💡 Hint
Group words by their sorted character string as key, accumulating all anagrams in arrays.

Practice

(1/5)
1. What does it mean if two strings are anagrams of each other?
easy
A. They contain the same characters in any order
B. They have the same length but different characters
C. They start with the same character
D. They end with the same character

Solution

  1. Step 1: Understand the definition of anagrams

    Anagrams are words or phrases made by rearranging the letters of another, so they must have the same characters.
  2. Step 2: Compare the character sets

    If two strings have exactly the same characters, just in different orders, they are anagrams.
  3. Final Answer:

    They contain the same characters in any order -> Option A
  4. Quick Check:

    Anagrams = same characters [OK]
Hint: Check if sorted strings match to confirm anagrams [OK]
Common Mistakes:
  • Thinking same length means anagrams
  • Only comparing first or last characters
  • Ignoring character counts
2. Which TypeScript code snippet correctly checks if two strings are anagrams by sorting?
easy
A. const isAnagram = (a: string, b: string) => a.split('').sort().join('') === b.split('').sort().join('');
B. const isAnagram = (a: string, b: string) => a.split('').sort() === b.split('').sort();
C. const isAnagram = (a: string, b: string) => a.sort() === b.sort();
D. const isAnagram = (a: string, b: string) => a === b;

Solution

  1. Step 1: Understand string sorting in TypeScript

    Strings must be split into arrays, sorted, then joined back to strings for comparison.
  2. Step 2: Identify correct syntax

    const isAnagram = (a: string, b: string) => a.split('').sort().join('') === b.split('').sort().join(''); correctly splits, sorts, and joins both strings before comparing.
  3. Final Answer:

    const isAnagram = (a: string, b: string) => a.split('').sort().join('') === b.split('').sort().join(''); -> Option A
  4. Quick Check:

    Sort and join strings before compare [OK]
Hint: Always join after sorting arrays to compare strings [OK]
Common Mistakes:
  • Comparing arrays directly without join
  • Trying to sort strings directly
  • Comparing strings without sorting
3. What is the output of this TypeScript code?
function areAnagrams(s1: string, s2: string): boolean {
  const normalize = (str: string) => str.toLowerCase().replace(/\s+/g, '').split('').sort().join('');
  return normalize(s1) === normalize(s2);
}

console.log(areAnagrams('Listen', 'Silent'));
console.log(areAnagrams('Hello', 'Olelh'));
console.log(areAnagrams('Test', 'Taste'));
medium
A. true false false
B. true true false
C. false true true
D. false false true

Solution

  1. 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.
  2. Step 2: Compare normalized strings

    'Listen' vs 'Silent' match (true), 'Hello' vs 'Olelh' match (true), 'Test' vs 'Taste' do not match (false).
  3. Final Answer:

    true true false -> Option B
  4. 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:
function isAnagram(a: string, b: string): boolean {
  return a.split('').sort() === b.split('').sort();
}
medium
A. Not removing spaces before comparison
B. Missing toLowerCase() for case-insensitive check
C. Comparing arrays directly instead of strings
D. Using split() without arguments

Solution

  1. 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.
  2. Step 2: Fix by joining arrays to strings before comparing

    Joining sorted arrays to strings allows content comparison, fixing the bug.
  3. Final Answer:

    Comparing arrays directly instead of strings -> Option C
  4. 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

  1. Step 1: Understand character count map approach

    Count characters in first string, subtract counts using second string, then check all counts zero.
  2. 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.
  3. Final Answer:

    Option D correctly implements character count map with Map and zero check -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard