PPractick

DSA interview question

Check if a Number is an Armstrong Number

Easy · Algorithms · Topics: basic maths, number theory

The problem

A maths-quiz app has a bonus round on Armstrong numbers (also called narcissistic numbers): a number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number itself. For example, 153 has 3 digits and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. The app needs a checker, and it also wants to list every Armstrong number below 10,000 for its hint screen. The problem combines two things from earlier: counting digits and extracting them one by one.

Example: 153 → true, 9474 → true (9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474), 123 → false.

Function signature

bool isArmstrong(long long n);  // sum of digits^k equals n, where k is the digit count

Test cases

Input nOutputExplanation
153true1³ + 5³ + 3³ = 153
370true3³ + 7³ + 0³ = 370
9474true4 digits: 6561 + 256 + 2401 + 256 = 9474
123false1 + 8 + 27 = 36
7trueEvery single-digit number: 7¹ = 7
0true0¹ = 0
10false1² + 0² = 1

(Confirm: the power is the number of digits of n, not always 3; n is non-negative. The Armstrong numbers below 10,000 are 0–9, 153, 370, 371, 407, 1634, 8208 and 9474.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the power the number of digits, or always 3?
  2. Can the number be negative or zero?
  3. What's the range of N? (Decides the integer type for the sum.)

Follow-ups to expect

  1. "Why not use pow from <cmath>?"
  2. "How do you find the number of digits?"
  3. "How many Armstrong numbers are there?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in