49. Group Anagrams
mediumAsked at RobinhoodGiven an array of strings, group the anagrams together. Robinhood asks this to test hash-key design: the right answer is to pick a canonical form (sorted chars or char-count signature) and bucket by it.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Robinhood loops.
- Glassdoor (2026-Q1)— Robinhood SWE-I phone-screen reports list Group Anagrams as a recurring 25-minute problem.
- Reddit r/leetcode (2025-12)— Robinhood new-grad trip reports cite Group Anagrams as a warm-up before a harder string follow-up.
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^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"]]Example 2
strs = [""][[""]]Example 3
strs = ["a"][["a"]]Approaches
1. Sort each string as the hash key
For each string, sort its chars and use the sorted form as the map key. Group all strings sharing a key.
- Time
- O(n * k log k) where k is max string length
- Space
- O(n * k)
function groupAnagramsSort(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: Cleanest code. The sort dominates per-string. Acceptable for k <= 100. Easy to read under interview pressure.
2. Character count signature (optimal)
Build a 26-length count array per string, serialize as a key, group.
- Time
- O(n * k)
- Space
- O(n * k)
function groupAnagrams(strs) {
const groups = 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 (!groups.has(key)) groups.set(key, []);
groups.get(key).push(s);
}
return [...groups.values()];
}Tradeoff: True O(n * k) — no per-string sort. Slightly more code but better asymptotic. Use when k could grow large.
Robinhood-specific tips
Robinhood interviewers care most about the hash-key design choice — articulate why sorted-string or count-signature is canonical: 'two anagrams share the same sorted form, so it's the natural bucket.' Cite both options, mention the asymptotic difference, and pick. Both are accepted answers. Be ready for the follow-up: 'what if the strings could be unicode?' (use a Map<char, count> instead of fixed 26-array).
Common mistakes
- Using the string itself as the key — misses all anagrams.
- Forgetting to handle empty strings — '' is its own anagram group of one.
- Joining the count array without a separator — count [1, 1, 0, ...] vs [11, 0, ...] would hash-collide if you used join('').
Follow-up questions
An interviewer at Robinhood may pivot to one of these next:
- Group Shifted Strings — group strings that share a shift pattern (a->b, b->c, ...).
- Find All Anagrams in a String (LC 438) — sliding-window count matching.
- Valid Anagram (LC 242) — simple 26-array equality.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Sort or count — which does Robinhood prefer?
Both are accepted. Sort is cleaner code; count is strictly better asymptotic. State the tradeoff, pick one, mention you could pivot.
Why use ',' as the separator in the count key?
Without it, [1, 1, 0, ...] and [11, 0, ...] would both serialize to '110...' and collide. The comma forces unambiguous parsing back to the count array.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Group Anagrams and other Robinhood interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →