# The Scramble Check

> Same letters, different order - are these two strings secret twins?

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given two strings, return True if they have the same character multiset (they are permutations of each other). Comparison is case-sensitive.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **character frequency comparison**, the standard approach for anagram/permutation detection. It checks whether candidates use counting instead of sorting, which is more efficient.

---

### Break down the requirements

#### Step 1: Quick length check

If the strings have different lengths, they cannot be permutations.

#### Step 2: Count character frequencies in both strings

Build a frequency map for each string.

#### Step 3: Compare the frequency maps

If every character has the same count in both maps, the strings are permutations of each other.

---

### The solution

**Frequency map comparison for anagram check**

```python
def is_permutation(s1, s2):
    if len(s1) != len(s2):
        return False
    counts1 = {}
    for ch in s1:
        if ch in counts1:
            counts1[ch] += 1
        else:
            counts1[ch] = 1
    counts2 = {}
    for ch in s2:
        if ch in counts2:
            counts2[ch] += 1
        else:
            counts2[ch] = 1
    return counts1 == counts2
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the string length. Building both maps is O(n) and comparing them is O(k) where k is the alphabet size.
> 
> **Space:** O(k) for the frequency maps.

> **Interviewers Watch For**
>
> Do you check lengths first? This O(1) early exit avoids unnecessary work. Also, comparing dicts with `==` in Python compares all key-value pairs, which is clean and correct.

> **Common Pitfall**
>
> Sorting both strings and comparing. This is O(n log n) instead of O(n). For small inputs it does not matter, but the counting approach is strictly better.

---

## Common follow-up questions

- What if the check should be case-insensitive? _(Tests normalizing both strings to lowercase before counting.)_
- How would you check if one string can be formed from a subset of another? _(Tests checking that each count in the shorter string is less than or equal to the longer one.)_
- What if you needed to find all anagram groups in a list of strings? _(Tests grouping by sorted string or by frequency tuple as a hash key.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_scramble_check)
- [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.