# The Self-Portrait Number

> Some numbers describe themselves perfectly.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a positive integer n with d digits, return True if the sum of each digit raised to the power d equals n. (Armstrong / narcissistic number check.)

## Worked solution and explanation

### Why this problem exists in real interviews

This probes **digit extraction**, **exponentiation**, and **basic loop mechanics**. It is a straightforward math problem that reveals whether a candidate can decompose an integer into its digits and apply an operation to each one cleanly.

---

### Break down the requirements

#### Step 1: Count the total number of digits

Convert the number to a string to get the digit count, or use repeated division. The exponent depends on this count.

#### Step 2: Extract each digit and raise it to the power of the digit count

Iterate through each digit, compute `digit ** num_digits`, and accumulate the sum.

#### Step 3: Compare the sum to the original number

If the accumulated sum equals the input, return `True`.

---

### The solution

**Digit extraction with exponentiation**

```python
def is_armstrong(n: int) -> bool:
    digits = str(n)
    num_digits = len(digits)
    total = 0
    for d in digits:
        total += int(d) ** num_digits
    result = total == n
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(d) where d is the number of digits. Each digit requires one exponentiation of at most d, which is O(log d) with fast exponentiation but practically constant for reasonable inputs.
> 
> **Space:** O(d) for the string representation of the number.

> **Interviewers Watch For**
>
> Can you articulate why the exponent is the total digit count, not a fixed value? Armstrong numbers are sometimes confused with simpler digit-sum problems.

> **Common Pitfall**
>
> Using `math.log10` to count digits introduces floating-point edge cases. `str(n)` is cleaner and avoids off-by-one errors at powers of 10.

---

## Common follow-up questions

- What happens for single-digit numbers? _(Every single-digit number is trivially an Armstrong number since d^1 = d.)_
- How would you find all Armstrong numbers below a given limit? _(Tests whether you can wrap the check in an outer loop efficiently.)_
- What if the input could be negative or zero? _(Tests defensive input handling and clarifying assumptions with the interviewer.)_

## Related

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