Skip to main content

49. Group Anagrams

mediumAsked at Akamai

Group strings that are anagrams of each other. Akamai uses this to probe hash key design — choosing the right canonical form (sorted string vs. character frequency vector) is the same trade-off engineers face when designing cache keys for edge routing rules.

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

Source citations

Public interview reports confirming this problem appears in Akamai loops.

  • Glassdoor (2025-12)Reported in Akamai SWE interview feedback as a hashing and string manipulation question in mid-level onsite rounds.
  • Blind (2025-09)Akamai threads list Group Anagrams as a typical grouping and hash key design problem.

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An anagram is a word formed by rearranging the letters of a different word, 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: Anagram groups identified by shared sorted signature.

Example 2

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

Explanation: Single empty string forms its own group.

Approaches

1. Sorted string as hash key

For each string, sort its characters to produce a canonical key. Group strings by this key in a Map.

Time
O(n * k log k)
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: O(n * k log k) where k is the max string length. Simple and correct. The sort per string is the bottleneck — mention it explicitly and offer the frequency-vector approach as an optimization.

2. Frequency vector as hash key

For each string, build a 26-element character count array and serialize it as a string key (e.g., '1#0#0...#1#'). This avoids sorting.

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

Tradeoff: O(n * k) time — avoids the sort. Separator '#' between counts prevents key collisions like '1,11' vs '11,1'. Mention this collision risk explicitly; Akamai interviewers value correctness reasoning around hash keys.

Akamai-specific tips

Discuss the key design trade-off upfront: 'My two options are: sort each string in O(k log k), or build a 26-integer frequency vector in O(k). The vector approach is asymptotically better but requires careful separator choice to avoid hash collisions.' Akamai interviewers prize this level of hash-key design reasoning — it directly maps to cache key engineering in their systems.

Common mistakes

  • Using freq.join('') without a separator — '12' and '1' + '2' collide ('a*12 b' vs 'a b*12').
  • Storing the result as a Set of arrays — JavaScript Sets use reference equality; two different array objects are never equal.
  • Forgetting single-character and empty-string edge cases — both should form their own groups.

Follow-up questions

An interviewer at Akamai 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 variant.
  • What if the strings contained Unicode characters instead of only lowercase 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 use '#' as a separator in the frequency key?

Without a separator, counts like [1,12] and [11,2] would both serialize to '112', causing a collision. A separator that doesn't appear in any count value guarantees uniqueness.

Is O(n * k log k) acceptable for the given constraints?

Yes — n=10^4 and k=100 gives 10^6 * log(100) ≈ 7×10^6 operations. Well within time limits. But always mention the O(n * k) alternative to show you know the optimum.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →