Skip to main content

47. Group Anagrams

mediumAsked at Plaid

Group anagrams together from an array of strings. Plaid asks this because grouping merchant names that share the same character profile (after normalization) is a daily reality on their merchant-deduplication pipeline.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend onsite — framed as merchant grouping.
  • Blind (2026)Plaid data-engineering screen.

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
[[""]]

Approaches

1. Pairwise anagram comparison

For each new string, compare against the representative of each existing group.

Time
O(n^2 * k)
Space
O(n * k)
// Pairwise. Too slow for n=10^4.

Tradeoff: Quadratic in number of strings. Don't ship.

2. Sorted-string key

Group by the sorted-character key of each string.

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

Tradeoff: n * k log k. For k <= 100 the log k factor is negligible. The sorted string is a canonical anagram key.

Plaid-specific tips

Plaid grades this on whether you reach for a canonical-key approach. Bonus signal: discuss the char-count key alternative (O(n*k) total) and explain when each wins — sorted-key when k is small, count-key when k can be large but the alphabet is fixed. Connect to merchant grouping where 'AMZN Marketplace' and 'Amazon Marketplace' must hash to the same canonical form.

Common mistakes

  • Using the original string as a key — defeats the point of grouping.
  • Forgetting to spread the Set/Map values when returning.
  • Using string concatenation in a loop instead of Array.join — quadratic time.

Follow-up questions

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

  • Anagrams in a stream — bounded memory.
  • Group similar merchants by fuzzy edit distance (not exact anagram).
  • Find pairs of strings that differ by exactly one letter.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Sorted-key vs count-key?

Sorted is n*k log k. Count is n*k + 26 per key (build a 26-element vector). For small k both are fine; for k=10^4 the count-key wins.

Why a Map and not an Object?

Map keys can be anything (e.g., a count tuple if you go that route). For string keys both work, but Map.values() is more iteration-friendly.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →