Magicsheet logo

Armstrong Number

Easy
25%
Updated 8/1/2025

Asked by 1 Company

Topics

Armstrong Number

What is this problem about?

The Armstrong Number interview question is a classic mathematical challenge. An Armstrong number (also known as a narcissistic number) is an n-digit number that is equal to the sum of its own digits each raised to the power of n. For example, a 3-digit number like 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. This Armstrong Number coding problem focuses on basic arithmetic operations and digit manipulation.

Why is this asked in interviews?

Amazon and other top-tier companies use this question for entry-level roles to test a candidate's ability to extract digits from a number and use loops effectively. It assesses whether you can correctly identify the number of digits and perform exponentiation without built-in libraries if required.

Algorithmic pattern used

This follows the Math interview pattern. The core logic involves:

  1. Counting the total number of digits (k) in the input.
  2. Extracting each digit one by one using the modulo operator (% 10).
  3. Raising each digit to the power of k and summing them up.
  4. Comparing the final sum to the original number.

Example explanation

Let's check if 371 is an Armstrong Number.

  1. Digit Count: 371 has 3 digits. So, k = 3.
  2. Extraction & Power:
    • Last digit: 1^3 = 1
    • Middle digit: 7^3 = 343
    • First digit: 3^3 = 27
  3. Sum: 1 + 343 + 27 = 371.
  4. Result: Since the sum (371) equals the original number, it is an Armstrong number.

Common mistakes candidates make

  • Hardcoding Power: Assuming k is always 3, whereas it depends on the length of the number.
  • Data Type Overflow: Using standard integers for very large numbers where the power operation might exceed the storage limit.
  • Original Value Loss: Modifying the input variable while extracting digits and then having no reference to the original number for comparison.

Interview preparation tip

Always keep a copy of the original number before you start dividing it to extract digits. Practice digit extraction using % and / as it is a building block for many "Easy" level interview questions.

Similar Questions