Magicsheet logo

Super Palindromes

Hard
25%
Updated 8/1/2025

Asked by 2 Companies

Super Palindromes

What is this problem about?

The Super Palindromes coding problem is a unique math-heavy question that asks you to find the number of "super palindromes" in a given range [L, R]. A super palindrome is a positive integer that is a palindrome itself and is also the square of another palindrome. For example, 121 is a super palindrome because it is a palindrome and its square root is 11, which is also a palindrome. Given the potentially large range (up to 10^18), you cannot simply iterate through all numbers.

Why is this asked in interviews?

Google and Microsoft use this question to see if a candidate can handle large-scale numeric ranges and if they can apply efficient enumeration techniques. It tests your ability to work backwards—instead of checking every number to see if it's a super palindrome, you generate palindromes, square them, and then check if the square is also a palindrome and falls within the range. This "generation vs. checking" shift is a vital skill for high-performance computing.

Algorithmic pattern used

The core algorithmic pattern is Enumeration and Math. Since the maximum value is 10^18, the square root can be at most 10^9. We can generate all palindromes up to 10^9 by constructing them from their first half (e.g., to get 12321, start with 123). After generating a palindrome P, calculate P*P. If P*P is a palindrome and is within [L, R], count it. This reduces the search space from 10^18 down to roughly 2 * 10^4.5 (the number of palindromes up to 10^9), which is very manageable.

Example explanation

Suppose we want to find super palindromes between 1 and 500.

  1. Palindromes P up to sqrt(500) (~22):
    • P=1, P*P=1 (Palindrome, Yes!)
    • P=2, P*P=4 (Palindrome, Yes!)
    • P=3, P*P=9 (Palindrome, Yes!)
    • P=11, P*P=121 (Palindrome, Yes!)
    • P=22, P*P=484 (Palindrome, Yes!) These are all super palindromes in the range. Note that P=4, P*P=16 is not a super palindrome because 16 is not a palindrome.

Common mistakes candidates make

One error is failing to handle the long range correctly, resulting in overflow if not using 64-bit integers. Another mistake is trying to iterate from L to R directly, which is impossible given the constraints. Some candidates also forget that palindromes can have an odd or even number of digits, requiring two different generation logic paths (e.g., 12 -> 121 and 12 -> 1221).

Interview preparation tip

To prepare for the Super Palindromes interview question, practice palindrome generation techniques. Learn how to build a palindrome by taking a number, converting it to a string, reversing it, and appending it. Also, brush up on your Math interview pattern skills, specifically regarding properties of squares and symmetry. Understanding how to drastically reduce search spaces is a key trait of a senior engineer.

Similar Questions