← Booking.com Interview Insights
I knew the sorted-key hashmap approach pretty much immediately, sort each string, use it as a key, bucket the originals under it.
Use a hash map to group strings by a canonical key that is identical for all anagrams. For each string, compute the key (e.g., sorted characters or character count signature) and append the string to the corresponding group. Finally, return the groups as a list of lists.
Pro tip: Discuss the trade-offs between sorting each string (O(n * k log k)) and using a character count key (O(n * k)), where n is the number of strings and k is the maximum length. Mention that the count-based approach can be more efficient for long strings with small alphabets, but sorting is simpler and often fast enough.
Ask clarifying questions: Are all strings lowercase? Can the input be empty? Should the output groups be sorted? Confirm that anagrams are case-sensitive and that the order of groups and within groups does not matter.
Decide on a key that uniquely identifies anagrams. Common choices: sorted string (e.g., 'eat' -> 'aet') or a character count signature (e.g., 'a1e1t1'). Explain the trade-offs.
Iterate through the array, compute the key for each string, and use a hash map to map the key to a list of strings. Append the current string to the list for its key.
After processing all strings, return the values of the hash map as a list of lists. The order of groups is arbitrary.
State the time and space complexity. For sorting approach: O(n * k log k) time, O(n * k) space. For count approach: O(n * k) time, O(n * k) space. Mention that k is the max string length.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.