49. Group Anagrams
mediumAsked at IBMGroup Anagrams is IBM's hash-keyed-bucketing screener. The interviewer is grading whether you pick a canonical key (sort the chars OR a frequency tuple), articulate the tradeoff between O(k log k) sort and O(k) counter, and ship the bucket map cleanly.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in IBM loops.
- Glassdoor (2026-Q1)— IBM SWE-2 / SWE-3 onsite recurring problem.
- LeetCode (2026-Q1)— Top-30 IBM-tagged problem (medium tier).
- GeeksforGeeks (2025-12)— Listed in IBM interview-experience archive.
Problem
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
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 chars as the bucket key
Sort each string's characters; group by the sorted string.
- Time
- O(n * k log k)
- Space
- O(n * k)
function groupAnagramsSort(strs) {
const buckets = new Map();
for (const s of strs) {
const key = s.split('').sort().join('');
const arr = buckets.get(key) ?? [];
arr.push(s);
buckets.set(key, arr);
}
return [...buckets.values()];
}Tradeoff: Cleanest one-liner key derivation. The k log k sort dominates per-string; for short strings (k <= 100) this is fine. Mention the counter-key variant as the asymptotically faster alternative.
2. Frequency-tuple key (optimal)
Build a 26-element count array for each string; serialize it as the bucket key.
- Time
- O(n * k)
- Space
- O(n * k)
function groupAnagrams(strs) {
const buckets = new Map();
const A = 'a'.charCodeAt(0);
for (const s of strs) {
const counts = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) counts[s.charCodeAt(i) - A]++;
const key = counts.join(',');
const arr = buckets.get(key) ?? [];
arr.push(s);
buckets.set(key, arr);
}
return [...buckets.values()];
}Tradeoff: Asymptotically faster than the sort variant (O(k) vs O(k log k) per string) but uses a wider key (the 26-element serialized tuple). At the constraint k <= 100, both run in milliseconds; the counter variant wins clearly at k = 10000.
IBM-specific tips
IBM grades the explicit complexity tradeoff between the two key derivations. State 'sort gives O(k log k) per string but a compact key; the counter gives O(k) per string but a longer key' before coding. Pick the counter variant for the final code if the interviewer asks 'which is faster?' but the sort variant is fine if they say 'just ship it.' Naming both signals depth.
Common mistakes
- Using the JavaScript Object as the bucket map and stringifying the key as JSON — works but slower than Map and risks prototype-key collisions.
- Forgetting that empty string is a valid input — the sort key is empty, the counter key is 26 zeros, both work fine if you don't special-case.
- Returning the Map's entries instead of just the values — drops the answer.
- Joining the counter key with a single character separator that could appear in another count — using ',' is safe; using ':' or '#' would also work.
Follow-up questions
An interviewer at IBM may pivot to one of these next:
- What if the input is Unicode? (Counter loses; switch to a Map<char, count> serialized to a deterministic string.)
- What if you only need the GROUP COUNT, not the groups? (Track unique keys; skip the buckets.)
- Streaming variant — emit each group as soon as you see a member.
- Find All Anagrams in a String (LeetCode 438) — sliding-window cousin.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Which key derivation is the IBM-preferred answer?
The frequency counter — it's strictly asymptotically faster and the question is medium-tier, so the interviewer expects you to know it. The sort key is acceptable on a phone screen as long as you also mention the counter as the asymptotic optimal. Senior-loop candidates who only ship sort get a 'meets bar' rating.
Why use Map over Object for the buckets?
Map preserves insertion order, handles numeric keys cleanly, and has explicit .has() / .get() semantics that don't collide with prototype keys like '__proto__' or 'constructor'. For interview-quality code, Map is the safer default.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Group Anagrams and other IBM interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →