Group Anagrams
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Comparing every pair of strings to check for anagrams is the move that sinks people on this one. The Bloomberg OA reported in September 2020 asks you to group anagrams, and the input size makes the quadratic pairwise check a bad idea. The real answer is a hash table with a canonical key per string. This one is a known pattern, so it's very doable in 24-72 hours if you lock in the key idea. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the approach in real time.
The problem
Group the strings in strs so that two strings appear in the same group exactly when they are anagrams. For deterministic output, sort the strings inside each group and then sort the groups lexicographically by their first string. Function groupAnagrams(strs: String[]) → String[][] Examples Example 1 strs = ["eat","tea","tan","ate","nat","bat"] return = [["ate","eat","tea"],["bat"],["nat","tan"]] Example 2 strs = [""] return = [[""]]
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a canonical key. Every anagram maps to the same key, so you drop each string into a hash map bucket by that key. Two options: sort the characters of the string (cost k log k per string), or build a 26-count tuple if the strings are lowercase letters (cost k). Then collect the map values. The pitfall here is the output format. This version wants determinism, so sort the strings inside each group, then sort the groups by their first string. Skip that and your answer is correct but fails the comparison. Also handle the empty string. [""] must return [[""]], and an empty sorted key works fine as a map key. Don't use a mutable list as a key in languages that forbid it, join to a string or use a tuple. If you freeze on the live OA, StealthCoder is the hedge that surfaces this pattern fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Group Anagrams cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as group anagrams. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Group Anagrams FAQ
What's the trick to Group Anagrams?+
Give every anagram the same key and bucket by it in a hash map. Sorting the characters of each string is the simplest key. A 26-length count array works too and avoids the sort. Once grouped, just output the map's values in the required order.
How hard is this Bloomberg question really?+
It's a medium at most, and it's a standard hash table problem. If you've seen the sorted-key idea once, you can write it in a few minutes. The only real friction is the sorting requirement for deterministic output, which is easy to forget.
Why does the problem ask me to sort groups and strings?+
Hash maps don't guarantee order, so the expected output needs a fixed form. Sort each group's strings, then sort the list of groups by first element. Do this last, after grouping, so it doesn't change your keys or complexity much.
What's the time complexity I should state?+
With sorted keys it's O(n * k log k) for n strings of max length k, plus the final output sorting. With count-tuple keys it's O(n * k) for grouping. Space is O(n * k) for the map. Mention both if asked and say which you picked.
How do I prepare for this in 48 hours?+
Write the sorted-key version from scratch twice, then the count-tuple version. Test the empty string case and a single-string input. Then practice the final ordering step so you don't forget it. That covers nearly every failure mode on this problem.