Automorphic Number

Asked atVisa
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given a positive integer N, determine whether it is an Automorphic Number.

A number is called an Automorphic Number if the square of the number ends with the number itself.

Input: N = 76 Output: true Explanation: 76 * 76 = 5776 → ends in 76

Input: N = 25 Output: true Explanation: 25 * 25 = 625 → ends in 25

Input: N = 7

  • 1 ≤ N ≤ 10⁶

cpp

class Solution {
public:
    bool isAutomorphic(int n) {
        // Your code goes here
    }
};

java

class Solution {
    public boolean isAutomorphic(int n) {
        // Your code goes here
    }
}

python

class Solution:
    def isAutomorphic(self, n: int) -> bool:
        # Your code goes here

javascript

class Solution {
    isAutomorphic(n) {
        // Your code goes here
    }
}

csharp

class Solution {
    public bool IsAutomorphic(int n) {
        // Your code goes here
    }
}

go

func isAutomorphic(n int) bool {
Stuck? Show a way to structure it+
  1. 01Count decimal digits, with zero as one digit
  2. 02Compute modulus 10^digits
  3. 03Compare square modulo modulus with the original number
  4. 04Use a wider numeric type or string arithmetic if needed

Reference answer

Then expect these follow-ups

  • How would you test very large integers?

    Tests: constraint adaptation

  • Which residues are automorphic in base 10?

    Tests: follow-up reasoning

Hiring now

Visa has 638 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Software Engineer Intern

    GB - London, United Kingdom

  • Sr. Software Engineer (3+ yrs, DevOps, Cloud, Coding, Infra)

    IN - Bengaluru, India

  • Sr Software Engineer (3-5 yrs of exp - Full stack development, Java, React, Gen AI)

    IN - Bengaluru, India

Or practice this question first

Next question