Spy Number

Java coding interview problem for Number Logic: Spy Number.

Checking whether a number is a Spy Number is one of the most common beginner-level Java coding interview questions.

This problem helps interviewers evaluate your understanding of:

  • Loops
  • Digit extraction
  • Arithmetic operators
  • Mathematical logic
  • Problem-solving skills

Spy Number problems are commonly asked in Java, C, C++, Python, and competitive programming interviews.


What is a Spy Number?

A Spy Number is a number whose sum of its digits is equal to the product of its digits.

In simple words,

Spy Number

=

Sum of Digits

=

Product of Digits

Mathematical Definition

If a number contains digits

d1

d2

d3

...

Then

Sum

=

d1 + d2 + d3 + ...

and

Product

=

d1 × d2 × d3 × ...

If

Sum = Product

then

The number is a Spy Number.

Example 1

Number

1124

Digits

1

1

2

4

Sum

1 + 1 + 2 + 4

=

8

Product

1 × 1 × 2 × 4

=

8

Since

8 = 8

Therefore

1124 is a Spy Number

Example 2

Number

123

Digits

1

2

3

Sum

1 + 2 + 3

=

6

Product

1 × 2 × 3

=

6

Therefore

123 is a Spy Number

Example 3

Number

1412

Digits

1

4

1

2

Sum

1 + 4 + 1 + 2

=

8

Product

1 × 4 × 1 × 2

=

8

Therefore

1412 is a Spy Number

Example 4

Number

145

Digits

1

4

5

Sum

1 + 4 + 5

=

10

Product

1 × 4 × 5

=

20

Since

10 ≠ 20

Therefore

145 is NOT a Spy Number

Some Spy Numbers

Number Digit Sum Digit Product Spy Number?
1124 8 8 ✅ Yes
123 6 6 ✅ Yes
1412 8 8 ✅ Yes
132 6 6 ✅ Yes
145 10 20 ❌ No
245 11 40 ❌ No

Real Interview Question

Write a Java program to check whether a given number is a Spy Number.


Understanding the Logic

Suppose

Number = 1124

Extract every digit.

Find

  • Sum of digits
  • Product of digits

Finally compare

Sum == Product

If true,

the number is a Spy Number.


Visual Representation

Input

1124

Processing

1124

↓

Digit = 4

↓

Sum = 4

Product = 4

↓

Digit = 2

↓

Sum = 6

Product = 8

↓

Digit = 1

↓

Sum = 7

Product = 8

↓

Digit = 1

↓

Sum = 8

Product = 8

↓

Compare

↓

8 == 8

↓

Spy Number

Brute Force Approach

The simplest solution is

  • Store the original number.
  • Initialize
    • Sum = 0
    • Product = 1
  • Extract one digit at a time.
  • Add the digit to the sum.
  • Multiply the digit with the product.
  • Remove the last digit.
  • Continue until the number becomes zero.
  • Compare the sum and product.

Algorithm

Step 1

Read the number.

Step 2

Store the original number.

original = number;

Step 3

Initialize

sum = 0;

product = 1;

Step 4

Extract the last digit.

digit = number % 10;

Step 5

Update

sum += digit;

product *= digit;

Step 6

Remove the last digit.

number /= 10;

Step 7

Repeat until

number = 0

Step 8

Compare

sum == product

Dry Run

Input

123

Initial

sum = 0

product = 1
Digit Sum Product
3 3 3
2 5 6
1 6 6

Comparison

6 == 6

Output

Spy Number

Another Dry Run

Input

145

Initial

sum = 0

product = 1
Digit Sum Product
5 5 5
4 9 20
1 10 20

Comparison

10 ≠ 20

Output

Not a Spy Number

Approach 1 — Using Iteration (Loop)

This is the simplest and most commonly asked interview solution.


Complete Java Program

public class SpyNumber {

    public static void main(String[] args) {

        int number = 1124;

        int original = number;

        int sum = 0;

        int product = 1;

        while (number > 0) {

            int digit = number % 10;

            sum += digit;

            product *= digit;

            number /= 10;

        }

        if (sum == product) {

            System.out.println(original + " is a Spy Number");

        } else {

            System.out.println(original + " is NOT a Spy Number");

        }

    }

}

Output

1124 is a Spy Number

Step-by-Step Code Explanation

Step 1

Declare the number.

int number = 1124;

Current value

1124

Step 2

Store the original value.

int original = number;

The original number is preserved because the variable

number

changes while extracting digits.


Step 3

Initialize

int sum = 0;

int product = 1;

Initially

sum = 0

product = 1

The product starts at 1 because multiplying by 0 would always result in 0.


Step 4

Extract the last digit.

int digit = number % 10;

For

1124

First extracted digit

4

Step 5

Update the sum and product.

sum += digit;

product *= digit;

Running values

Digit = 4

Sum = 4

Product = 4

Step 6

Remove the last digit.

number /= 10;

Values become

1124

↓

112

↓

11

↓

1

↓

0

Step 7

Compare

sum == product

If true,

the number is a Spy Number.

Otherwise,

it is not.


Example Execution

Input

Number = 132

Digits

1

3

2

Sum

1 + 3 + 2

=

6

Product

1 × 3 × 2

=

6

Output

132 is a Spy Number

Input

Number = 245

Digits

2

4

5

Sum

11

Product

40

Output

245 is NOT a Spy Number

Why Does This Work?

The algorithm extracts each digit of the number exactly once.

During each iteration:

  • The digit is added to the running sum.
  • The digit is multiplied into the running product.

After processing all digits, if the sum and product are equal, the number satisfies the mathematical definition of a Spy Number.


Advantages of This Approach

  • Easy to understand.
  • Uses simple loops.
  • Demonstrates digit extraction.
  • Uses constant extra memory.
  • Excellent for beginners.
  • Frequently asked in coding interviews.

Drawbacks

Although this solution is simple, interviewers often ask follow-up questions such as:

  • Can you create a reusable method?
  • Can you print all Spy Numbers within a range?
  • What is the time complexity?
  • How would you handle the number 0?
  • How does a Spy Number differ from a Neon Number or Strong Number?

In Part 2, we'll cover:

  • Reusable Method Approach
  • Printing Spy Numbers in a Range
  • Time and Space Complexity
  • Comparison of Approaches
  • Common Interview Mistakes
  • Interview Follow-up Questions
  • Related Coding Problems
  • Key Takeaways
  • Interview Tips

Approach 2 — Using a Reusable Method

Instead of writing the Spy Number logic inside the main() method, we can create a reusable method.

This approach improves:

  • Code readability
  • Reusability
  • Maintainability
  • Unit testing

Java Program

public class SpyNumberMethod {

    static boolean isSpy(int number) {

        int sum = 0;

        int product = 1;

        while (number > 0) {

            int digit = number % 10;

            sum += digit;

            product *= digit;

            number /= 10;

        }

        return sum == product;

    }

    public static void main(String[] args) {

        int number = 1124;

        if (isSpy(number)) {

            System.out.println(number + " is a Spy Number");

        } else {

            System.out.println(number + " is NOT a Spy Number");

        }

    }

}

Output

1124 is a Spy Number

Approach 3 — Print Spy Numbers in a Range

Interviewers sometimes ask:

Print all Spy Numbers between 1 and N.

We can reuse the isSpy() method.


Java Program

public class SpyNumbersRange {

    static boolean isSpy(int number) {

        int sum = 0;

        int product = 1;

        int temp = number;

        while (temp > 0) {

            int digit = temp % 10;

            sum += digit;

            product *= digit;

            temp /= 10;

        }

        return sum == product;

    }

    public static void main(String[] args) {

        int limit = 200;

        System.out.println("Spy Numbers:");

        for (int i = 1; i <= limit; i++) {

            if (isSpy(i)) {

                System.out.print(i + " ");

            }

        }

    }

}

Sample Output

Spy Numbers:

1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124

Note: The exact output depends on the selected range.


Dry Run (Reusable Method)

Input

Number = 123

Initial

Sum = 0

Product = 1

Processing

Digit = 3

↓

Sum = 3

Product = 3

↓

Digit = 2

↓

Sum = 5

Product = 6

↓

Digit = 1

↓

Sum = 6

Product = 6

↓

Compare

↓

6 == 6

↓

Return true

Time Complexity

Suppose the number contains

d

digits.


Basic Iterative Solution

Operation Complexity
Time O(d)
Space O(1)

Reusable Method

Operation Complexity
Time O(d)
Space O(1)

Printing Spy Numbers in a Range

Operation Complexity
Time O(n × d)
Space O(1)

where

n

is the upper limit.


Comparison of Approaches

Approach Time Space Recommended
Loop-Based Solution O(d) O(1) Good for Beginners
Reusable Method O(d) O(1) ✅ Best for Interviews
Range Solution O(n × d) O(1) Useful Follow-up

Common Mistakes

Mistake 1

Initializing product with 0.

Wrong

int product = 0;

Every multiplication becomes

0

Correct

int product = 1;

Mistake 2

Using addition instead of multiplication.

Wrong

product += digit;

Correct

product *= digit;

Mistake 3

Forgetting to remove the processed digit.

Wrong

while(number > 0) {

    sum += number % 10;

}

Correct

number /= 10;

Otherwise, the loop never terminates.


Mistake 4

Comparing with the original number.

Wrong

sum == number

Correct

sum == product

The definition of a Spy Number compares sum of digits with product of digits, not with the original number.


Mistake 5

Not using a temporary variable inside reusable methods.

Wrong

number /= 10;

If the original value is needed later, it will be lost.

Better

int temp = number;

Interview Follow-up Questions

Q1. What is a Spy Number?

Q2. Why is 1124 a Spy Number?

Q3. Is 123 a Spy Number?

Q4. Why should the product start with 1?

Q5. Print all Spy Numbers within a range.

Q6. What is the time complexity?

Q7. Can negative numbers be Spy Numbers?

Q8. Explain digit extraction using % and /.

Q9. Compare Spy Number and Neon Number.

Q10. Write a reusable method for checking Spy Numbers.


Related Coding Problems

  • Neon Number
  • Strong Number
  • Armstrong Number
  • Perfect Number
  • Palindrome Number
  • Reverse Number
  • Sum of Digits
  • Product of Digits
  • Harshad Number

Key Takeaways

  • A Spy Number is a number whose sum of digits equals the product of digits.
  • Examples include:
1

2

3

...

9

22

123

132

213

231

312

321

1124
  • Extract digits using:
digit = number % 10;
  • Remove digits using:
number /= 10;
  • Initialize:
sum = 0;

product = 1;
  • Compare:
sum == product
  • The solution runs in O(d) time and O(1) extra space, where d is the number of digits.

Frequently Asked Interview Questions

Q1. What is a Spy Number?

A Spy Number is a number whose sum of digits is equal to the product of digits.

Example

123

Sum = 1 + 2 + 3 = 6

Product = 1 × 2 × 3 = 6

Q2. Why is 22 a Spy Number?

Digits

2

2

Sum

2 + 2 = 4

Product

2 × 2 = 4

Since

4 = 4

it is a Spy Number.


Q3. Are all single-digit numbers Spy Numbers?

Yes.

For any single-digit number d:

Sum = d

Product = d

Therefore, every single-digit number from 1 to 9 is a Spy Number.


Q4. Can a number containing zero be a Spy Number?

Usually No, because the product becomes 0 whenever any digit is 0.

Example

120

Sum = 3

Product = 0

However, always verify by calculating both values.


Q5. What is the difference between a Spy Number and a Neon Number?

Spy Number Neon Number
Sum of digits = Product of digits Sum of digits of the square = Original number
Example: 123 Example: 9

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is a Spy Number."

Start by explaining the mathematical definition:

A Spy Number is a number whose sum of digits equals the product of digits.

Implement the loop-based solution using % to extract digits and / to remove them. Maintain two variables—sum initialized to 0 and product initialized to 1. Explain why the product must start with 1 instead of 0, as initializing it to 0 would make every multiplication result in 0. Mention that the algorithm runs in O(d) time with O(1) extra space, where d is the number of digits. Finally, show how the logic can be moved into a reusable isSpy() method and extended to print all Spy Numbers within a specified range.