Find the largest palindrome that is a product of two n-digit numbers, modulo 1337

Quick Overview

Find the largest palindromic integer that is the product of two n-digit numbers, for n from 1 to 8, and return it modulo 1337. Tests generating candidates in decreasing order, bounding a factor search so it runs fast, and handling 16-digit integers without overflow.

Find the largest palindrome that is a product of two n-digit numbers, modulo 1337

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an integer `n`, return the largest palindromic integer that can be represented as the product of two `n`-digit integers. Since the answer can be very large, return it modulo `1337`. ### Function Signature ```python def largest_palindrome(n: int) -> int: ``` ### Rules - An `n`-digit integer is between `10^(n-1)` and `10^n - 1` inclusive. The two factors may be equal. - A palindromic integer reads the same forward and backward in decimal, for example `9009`. - Maximize the palindrome itself, then reduce it modulo `1337`. ### Constraints - `1 <= n <= 8` ### Examples ```text Input: n = 2 Output: 987 ``` The largest palindrome product of two 2-digit numbers is 9009 = 91 * 99. Since 1337 * 6 = 8022, 9009 mod 1337 = 9009 - 8022 = 987. ```text Input: n = 1 Output: 9 ``` Every two-digit palindrome is a multiple of 11, and 11 is a prime larger than any one-digit factor, so no two-digit palindrome qualifies. The largest palindrome product is 9 = 1 * 9 = 3 * 3, and 9 mod 1337 = 9. ```text Input: n = 3 Output: 123 ``` The largest palindrome product of two 3-digit numbers is 906609 = 913 * 993. Since 1337 * 678 = 906486, 906609 mod 1337 = 123.

Overview: Find the largest palindromic integer that is the product of two n-digit numbers, for n from 1 to 8, and return it modulo 1337. Tests generating candidates in decreasing order, bounding a factor search so it runs fast, and handling 16-digit integers without overflow.

PracHub logo
General
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digit integers. Since the answer can be very large, return it modulo 1337.

Function Signature

def largest_palindrome(n: int) -> int:

Rules

  • An n -digit integer is between 10^(n-1) and 10^n - 1 inclusive. The two factors may be equal.
  • A palindromic integer reads the same forward and backward in decimal, for example 9009 .
  • Maximize the palindrome itself, then reduce it modulo 1337 .

Constraints

  • 1 <= n <= 8

Examples

Input: n = 2
Output: 987

The largest palindrome product of two 2-digit numbers is 9009 = 91 * 99. Since 1337 * 6 = 8022, 9009 mod 1337 = 9009 - 8022 = 987.

Input: n = 1
Output: 9

Every two-digit palindrome is a multiple of 11, and 11 is a prime larger than any one-digit factor, so no two-digit palindrome qualifies. The largest palindrome product is 9 = 1 * 9 = 3 * 3, and 9 mod 1337 = 9.

Input: n = 3
Output: 123

The largest palindrome product of two 3-digit numbers is 906609 = 913 * 993. Since 1337 * 678 = 906486, 906609 mod 1337 = 123.