Skip to main content

49. Group Anagrams

mediumAsked at Pinterest

Group Anagrams is Pinterest's go-to hash-map problem: given an array of strings, group strings that are anagrams of each other. The interviewer wants you to pick a canonical signature (sorted string OR character-count tuple) and articulate the tradeoff.

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

Source citations

Public interview reports confirming this problem appears in Pinterest loops.

  • Glassdoor (2026-Q1)Pinterest SWE-onsite reports list Group Anagrams as the hash-map round.
  • LeetCode Pinterest tag (2026-Q1)Listed on the Pinterest company-tagged problem list.

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, 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. Sort each string, group by sorted key

For each string, sort its characters to get a canonical key. Group by that key in a hash map.

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

Tradeoff: Shortest to write and the most-common interview answer. Sorting per-string is O(k log k); fine at k <= 100.

2. Character-count fingerprint as key (optimal for long strings)

Build a 26-length count array per string, join into a string key. Group by that key.

Time
O(n * k) where k = max string length
Space
O(n * k)
function groupAnagrams(strs) {
  const groups = new Map();
  for (const s of strs) {
    const counts = new Array(26).fill(0);
    for (const ch of s) counts[ch.charCodeAt(0) - 97] += 1;
    const key = counts.join('#');
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(s);
  }
  return [...groups.values()];
}

Tradeoff: Strictly better asymptotics: O(k) per string instead of O(k log k). The '#' separator prevents [1,11] colliding with [11,1]. Use this if the interviewer asks for sub-O(n k log k).

Pinterest-specific tips

Pinterest interviewers like when you volunteer BOTH solutions: lead with the sort-key version (universal, language-agnostic), then mention the count-array key as the optimization. Don't lead with the count array unless asked — it signals over-engineering for the warm-up. The named tradeoff ('sorting per string adds a log factor I can drop') is the senior-IC move.

Common mistakes

  • Using char-count join without a separator: counts [1,11] and [11,1] both join to '111' — wrong.
  • Using a Map keyed by an Array — JavaScript Maps compare arrays by reference, so every array is a new key. Stringify first.
  • Sorting in-place on the original strings (s.split is fine since strings are immutable, but mutating an array bites in other languages).
  • Returning the keys instead of the grouped values.

Follow-up questions

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

  • What if strings can be in unicode (multi-byte characters)? Count array no longer works; use a sorted-string key or a Map<char, count>.
  • What if all anagram-groups must be sorted lexicographically internally?
  • Streaming: anagrams arrive one at a time; emit groups as they form.
  • Find anagram subsets of length K from a larger string.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is the count-array version actually faster in practice?

At k <= 100 with lowercase-only strings, the constant factor of array allocation often makes it slower than sort in V8. The asymptotic improvement matters at larger k or unicode. Mention this nuance.

Why does Pinterest ask this specifically?

It tests whether you reach for a canonicalization-then-group pattern, which shows up in dedup / clustering / categorization work across the product.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →