# The Alphabet Score

> Every letter has a secret numeric value - what's your total?

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string, return the sum of alphabetic character values where a=1..z=26, case-insensitive. Non-alphabetic characters contribute 0.

## Worked solution and explanation

### Why this problem exists in real interviews

Character-to-value scoring tests whether you know the ord/chr ASCII trick or whether you'd build a 26-entry dict by hand. Interviewers grade this on how cleanly you state the alphabetic-only filter, how you handle case, and whether your final expression is a single sum() over a generator (Pythonic) or a manual accumulator (junior).

---

### Break down the requirements

#### Step 1: Lowercase once, filter alphabetic

Lowercase the entire string up front so you only need one ord-anchor (ord('a')). Use str.isalpha() to skip non-letters; this also correctly handles digits, punctuation, and whitespace as zero contributions.

#### Step 2: Convert each letter with ord arithmetic

For a lowercase letter c, the value is ord(c) - ord('a') + 1. This maps 'a' to 1 and 'z' to 26 in one expression with no lookup table. Memorizing this idiom saves you from constructing a 26-item dict, which is the most common over-engineering.

#### Step 3: Sum in one expression

Wrap the conversion in a generator expression inside sum(). It runs in C, allocates no intermediate list, and reads as the spec sentence: 'sum of alphabetic character values.' Empty input returns 0 with no special case.

---

### The solution

**Lowercase, filter, ord-arithmetic, sum**

```python
def convert_string_mapping(s: str) -> int:
    s = s.lower()
    return sum(ord(c) - ord('a') + 1 for c in s if c.isalpha())
```

> **Cost Analysis**
>
> Time is O(n) for one lowercase pass and one summation pass. Space is O(n) for the lowercased copy (Python strings are immutable). The generator expression avoids materializing a second list, so peak memory stays at the lowercased string.

> **Interviewers Watch For**
>
> Whether you reach for ord arithmetic instead of building a 26-entry dict, whether you handle case explicitly with .lower(), and whether you use isalpha() rather than a manual range check. Strong candidates also mention that isalpha() on Python strings is Unicode-aware and would accept letters outside a-z, which may or may not be desired.

> **Common Pitfall**
>
> Using c.isalpha() without lowercasing and then computing ord(c) - ord('a') + 1 for an uppercase letter, which gives negative or wrong values (uppercase A is ord 65, lowercase a is 97). Either lowercase first or branch on case. Another miss is filtering with c.isascii() and c.isalpha() together for safety, but then forgetting that isalpha() on Greek or Cyrillic letters returns True; clarify scope with the interviewer.

---

## Common follow-up questions

- How would the solution change for case-sensitive scoring (uppercase contributes 27..52)? _(branch on isupper() or build a small dict; show both.)_
- What if the alphabet weights are arbitrary (e.g., Scrabble values)? _(replace ord-arithmetic with a dict lookup; mention str.translate for very hot paths.)_
- How would you score a 10 GB text file without loading it all? _(stream chunk by chunk, accumulate the sum; note that lowercasing on chunk boundaries is safe for ASCII but not for some Unicode case-folding edge cases.)_

## Related

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