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.
Input: ([],)
Expected Output: []
Explanation: Empty input yields no groups.
Input: (["a"],)
Expected Output: [["a"]]
Explanation: Single word forms a single group.
Input: (["", ""],)
Expected Output: [["", ""]]
Explanation: Two empty strings are anagrams of each other (identical empty count vector).
Input: (["abc", "def", "ghi"],)
Expected Output: [["abc"], ["def"], ["ghi"]]
Explanation: All distinct signatures, so three singleton groups.
Input: (["listen", "silent", "enlist", "google", "banana"],)
Expected Output: [["listen", "silent", "enlist"], ["google"], ["banana"]]
Explanation: 'listen'/'silent'/'enlist' are anagrams; 'google' and 'banana' stand alone.
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.