DSA interview question
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.
bool isArmstrong(long long n); // sum of digits^k equals n, where k is the digit count
Input n | Output | Explanation |
|---|---|---|
| 153 | true | 1³ + 5³ + 3³ = 153 |
| 370 | true | 3³ + 7³ + 0³ = 370 |
| 9474 | true | 4 digits: 6561 + 256 + 2401 + 256 = 9474 |
| 123 | false | 1 + 8 + 27 = 36 |
| 7 | true | Every single-digit number: 7¹ = 7 |
| 0 | true | 0¹ = 0 |
| 10 | false | 1² + 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.)
pow from <cmath>?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in