49. Group Anagrams
mediumAsked at GustoGroup strings that are anagrams of each other. Gusto asks this to test hash-map design and key construction — the sorted-string key is the canonical approach, but they may push you to the character-count key for O(n·k) without sorting.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Gusto loops.
- Glassdoor (2025-10)— Reported as a Gusto medium interview problem with a follow-up asking about the character-frequency key optimisation.
- Blind (2025-08)— Gusto threads mention Group Anagrams as a hash-map problem that surfaces often in mid-level SWE interviews.
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 another word, using all the original letters exactly once.
Constraints
1 <= strs.length <= 10^40 <= strs[i].length <= 100strs[i] consists of lowercase English letters.
Examples
Example 1
strs = ["eat","tea","tan","ate","nat","bat"][["bat"],["nat","tan"],["ate","eat","tea"]]Explanation: eat/tea/ate are anagrams; nat/tan are anagrams; bat stands alone.
Example 2
strs = [""][[""]]Explanation: Single empty string — one group.
Approaches
1. Sorted-string key
Sort each string's characters to produce a canonical key. Group strings by this key using a Map.
- Time
- O(n · k log k) where k is max string length
- Space
- O(n · k)
function groupAnagrams(strs) {
const groups = new Map();
for (const str of strs) {
const key = str.split('').sort().join('');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(str);
}
return [...groups.values()];
}Tradeoff: Clean and easy to explain. The sort cost is O(k log k) per string. Fine for k ≤ 100 as constrained here.
2. Character-count key
Build a 26-element count array for each string, stringify it as the key. Avoids sorting — O(k) per string instead of O(k log k).
- Time
- O(n · k)
- Space
- O(n · k)
function groupAnagrams(strs) {
const groups = 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 (!groups.has(key)) groups.set(key, []);
groups.get(key).push(str);
}
return [...groups.values()];
}Tradeoff: Asymptotically better for long strings — O(k) vs O(k log k). The tradeoff is a longer key (52+ chars) which is still O(k) but with a larger constant. Preferred when strings can be very long.
Gusto-specific tips
Gusto interviewers look for candidates who recognise that sorted-string key is correct and sufficient, then proactively offer the character-count optimisation as a follow-up. Name your variable `groups` or `anagramMap` — be specific. Also mention that the output order of groups and strings within groups doesn't matter per the problem statement.
Common mistakes
- Comparing sorted strings directly in a nested loop — O(n² · k log k), much worse than the map approach.
- Using an object literal instead of a Map — object keys must be strings, which works here but is semantically less precise.
- Not handling empty strings — they sort to '' and group correctly, but candidates often forget to trace through this case.
- Joining the count array with no separator — [1,10] and [11,0] would produce the same key '110'. Always use a separator.
Follow-up questions
An interviewer at Gusto may pivot to one of these next:
- What if strings can contain non-lowercase characters? How does the key change?
- How would you count distinct anagram groups without storing all the strings?
- Is there a way to detect anagrams in O(1) per comparison using hashing?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why join the count array with commas rather than nothing?
Without a separator, count arrays [1,10] and [11,0] both produce '110'. A comma delimiter ensures unambiguous keys.
What is the key for an empty string?
Sorted: the key is ''. Count-based: 26 zeros joined, e.g. '0,0,...,0'. Both correctly group all empty strings together.
Which approach does Gusto prefer?
Either is acceptable — start with sorted-string for clarity, then offer character-count as the asymptotic improvement. Gusto values the awareness of both.
Practice these live with InterviewChamp.AI
Drill Group Anagrams and other Gusto interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →