Group words into anagram lists
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates string manipulation, data-structure design, and algorithmic complexity analysis, including considerations for case sensitivity, non-ASCII characters, and handling very long strings.
Constraints
- 0 <= number of words <= 10^4
- 0 <= length of each word; total characters fit in memory
- Words consist of lowercase English letters a-z (empty strings allowed)
- Return groups in any order; words within a group in any order
Examples
Input: (["abc", "cab", "edf"],)
Expected Output: [["abc", "cab"], ["edf"]]
Explanation: 'abc' and 'cab' share counts {a:1,b:1,c:1}; 'edf' is alone.
Input: (["eat", "tea", "tan", "ate", "nat", "bat"],)
Expected Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Explanation: Three anagram groups; 'bat' has no partner.
Hints
- Sorting each string to form a key works but costs O(L log L) per word. Can you build a key in O(L) instead?
- Two words are anagrams iff they have the same letter-frequency vector. Use the 26-length count array as a hashable key (a tuple).
- Use a hash map from frequency-signature to the list of words sharing that signature; the map values are your answer.
- For Unicode input, replace the fixed 26-array with a count dict and key on its sorted (char, count) items.