Skip to main content

49. Group Anagrams

mediumAsked at Snowflake

Group strings that are anagrams of each other. Snowflake asks this to test composite-key grouping — the same hashing pattern used in GROUP BY execution where you bucket rows by a normalized key.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)Snowflake execution-engine uses this as GROUP BY warm-up.
  • LeetCode Discuss (2025-12)Recurring at Snowflake new-grad onsites.

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

Example 3

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

Approaches

1. Pairwise anagram check

For each string, scan existing groups; if any member is an anagram, append; else start new group.

Time
O(n^2 * k)
Space
O(n * k)
function groupAnagrams(strs) {
  const groups = [];
  for (const s of strs) {
    let placed = false;
    for (const g of groups) {
      if (isAnagram(s, g[0])) { g.push(s); placed = true; break; }
    }
    if (!placed) groups.push([s]);
  }
  return groups;
  function isAnagram(a, b) {
    if (a.length !== b.length) return false;
    const cnt = new Array(26).fill(0);
    for (const c of a) cnt[c.charCodeAt(0) - 97]++;
    for (const c of b) cnt[c.charCodeAt(0) - 97]--;
    return cnt.every(x => x === 0);
  }
}

Tradeoff: Quadratic in number of strings. Don't ship.

2. Canonical-key hash map (optimal)

For each string, compute a canonical key (sorted chars OR count tuple). Group by key in a hash map.

Time
O(n * k log k) or O(n * k) with count key
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 c of s) count[c.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) with count key — avoids the sort. Exactly the GROUP BY hash-aggregate pattern.

Snowflake-specific tips

Snowflake interviewers want the count-key version (faster than sort-key for lowercase ASCII). Bonus signal: connect to hash aggregation — Snowflake's GROUP BY uses a hash table keyed on the group-by expressions, with the same 'canonical key extraction' step you'd use to normalize anagrams.

Common mistakes

  • Sorting characters as a key without explaining the asymptotic trade-off vs counting.
  • Using count.toString() — produces ambiguous keys due to multi-digit counts.
  • Returning a Map instead of an array of arrays.

Follow-up questions

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

  • Valid Anagram (LC 242).
  • Find All Anagrams in a String (LC 438).
  • How does GROUP BY hash-aggregate work in Snowflake?

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 or count-key?

For 26-letter ASCII, count-key is O(k) and unambiguous. For general Unicode, sorted-key is the safe default at O(k log k).

How does this map to GROUP BY?

GROUP BY hash-aggregate computes a hash of each row's group-by columns, buckets rows by hash, and aggregates within each bucket. Group Anagrams is the same shape with a custom key extractor.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →