Skip to main content

49. Group Anagrams

mediumAsked at DRW

DRW uses Group Anagrams to test hash-key design — the skill of choosing a canonical representation that correctly groups equivalent items. In trading systems this appears in instrument deduplication: grouping ticker aliases (GOOG / GOOGL, different listings of the same underlying) by a canonical identifier.

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

Source citations

Public interview reports confirming this problem appears in DRW loops.

  • Glassdoor (2025-Q4)DRW SWE candidates report hash-map grouping problems as a recurring theme in mid-level coding rounds.
  • Blind (2025-10)DRW interview threads cite Group Anagrams as a test of hash-key design, with interviewers asking for two different canonical-key strategies.

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

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

Explanation: Group by anagram equivalence class.

Example 2

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

Explanation: Single empty string.

Example 3

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

Explanation: Single character.

Approaches

1. Sort each string as the key

For each word, sort its characters to produce a canonical key. Group words by this key in a hash map.

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

Tradeoff: O(n × k log k) — the sort dominates. Simple and correct. Present this first, then offer the character-count key as an O(n × k) improvement.

2. Character frequency array as key

Represent each string by its 26-character frequency count. Use the frequency array as the hash-map key (stringified). Eliminates the O(k log k) sort.

Time
O(n × k)
Space
O(n × k)
function groupAnagrams(strs) {
  const map = new Map();
  for (const str of strs) {
    const count = new Array(26).fill(0);
    for (const ch of str) count[ch.charCodeAt(0) - 97]++;
    const key = count.join(',');
    if (!map.has(key)) map.set(key, []);
    map.get(key).push(str);
  }
  return [...map.values()];
}

Tradeoff: O(n × k) time — linear in total character count. The key is always 26 integers, so key comparison is O(1) for fixed charset. This is the preferred answer when k >> 26.

DRW-specific tips

DRW interviewers will ask: 'Which key strategy do you prefer and why?' Lead with the trade-off: 'For small k (short strings), sort is fine. For large k with small alphabet, frequency-count is better by a log factor. For large k with large alphabet (Unicode), a rolling hash or polynomial hash gives O(k) key construction without the 26-wide fixed array.' They will then pivot to: 'How would you group financial instrument aliases by underlying security?' — same canonical-key concept applied to a domain problem.

Common mistakes

  • Using the sorted string key for Unicode strings — sort order is locale-dependent and may produce inconsistent keys.
  • Not using a consistent key for empty strings — the empty array frequency key '0,0,...,0' and empty sort '' both work, but must be consistent.
  • Using JSON.stringify(count) as the key — slightly slower than count.join(',') and produces a longer string.
  • Returning Map.values() as-is without spreading — Map.values() returns an iterator, not an array.

Follow-up questions

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

  • Valid Anagram (LC 242) — check if two strings are anagrams; the frequency-count approach applies directly.
  • How would you group strings that are anagrams of each other as a streaming problem, grouping in real time?
  • Design a hash function for instrument aliases that is collision-resistant under adversarial ticker naming.

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 is the frequency-count key O(n × k) and not O(n × k × 26)?

Building the count array is O(k) per string. Joining it is O(26) = O(1) for fixed English-letter alphabet. Total: O(n × k).

What if strings contain Unicode characters?

Use a Map<char, count> as the frequency structure, then serialize it by sorting its entries. Or use a polynomial rolling hash for O(k) computation with collision probability controlled by the hash modulus.

How does this appear at DRW?

Reference data management groups ticker aliases (e.g., Bloomberg vs. Reuters codes for the same bond) by a canonical ISIN or CUSIP. The grouping logic is structurally identical.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →