# The Secret Twins

> Same letters, different disguises.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given two strings s1 and s2, return True if they are anagrams (same character multiset). Comparison is case-insensitive (treat 'A' and 'a' as equal).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string manipulation**, **frequency counting with dicts**, and **case normalization**. Interviewers use it to see if a candidate handles character counting cleanly without reaching for sorted-string shortcuts that hide understanding.

---

### Break down the requirements

#### Step 1: Normalize both strings to lowercase

Case-insensitive comparison means `'Listen'` and `'Silent'` should match. Convert both inputs with `.lower()` before counting.

#### Step 2: Count character frequencies for each string

Build a frequency dict for each string by iterating character by character. Two strings are anagrams only if their frequency maps are identical.

#### Step 3: Compare the two frequency dicts

If the dicts are equal, return `True`. Python dict equality checks both keys and values, so this is a single comparison.

---

### The solution

**Frequency map comparison**

```python
def is_anagram(s1: str, s2: str) -> bool:
    s1 = s1.lower()
    s2 = s2.lower()
    if len(s1) != len(s2):
        return False
    freq1 = {}
    for ch in s1:
        freq1[ch] = freq1.get(ch, 0) + 1
    freq2 = {}
    for ch in s2:
        freq2[ch] = freq2.get(ch, 0) + 1
    result = freq1 == freq2
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the length of the strings. Building each frequency map is a single pass.
> 
> **Space:** O(k) where k is the number of distinct characters. For English letters this is bounded by 26.

> **Interviewers Watch For**
>
> The early length check is a quick optimization that avoids unnecessary work. Candidates who skip it still get a correct answer, but miss a chance to show they think about short-circuit exits.

> **Common Pitfall**
>
> Sorting both strings and comparing is O(n log n). It works but is slower than the O(n) frequency approach. Interviewers notice when you pick the suboptimal path on a warm-up problem.

---

## Common follow-up questions

- What if you needed to ignore spaces and punctuation? _(Tests whether you can add a filtering step before counting.)_
- How would you check if one string is an anagram of any substring of another? _(Tests sliding window with frequency maps, a significant difficulty jump.)_
- What if the input contained unicode characters like accented letters? _(Tests awareness of unicode normalization (e.g. NFD vs NFC forms).)_

## Related

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