Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digits integers. Since the answer can be very large, return it modulo 1337.
Example 1:
Input: n = 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
Example 2:
Input: n = 1 Output: 9
Constraints:
1 <= n <= 8Problem summary: Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digits integers. Since the answer can be very large, return it modulo 1337.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2
1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #479: Largest Palindrome Product
class Solution {
public int largestPalindrome(int n) {
int mx = (int) Math.pow(10, n) - 1;
for (int a = mx; a > mx / 10; --a) {
int b = a;
long x = a;
while (b != 0) {
x = x * 10 + b % 10;
b /= 10;
}
for (long t = mx; t * t >= x; --t) {
if (x % t == 0) {
return (int) (x % 1337);
}
}
}
return 9;
}
}
// Accepted solution for LeetCode #479: Largest Palindrome Product
func largestPalindrome(n int) int {
mx := int(math.Pow10(n)) - 1
for a := mx; a > mx/10; a-- {
x := a
for b := a; b != 0; b /= 10 {
x = x*10 + b%10
}
for t := mx; t*t >= x; t-- {
if x%t == 0 {
return x % 1337
}
}
}
return 9
}
# Accepted solution for LeetCode #479: Largest Palindrome Product
class Solution:
def largestPalindrome(self, n: int) -> int:
mx = 10**n - 1
for a in range(mx, mx // 10, -1):
b = x = a
while b:
x = x * 10 + b % 10
b //= 10
t = mx
while t * t >= x:
if x % t == 0:
return x % 1337
t -= 1
return 9
// Accepted solution for LeetCode #479: Largest Palindrome Product
struct Solution;
impl Solution {
fn largest_palindrome(n: i32) -> i32 {
if n == 1 {
return 9;
}
let max = 10u64.pow(n as u32) - 1;
for i in (0..max).rev() {
let left: String = i.to_string();
let right: String = i.to_string().chars().rev().collect();
let palindrome = format!("{}{}", left, right);
if let Ok(value) = palindrome.parse::<u64>() {
let mut j = max;
while j * j >= value {
if value % j == 0 {
return (value % 1337) as i32;
}
j -= 1;
}
}
}
0
}
}
#[test]
fn test() {
let n = 2;
let res = 987;
assert_eq!(Solution::largest_palindrome(n), res);
let n = 1;
let res = 9;
assert_eq!(Solution::largest_palindrome(n), res);
let n = 5;
let res = 677;
assert_eq!(Solution::largest_palindrome(n), res);
}
// Accepted solution for LeetCode #479: Largest Palindrome Product
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #479: Largest Palindrome Product
// class Solution {
// public int largestPalindrome(int n) {
// int mx = (int) Math.pow(10, n) - 1;
// for (int a = mx; a > mx / 10; --a) {
// int b = a;
// long x = a;
// while (b != 0) {
// x = x * 10 + b % 10;
// b /= 10;
// }
// for (long t = mx; t * t >= x; --t) {
// if (x % t == 0) {
// return (int) (x % 1337);
// }
// }
// }
// return 9;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.