Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Count the digits k, then sum each digit raised to the power k. The number is an Armstrong (narcissistic) number iff that sum equals the original number. It's O(d) where d is the digit count, with O(1) space — the trap is using k = the digit count, not a hard-coded 3.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you parameterize the exponent by the digit count instead of assuming 3, and whether you preserve the original value before the digit loop mutates it.
Where candidates slip
Hard-coding the cube (works only for 3-digit numbers), or comparing the power-sum against an n that the loop has already reduced to 0.
How to stand out
Generalize cleanly to k = digit count, use integer power (not Math.pow with its float rounding), and note that single-digit numbers and the boundary 9474 / 9475 are good tests.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewIs n Armstrong?sum == original n?YesNoExplore the full treeCollapse the tree
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Count Digits, Then Sum Powers (Standard)
1Hint 1Explore hintHide hint
The exponent isn't fixed — it's how many digits the number has. So what's the very first thing to compute?
2Hint 2Explore hintHide hint
First find k = number of digits. Then loop over the digits with %10 //10, adding digit^k each time.
3Hint 3Explore hintHide hint
Compare the accumulated sum to the ORIGINAL n — but your loop just destroyed n. How will you keep the original?
A · Core idea
Two phases. First, count the digits k (since the exponent is the digit count). Second, walk the digits with the familiar % 10 / // 10 peel, adding digit^k to a running total. The number is Armstrong iff that total equals the original n.
Why the exponent must be the digit count. The definition raises each digit to the k-th power where k is the number of digits. 153 is 3 digits, so cubes; 1634 is 4 digits, so fourth powers. Candidates who hard-code 3 get 153 right and then fail every other length — a narrow correctness trap. Compute k first, separately.
Why you must preserve the original n. The summing loop repeatedly does n /= 10, so by the time the loop ends n is 0. If you then compare total == n, you're comparing against 0 and always get false (except for n = 0). Stash the original in a variable like original before the loop and compare against that.
Why integer power, not floating-point. Math.pow(5, 3) returns a double, and floating-point can land on 124.99999999 which truncates to 124. For an exact equality test that's poison. Multiply integers in a small loop, or use a long-based integer-pow helper, so every digit^k is exact.
Why a 64-bit accumulator. Even when n fits in 32 bits, the sum of several large powers can momentarily exceed Integer.MAX_VALUE. Accumulating in a long avoids a silent overflow that would corrupt the comparison.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 153Array state
digit 3
3³ = 27, total = 27
trueC · Code with teaching comments
D · Why it works and complexityO(d · log k) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
It's a fixed-point test: does the number equal a function (sum of digit powers) of its own digits? The two non-obvious correctness points are 'exponent = digit count' and 'compare against the saved original'.
ComplexityO(d · log k) time, O(1) space — There are d = O(log₁₀ n) digits; each contributes one power d^k computed in O(log k) multiplications (fast pow) or O(k) naive. Space is just a couple of accumulators.
E · Edge cases and pitfalls3Hard-coding the exponent to 3 — fails on 4+ digit Armstrong numbers like 1634.Explore edge casesHide edge cases
Pitfalls
- Hard-coding the exponent to 3 — fails on 4+ digit Armstrong numbers like 1634.
- Comparing total against the mutated n (now 0) instead of the saved original.
- Using Math.pow (double) and getting rounding errors on the equality test.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isArmstrong(int n) { int original = n; int k = countDigits(n); long total = 0; while (n > 0) {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Hard-coding the cube
The exponent is the digit count k, not 3. Cubing works for 153 but reports 1634 (a 4-digit Armstrong number) as false.
Explore the code comparisonHide the code comparison
Hard-coding the cube
The exponent is the digit count k, not 3. Cubing works for 153 but reports 1634 (a 4-digit Armstrong number) as false.
Don't write this
total += digit * digit * digit; // only correct for 3-digit numbersWrite this instead
int k = countDigits(original);
total += intPow(digit, k);2Comparing against the mutated n
The loop does n /= 10 until n is 0, so comparing total == n compares against 0 and is almost always false.
Explore the code comparisonHide the code comparison
Comparing against the mutated n
The loop does n /= 10 until n is 0, so comparing total == n compares against 0 and is almost always false.
Don't write this
while n > 0:
total += (n % 10) ** k
n //= 10
return total == n # n is now 0Write this instead
original = n
while n > 0:
total += (n % 10) ** k
n //= 10
return total == original3Floating-point power
Math.pow returns a double; 5³ might come back as 124.99999999 and truncate to 124, breaking the exact comparison.
Explore the code comparisonHide the code comparison
Floating-point power
Math.pow returns a double; 5³ might come back as 124.99999999 and truncate to 124, breaking the exact comparison.
Don't write this
total += (long) Math.pow(digit, k); // float roundingWrite this instead
total += intPow(digit, k); // integer multiplicationRevise
Carry the pattern into the interview
Pattern to remember
Sum of each digit raised to the digit-count power, compared back to n
Code pattern to memorize
k = number_of_digits(n)
total = 0
for each digit d of n:
total += d ** k
return total == nRules to remember
- 1The exponent is k = the number of digits, not always 3.
153uses cubes;1634uses fourth powers. Hard-coding3only passes 3-digit cases. - 2Compute the digit count before the digit-summing loop, because the loop destroys
nvia// 10. Save the original to compare against. - 3Use a 64-bit accumulator — for large
k,d^ksummed can exceed 32 bits even whennfits. - 4Single-digit numbers
0..9are all Armstrong numbers (d¹ = d). Don't accidentally exclude them. - 5Keep a copy of the original
n; comparing the sum against a mutatedn(now 0) always returns false.
Use this pattern when
- Checking the narcissistic / Armstrong property of a number
- Any 'does a digit-derived quantity equal the number itself' fixed-point test
- Generating Armstrong numbers up to a limit (same check in a loop)
- Demonstrating clean digit extraction plus integer power
Choose another pattern when
- You need digit *sum* (power 1) → that's the simpler sum-of-digits problem
- You need digit *count* statistics, not the power test → just count
- Arbitrary precision required → use BigInteger and integer pow, not floating-point
Interview takeaway
Armstrong is digit extraction plus 'raise to the digit-count power' — the whole interview lives in not hard-coding the exponent to 3 and in preserving the original number to compare against.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternDigit extraction plus a parameterized power, framed as a fixed-point check (n equals a function of its own digits). The reusable lessons: parameterize constants from the input (exponent = digit count) and never compare against a value your loop has mutated.
Practice queueDirect variations and pattern siblings
Direct follow-ups