47. Group Anagrams
mediumAsked at WorkdayGroup strings into anagram clusters. Workday uses this for canonical-key bucketing — same shape as grouping department-code aliases that all refer to the same cost center.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2026-Q1)— Workday SDE2 phone screen — classic hash-map question.
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. Pairwise anagram check
Check each pair, build groups via union-find or graph traversal.
- Time
- O(n^2 * k)
- Space
- O(n)
// quadratic — too slow at n=10^4Tradeoff: Quadratic. Won't pass.
2. Sort each string, hash-map group
Sorted version of each string is the canonical key. Map key -> list.
- 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].sort().join('');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(s);
}
return [...groups.values()];
}Tradeoff: Sort-as-canonical-key is simple. Faster: 26-char count tuple as key, O(n*k) total.
Workday-specific tips
Workday accepts either sort-key or count-tuple-key. Mention both; explain that count-tuple is faster but sort-key is shorter to write. Either is fine for the constraint sizes.
Common mistakes
- Using a Set instead of Map<string, list> — collapses anagrams into one entry.
- Sorting in place when input is shared — mutates caller's data.
- Forgetting empty-string edge case.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Valid Anagram (LC 242) — same vs different.
- What if strings can be very long? (Use count tuple.)
- Group Shifted Strings (LC 249) — anagram by shift.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Sort-key vs count-tuple?
Sort-key is O(k log k) per string. Count-tuple is O(k). For k <= 100, both are fine. Count-tuple wins at scale.
Empty string?
Sort-key of '' is ''. All empty strings hash to one bucket. Correct behavior.
Practice these live with InterviewChamp.AI
Drill Group Anagrams and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →