Skip to main content

49. Group Anagrams

mediumAsked at Broadcom

Group strings that are anagrams of each other. Broadcom asks this to test canonical-key design — the same normalisation thinking used when building flow-key hashing for network traffic classification in Broadcom's switching ASIC control plane.

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

Source citations

Public interview reports confirming this problem appears in Broadcom loops.

  • Glassdoor (2025-12)Reported in Broadcom SWE mid-level onsite coding rounds as a hash-map key-design problem.
  • Blind (2025-10)Broadcom threads list Group Anagrams as a frequently asked medium for infrastructure and VMware software roles.

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

Explanation: Strings sorted become the canonical key: eat→aet, tea→aet, ate→aet group together.

Example 2

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

Explanation: Single empty string is its own group.

Example 3

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

Explanation: Single-character string is its own group.

Approaches

1. Sort each string as canonical key

For each string, sort its characters to produce a canonical key. Use a Map from key → group array. Return all groups.

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 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: Simple and correct. O(n · k log k) due to per-string sort. Acceptable for most interview contexts.

2. Character frequency array as key

Instead of sorting, build a 26-element frequency count array for each string and use it as the map key (serialised as a string). Avoids the O(k log k) sort.

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

Tradeoff: O(n · k) — linear in total character count. The better asymptotic approach. Broadcom interviewers who probe for optimisations will ask for this version after the sort-based solution.

Broadcom-specific tips

Lead with the sorted-key approach, then offer the frequency-array optimisation: 'If k is large, I can avoid the sort by building a 26-character frequency vector as the key — reduces per-string work from O(k log k) to O(k).' At Broadcom, draw the parallel to flow-key hashing: 'In packet classification, you compute a canonical representation of a 5-tuple — same design pattern.' This shows networking domain fluency.

Common mistakes

  • Using an object key directly instead of serialising it — JavaScript object keys are strings; an array key requires .join() or JSON.stringify().
  • Not initialising the Map bucket before pushing — use map.has() guard or map.get() || [].
  • Sorting the input array itself instead of individual strings.
  • Forgetting that the empty string is a valid input — its sorted form is '' which is a valid map key.

Follow-up questions

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

  • What if the strings can contain Unicode characters, not just lowercase ASCII?
  • How would you group anagrams in a streaming setting where strings arrive one at a time?
  • What if you need to return groups sorted by size (largest first)?

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-array approach faster?

Sorting a string of length k costs O(k log k); building the frequency array costs O(k). For long strings the difference is significant.

Can I use a plain JavaScript object instead of Map?

Yes, but Map is preferred because it avoids prototype-chain collisions and has O(1) guaranteed lookups. Use it to demonstrate awareness of JS data-structure semantics.

How does Broadcom extend this problem?

Common extensions include grouping by specific edit-distance families or grouping packet headers by flow-key prefix — the canonical-key design principle is the same.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →