Skip to main content

49. Group Anagrams

mediumAsked at HubSpot

HubSpot frequently asks Group Anagrams because it tests canonical key generation for grouping — a fundamental skill in CRM data normalization where contacts or properties with different surface strings need to be clustered by canonical form.

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

Source citations

Public interview reports confirming this problem appears in HubSpot loops.

  • Glassdoor (2026-Q1)Multiple HubSpot SWE interview reports mention Group Anagrams as a canonical medium-difficulty string problem.
  • Blind (2025-10)HubSpot candidates frequently list Group Anagrams as an onsite coding problem requiring hash map grouping.

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

Explanation: Group strings that are anagrams of each other. Order within groups and order of groups doesn't matter.

Example 2

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

Explanation: An empty string is an anagram of itself — one group.

Approaches

1. Sort each string as key

For each string, sort its characters to produce a canonical key. Group all strings with the same sorted key using a hash map.

Time
O(n * k log k) where k is 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 Array.from(map.values());
}

Tradeoff: Clean and easy to implement. The k log k sort per string is the bottleneck. For most HubSpot interviews this is the expected answer — clean code, clearly explained key generation.

2. Frequency count as key

Represent each string by a 26-element character frequency array, serialized as a string key. No sorting needed — this is O(k) per string.

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 Array.from(map.values());
}

Tradeoff: O(n * k) — better asymptotically than sorting. Impressive to present as an optimization. The key is a comma-delimited frequency vector: '1,0,...,1,0,...' ensures unique serialization per anagram class.

HubSpot-specific tips

Lead with the key insight: 'Two strings are anagrams if and only if their sorted forms are identical — so I can use sorted(str) as a hash key.' HubSpot interviewers appreciate when you discuss the trade-off between the sort-based and frequency-count approaches. In Java or Python, sorting is idiomatic; in JavaScript, split/sort/join is the equivalent. Mention that the output order is non-deterministic (map traversal order) and that's acceptable per the problem.

Common mistakes

  • Sorting in place with Array.sort() — strings are immutable in JS, but always convert to array first to make the intent explicit.
  • Using a plain object instead of Map — objects coerce keys to strings, which works here but Map is more semantically correct.
  • Forgetting that strs[i] can be empty — empty string sorts to '' and correctly groups with other empty strings.
  • Building the key as count.toString() — this calls Array.prototype.toString() which uses commas, but count.join(',') is more explicit and collision-safe.

Follow-up questions

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

  • Valid Anagram (LC 242) — check if two strings are anagrams of each other.
  • Find All Anagrams in a String (LC 438) — sliding window to find all anagram substrings.
  • How would you group anagrams if the strings contained Unicode characters beyond ASCII?

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 sort-based key O(k log k) and not O(k)?

Sorting k characters requires O(k log k) comparisons in the general case. The frequency-count approach avoids sorting entirely by building a fixed-size 26-element vector in O(k).

Is the frequency-count key guaranteed to be unique per anagram class?

Yes — two strings have the same character frequency vector if and only if they are anagrams. Serializing the vector with commas between counts avoids collision (e.g., [11,2] vs [1,12]).

Does the order within each group matter?

No — the problem says 'you can return the answer in any order', so insertion order within each group is fine.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →