# The Letter Kin

> Words that share the same letters belong together.

Canonical URL: <https://datadriven.io/problems/the_letter_kin>

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a list of strings, group strings that are anagrams. Return a list of groups. Within each group preserve the input order. The outer list order is the order each group's first element appears in the input.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **anagram grouping using sorted character keys**, a classic hash-based grouping problem. It probes whether a candidate can derive a canonical form for anagram comparison and use it as a dict key.

> **Trick to Solving**
>
> Sort each word's characters to create a canonical key. All anagrams produce the same sorted key. Group words by this key using a dict of lists.

---

### Break down the requirements

#### Step 1: Compute a canonical key for each word

Sort the characters: `'eat'` and `'tea'` both become `'aet'`.

#### Step 2: Group words by their canonical key

Use a dict mapping sorted keys to lists of original words.

#### Step 3: Return the groups as a list of lists

Each group contains all words that are anagrams of each other.

---

### The solution

**Sorted-key anagram grouping**

```python
def group_anagrams(words: list) -> list:
    groups = {}
    for word in words:
        chars = sorted(word)
        key = ""
        for ch in chars:
            key += ch
        if key not in groups:
            groups[key] = []
        groups[key].append(word)
    result = []
    for group in groups.values():
        result.append(group)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n * k log k) where n is the number of words and k is the maximum word length. Sorting each word is O(k log k).
> 
> **Space:** O(n * k) for the groups dict.

> **Interviewers Watch For**
>
> Whether you use sorting vs character counting for the key. Both are valid; counting (26-element tuple) is O(k) vs O(k log k) for sorting.

> **Common Pitfall**
>
> Using the original word as the key. Anagrams have different spellings but the same character composition.

---

## Common follow-up questions

- How would you use character counts instead of sorting? _(Tests building a tuple of 26 counts as the dict key for O(k) key computation.)_
- What if the comparison should be case-insensitive? _(Tests normalizing to lowercase before computing the key.)_
- How would you find the largest anagram group? _(Tests sorting groups by length or tracking the max during construction.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_letter_kin)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.