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.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given a positive integer n.
Return the maximum product of any two digits in n.
Note: You may use the same digit twice if it appears more than once in n.
Example 1:
Input: n = 31
Output: 3
Explanation:
n are [3, 1].3 * 1 = 3.Example 2:
Input: n = 22
Output: 4
Explanation:
n are [2, 2].2 * 2 = 4.Example 3:
Input: n = 124
Output: 8
Explanation:
n are [1, 2, 4].1 * 2 = 2, 1 * 4 = 4, 2 * 4 = 8.Constraints:
10 <= n <= 109Problem summary: You are given a positive integer n. Return the maximum product of any two digits in n. Note: You may use the same digit twice if it appears more than once in n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
31
22
124
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3536: Maximum Product of Two Digits
class Solution {
public int maxProduct(int n) {
int a = 0, b = 0;
for (; n > 0; n /= 10) {
int x = n % 10;
if (a < x) {
b = a;
a = x;
} else if (b < x) {
b = x;
}
}
return a * b;
}
}
// Accepted solution for LeetCode #3536: Maximum Product of Two Digits
func maxProduct(n int) int {
a, b := 0, 0
for ; n > 0; n /= 10 {
x := n % 10
if a < x {
b, a = a, x
} else if b < x {
b = x
}
}
return a * b
}
# Accepted solution for LeetCode #3536: Maximum Product of Two Digits
class Solution:
def maxProduct(self, n: int) -> int:
a = b = 0
while n:
n, x = divmod(n, 10)
if a < x:
a, b = x, a
elif b < x:
b = x
return a * b
// Accepted solution for LeetCode #3536: Maximum Product of Two Digits
fn max_product(n: i32) -> i32 {
let mut v: Vec<_> = n.to_string().bytes().collect();
v.sort_unstable();
v.into_iter()
.rev()
.take(2)
.fold(1, |acc, b| acc * (b - b'0') as i32)
}
fn main() {
let ret = max_product(798);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(max_product(31), 3);
assert_eq!(max_product(22), 4);
assert_eq!(max_product(124), 8);
}
// Accepted solution for LeetCode #3536: Maximum Product of Two Digits
function maxProduct(n: number): number {
let [a, b] = [0, 0];
for (; n; n = Math.floor(n / 10)) {
const x = n % 10;
if (a < x) {
[a, b] = [x, a];
} else if (b < x) {
b = x;
}
}
return a * b;
}
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.