47. Group Anagrams
mediumAsked at SalesforceGroup strings that are anagrams of each other. Salesforce uses this to test canonical-form hashing — they want O(n*k) with a frequency-tuple key.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses anagram-like canonicalization in their deduplication-by-content logic.
- Blind (2025-12)— Common Salesforce onsite question.
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 key
Sort the string's characters; use the sorted version as a hash key.
- 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 per string costs O(k log k). Acceptable but Salesforce prefers the frequency version.
2. Frequency tuple as key
For each string, build a 26-int count array; serialize as key.
- 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 c of s) count[c.charCodeAt(0) - 97]++;
const key = count.join(',');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(s);
}
return [...groups.values()];
}Tradeoff: O(k) per string vs O(k log k) for sort. The frequency vector is the canonical form.
Salesforce-specific tips
Salesforce uses canonical-form hashing in their record-deduplication logic — same data, different field-order, must hash identically. They grade on whether you reach for the count-based canonicalization without prompting. Bonus signal: discuss that for very long strings, the frequency-vector approach wins; for short strings, sorting can be faster due to cache effects.
Common mistakes
- Using the string itself as key — only catches identical strings, not anagrams.
- Joining the count array without separator — '1', '11' collide for ['a'] and ['a','a'].
- Forgetting to handle empty strings — works but worth confirming.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Valid Anagram (LC 242) — pairwise check.
- Find All Anagrams in a String (LC 438).
- Generalize to Unicode strings — Map<string, count>.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why include separator in the key?
Because count.join('') makes '1' and '11' collide. Using ',' as separator gives unique keys per count tuple.
Sort key or frequency key — which is better?
Frequency key is O(k) vs sort's O(k log k). For long strings (k > 50), frequency wins. For short strings, sort can be faster due to JS engine optimizations.
Practice these live with InterviewChamp.AI
Drill Group Anagrams and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →