PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skills in string manipulation, grouping by character composition, and the use of hashing or sorting techniques to construct keys while reasoning about time and space trade-offs.

  • easy
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

Group strings that are anagrams

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Onsite

## Problem: Group anagrams Given an array of strings `strs`, group the strings that are anagrams of each other. Two strings are anagrams if they contain the same characters with the same multiplicities (order may differ). ### Input - `strs`: array of strings (assume lowercase English letters unless stated otherwise). ### Output Return a list of groups, where each group is a list of strings that are mutual anagrams. - The order of groups does not matter. - The order of strings inside a group does not matter. ### Constraints (typical) - `1 ≤ len(strs) ≤ 1e5` - `0 ≤ len(strs[i]) ≤ 100` ### Follow-ups discussed in interview - Provide **multiple approaches**, and compare time/space tradeoffs (e.g., sorting-based key vs. character-count key).

Quick Answer: This question evaluates skills in string manipulation, grouping by character composition, and the use of hashing or sorting techniques to construct keys while reasoning about time and space trade-offs.

Given an array of strings `strs`, group the strings that are anagrams of each other. Two strings are anagrams if they contain exactly the same characters with the same frequencies, but possibly in a different order. Return a list of groups, where each group contains strings that are mutual anagrams. For deterministic evaluation in this problem, follow these output-order rules: 1. Groups must appear in the order their anagram class is first seen in the input. 2. Strings inside each group must remain in their original input order. Example: `['eat', 'tea', 'tan', 'ate']` should become `[['eat', 'tea', 'ate'], ['tan']]`.

Constraints

  • 0 <= len(strs) <= 100000
  • 0 <= len(strs[i]) <= 100
  • Each string contains only lowercase English letters 'a' to 'z'

Examples

Input: (["eat", "tea", "tan", "ate", "nat", "bat"],)

Expected Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]

Explanation: "eat", "tea", and "ate" are anagrams; "tan" and "nat" are anagrams; "bat" stands alone.

Input: (["", "b", ""],)

Expected Output: [["", ""], ["b"]]

Explanation: Both empty strings belong to the same group, and that group appears first because the first element is an empty string.

Input: ([],)

Expected Output: []

Explanation: An empty input list produces no groups.

Input: (["ab", "ba", "ab", "cd", "dc", "e"],)

Expected Output: [["ab", "ba", "ab"], ["cd", "dc"], ["e"]]

Explanation: Duplicate strings are allowed and remain in the same anagram group in their original order.

Input: (["abc"],)

Expected Output: [["abc"]]

Explanation: A single string forms a group by itself.

Hints

  1. You need a way to map every string to an anagram signature so that all anagrams produce the same key.
  2. A sorted version of the string works as a key, but with lowercase letters only, a 26-character frequency count can be even more efficient.
Last updated: Apr 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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

  • Implement Integer Square Root - J.P. Morgan (medium)
  • Implement KNN from Scratch - J.P. Morgan (medium)
  • Minimize Operations to Balance Shipments - J.P. Morgan (medium)
  • Solve scheduling and collision problems - J.P. Morgan (medium)
  • Design an autocomplete suggestions API - J.P. Morgan (easy)