Skip to main content

49. Group Anagrams

mediumAsked at Reddit

Group strings that are anagrams of each other. Reddit uses this to test hash-key design — the same shape used when clustering near-duplicate post titles for spam detection (sort the letters to get a normalized fingerprint).

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit trust-and-safety phone screen — clustering near-duplicate content.
  • Blind (2025-11)Reported as a Reddit moderation-team favorite.

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Constraints

  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

Examples

Example 1

Input
strs = ["eat","tea","tan","ate","nat","bat"]
Output
[["bat"],["nat","tan"],["ate","eat","tea"]]

Example 2

Input
strs = [""]
Output
[[""]]

Example 3

Input
strs = ["a"]
Output
[["a"]]

Approaches

1. All-pairs anagram check

Compare each string against every other; group with set-of-visited.

Time
O(n^2 * k log k)
Space
O(n)
// Anti-pattern: O(n^2) pairing.

Tradeoff: TLE for n=10^4.

2. Sorted-string key (optimal)

Sort each string's characters; use that as a Map key.

Time
O(n * k log k)
Space
O(n * k)
function groupAnagrams(strs) {
  const map = new Map();
  for (const s of strs) {
    const key = s.split('').sort().join('');
    if (!map.has(key)) map.set(key, []);
    map.get(key).push(s);
  }
  return [...map.values()];
}

Tradeoff: Sort is O(k log k); 26-count vector is faster but uglier.

Reddit-specific tips

Reddit interviewers ask 'can you avoid the sort?' Answer: use a 26-character count vector as the key. Bonus signal: connect to their near-duplicate spam clustering where they normalize content with a sorted-token signature.

Common mistakes

  • Comparing sorted versions of every pair (O(n^2)).
  • Forgetting to wrap empty string ([''] is a valid group).
  • Using array as Map key directly (object reference, not value-equal).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Valid anagram (LC 242) — pair check.
  • Group shifted strings (LC 249) — different key.
  • Find all anagrams in a string (LC 438).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why sort versus count?

Sort is simpler to write. Count is O(k) vs. O(k log k) — faster for long strings. Both are correct.

Why isn't this LC's clustering problem?

Anagrams are exact matches (same letter multiset). Near-duplicate clustering uses fuzzy keys (shingles, MinHash).

Practice these live with InterviewChamp.AI

Drill Group Anagrams and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →