Skip to main content

47. Group Anagrams

mediumAsked at Vercel

Group strings that are anagrams of each other. Vercel asks this for the canonical-key + hash-map pattern — same shape as dedup in their build cache where alphabetically reordered dependency arrays should hash to the same key.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; sorted-string key expected.
  • Blind (2026-Q1)Listed in Vercel runtime engineer 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. All-pairs anagram comparison

For each pair (i, j), check if they're anagrams; union-find or grouping by representative.

Time
O(n^2 * k log k)
Space
O(n)
// O(n^2). Mention only to motivate the canonical-key approach.

Tradeoff: Quadratic.

2. Sorted-string canonical key (optimal)

For each string, sort its characters to get a canonical key. Group by key.

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.split('').sort().join('');
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(s);
  }
  return [...groups.values()];
}

Tradeoff: O(n * k log k) where k is the average string length. For 26-letter lowercase, a count-tuple key is O(k) per string — slightly faster, harder to read.

Vercel-specific tips

Vercel grades the canonical-key approach. Bonus signal: offering the count-tuple alternative (`a:1,b:2,...,z:0`) which is O(k) per string instead of O(k log k). They like when you weigh readability vs constant-factor speed.

Common mistakes

  • Using Object instead of Map — works but can collide with prototype properties.
  • Forgetting to spread `[...groups.values()]` — returns the iterator, not the array.
  • Building the key with a String#concat per char — quadratic in JS.

Follow-up questions

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

  • Valid Anagram (LC 242) — pair check only.
  • Streaming variant — strings arrive one at a time.
  • Group anagrams across multiple alphabets / Unicode.

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-tuple key?

Sorted is O(k log k) per string but reads cleanly. Count-tuple is O(k) per string (with 26-letter alphabet) but the key string is fiddly. For an interview, sorted is usually fine; mention the alternative.

Why Map over plain object?

Preserves insertion order, accepts any key safely, and the .has/.get/.set API reads cleanly. Plain object works but tempts prototype-pollution bugs.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →