Check whether a number equals the sum of its digits each raised to the number of digits, like 153 = 1³+5³+3³. Python loop practice with live tests.
The problem
An Armstrong number equals the sum of its digits, each raised to the power of the number of digits: 153 = 1³ + 5³ + 3³.
Return True if n (a non-negative integer) is one.
Examples
Example 1
Input
is_armstrong(153)
Expected output
True
Example 2
Input
is_armstrong(123)
Expected output
False
+ 4 hidden tests on Submit.
Edge cases to ask about
- Single digits are all Armstrong numbers
- Zero
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.
Pick both to reveal the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Digits list + sum of powers | O(d) | O(d) | d = number of digits. |
Walkthrough of the optimal approach (try it yourself first)
Find the number of digits k, raise each digit to k, add them up and compare with n. Here str(n) is the clearest way to get the digits.
Complexity: O(log n) time, O(log n) space. Work and the digit list are proportional to the number of digits.
Reveal the reference solution
def is_armstrong(n): digits = [int(d) for d in str(n)] power = len(digits) return sum(d ** power for d in digits) == n
Follow-ups interviewers ask
- List every Armstrong number below 100,000.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Check an Armstrong Number in Python?
The optimal solution runs in O(log n) time and O(log n) auxiliary space. Work and the digit list are proportional to the number of digits.
What follow-up questions do interviewers ask about Check an Armstrong Number?
List every Armstrong number below 100,000.
