# Caesar Shift Check

> The key turns. Does it open?

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

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given two equal-length strings a and b consisting of lowercase letters, return True if there exists an integer k (0..25) such that every character of b equals the corresponding character of a shifted by exactly k (wrapping z->a). Return False otherwise.

## Worked solution and explanation

### Why this problem exists in real interviews

This probes **modular arithmetic** and **character ordinal manipulation**, both essential for string transformation problems. It reveals whether a candidate can reason about wrap-around behavior and derive a consistent shift from character pairs.

> **Trick to Solving**
>
> Compute the shift from the first character pair using `(ord(b[0]) - ord(a[0])) % 26`. Then verify every subsequent pair produces the same shift. The modulo handles the z-to-a wraparound.

---

### Break down the requirements

#### Step 1: Check length equality

Different-length strings cannot be Caesar shifts of each other. Return False immediately.

#### Step 2: Compute the expected shift from the first pair

Use `(ord(b[0]) - ord(a[0])) % 26` to get the shift. The modulo ensures negative differences wrap correctly.

#### Step 3: Verify all remaining pairs match the shift

For each position, check that `(ord(b[i]) - ord(a[i])) % 26` equals the expected shift. If any pair disagrees, return False.

---

### The solution

**Single-pass shift verification with modular arithmetic**

```python
def can_shift(a, b):
    if len(a) != len(b):
        return False
    if not a:
        return True
    expected_shift = (ord(b[0]) - ord(a[0])) % 26
    for i in range(len(a)):
        actual_shift = (ord(b[i]) - ord(a[i])) % 26
        if actual_shift != expected_shift:
            return False
    return True
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the string length. One pass through both strings.
> 
> **Space:** O(1). Only stores the expected shift value.

> **Interviewers Watch For**
>
> Using `% 26` to handle wraparound. Candidates who write if-else chains for the z-to-a boundary are correct but miss the elegant modular arithmetic approach.

> **Common Pitfall**
>
> Forgetting to handle empty strings. Two empty strings are trivially a valid Caesar shift of each other.

---

## Common follow-up questions

- What if the strings contain uppercase and lowercase letters? _(Tests whether you normalize case before computing shifts or handle separate ranges.)_
- How would you return the shift value itself instead of a boolean? _(Tests minor API design: return the shift or -1 if no valid shift exists.)_
- What if the alphabet is not English but an arbitrary character set? _(Tests generalization of modular arithmetic to arbitrary alphabet sizes.)_

## Related

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