# The Letter Census

> Every crowd has its share of talkers and quiet ones.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string, return a dict with keys 'vowels' (count of a, e, i, o, u, case-insensitive) and 'consonants' (count of other alphabetic characters). Non-alphabetic chars are ignored.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **character classification combined with counting**, probing whether a candidate can separate vowels from consonants while ignoring non-alphabetic characters.

---

### Break down the requirements

#### Step 1: Define the vowel set

Use a set `{'a', 'e', 'i', 'o', 'u'}` for O(1) lookups.

#### Step 2: Iterate and classify each character

Check if alphabetic first, then classify as vowel or consonant.

#### Step 3: Return counts in a dict

Keys are `'vowels'` and `'consonants'`.

---

### The solution

**Character classification with set lookup**

```python
def letter_census(s: str) -> dict:
    vowels_set = {'a', 'e', 'i', 'o', 'u'}
    vowel_count = 0
    consonant_count = 0
    for ch in s:
        lower = ch.lower()
        if lower.isalpha():
            if lower in vowels_set:
                vowel_count += 1
            else:
                consonant_count += 1
    result = {'vowels': vowel_count, 'consonants': consonant_count}
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the string length.
> 
> **Space:** O(1). Fixed-size vowel set and two counters.

> **Interviewers Watch For**
>
> Whether you handle uppercase letters by normalizing to lowercase before checking.

> **Common Pitfall**
>
> Counting digits, spaces, or punctuation as consonants. The `isalpha()` check prevents this.

---

## Common follow-up questions

- What if you needed counts per character? _(Tests switching to a full frequency dict.)_
- How would you handle unicode vowels? _(Tests awareness of accented characters like 'e' with acute.)_
- What if the input is a file with millions of characters? _(Tests streaming line-by-line processing.)_

## Related

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