PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Bloomberg
  • Coding & Algorithms
  • Software Engineer

Group words into anagram lists

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given an array of strings, group the words into lists of anagrams and return the collection of groups in any order. For example, ["abc", "cab", "edf"] -> [["abc", "cab"], ["edf"]]. Aim for near-linear time in the total number of characters. Specify how you handle case sensitivity, non-ASCII characters, and very long strings, and choose an approach that avoids sorting each string if possible.

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.

Given an array of lowercase strings, group the words into lists of anagrams and return the collection of groups in any order (and the words within each group in any order). Two words are anagrams if one can be formed by rearranging the letters of the other (each letter used the same number of times). Example: Input: ["abc", "cab", "edf"] Output: [["abc", "cab"], ["edf"]] Aim for near-linear time in the total number of characters. Prefer an approach that avoids sorting each string: build a frequency signature instead. Discussion points the interviewer looks for: - Case sensitivity: decide whether 'Abc' and 'cab' are anagrams; here treat input as lowercase a-z. For mixed case, normalize (e.g. lowercase) first per the agreed rule. - Non-ASCII: a fixed 26-slot count array assumes a-z; for Unicode, key on a hashable count map (e.g. a sorted tuple of (char, count)) instead of a 26-array. - Very long strings: counting is O(length) per string, so total work stays O(total characters); avoid per-string O(L log L) sorting.

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

  1. 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?
  2. Two words are anagrams iff they have the same letter-frequency vector. Use the 26-length count array as a hashable key (a tuple).
  3. Use a hash map from frequency-signature to the list of words sharing that signature; the map values are your answer.
  4. For Unicode input, replace the fixed 26-array with a count dict and key on its sorted (char, count) items.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimize Travel Assignment Cost - Bloomberg (medium)
  • Determine Balloon Popping Time - Bloomberg (medium)
  • Solve meeting and tree problems - Bloomberg (easy)
  • Minimize travel cost with two cities - Bloomberg (easy)
  • Check connectivity between two subway stations - Bloomberg (easy)