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.
Move from brute-force thinking to an efficient approach using math strategy.
You are given an integer num.
A number num is called a Complete Prime Number if every prefix and every suffix of num is prime.
Return true if num is a Complete Prime Number, otherwise return false.
Note:
k digits of the number.k digits of the number.Example 1:
Input: num = 23
Output: true
Explanation:
num = 23 are 2 and 23, both are prime.num = 23 are 3 and 23, both are prime.true.Example 2:
Input: num = 39
Output: false
Explanation:
num = 39 are 3 and 39. 3 is prime, but 39 is not prime.num = 39 are 9 and 39. Both 9 and 39 are not prime.false.Example 3:
Input: num = 7
Output: true
Explanation:
true.Constraints:
1 <= num <= 109Problem summary: You are given an integer num. A number num is called a Complete Prime Number if every prefix and every suffix of num is prime. Return true if num is a Complete Prime Number, otherwise return false. Note: A prefix of a number is formed by the first k digits of the number. A suffix of a number is formed by the last k digits of the number. Single-digit numbers are considered Complete Prime Numbers only if they are prime.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
23
39
7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3765: Complete Prime Number
class Solution {
public boolean completePrime(int num) {
char[] s = String.valueOf(num).toCharArray();
int x = 0;
for (int i = 0; i < s.length; i++) {
x = x * 10 + (s[i] - '0');
if (!isPrime(x)) {
return false;
}
}
x = 0;
int p = 1;
for (int i = s.length - 1; i >= 0; i--) {
x = p * (s[i] - '0') + x;
p *= 10;
if (!isPrime(x)) {
return false;
}
}
return true;
}
private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #3765: Complete Prime Number
func completePrime(num int) bool {
isPrime := func(x int) bool {
if x < 2 {
return false
}
for i := 2; i*i <= x; i++ {
if x%i == 0 {
return false
}
}
return true
}
s := strconv.Itoa(num)
x := 0
for i := 0; i < len(s); i++ {
x = x*10 + int(s[i]-'0')
if !isPrime(x) {
return false
}
}
x = 0
p := 1
for i := len(s) - 1; i >= 0; i-- {
x = p*int(s[i]-'0') + x
p *= 10
if !isPrime(x) {
return false
}
}
return true
}
# Accepted solution for LeetCode #3765: Complete Prime Number
class Solution:
def completePrime(self, num: int) -> bool:
def is_prime(x: int) -> bool:
if x < 2:
return False
return all(x % i for i in range(2, int(sqrt(x)) + 1))
s = str(num)
x = 0
for c in s:
x = x * 10 + int(c)
if not is_prime(x):
return False
x, p = 0, 1
for c in s[::-1]:
x = p * int(c) + x
p *= 10
if not is_prime(x):
return False
return True
// Accepted solution for LeetCode #3765: Complete Prime Number
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3765: Complete Prime Number
// class Solution {
// public boolean completePrime(int num) {
// char[] s = String.valueOf(num).toCharArray();
// int x = 0;
// for (int i = 0; i < s.length; i++) {
// x = x * 10 + (s[i] - '0');
// if (!isPrime(x)) {
// return false;
// }
// }
// x = 0;
// int p = 1;
// for (int i = s.length - 1; i >= 0; i--) {
// x = p * (s[i] - '0') + x;
// p *= 10;
// if (!isPrime(x)) {
// return false;
// }
// }
// return true;
// }
//
// private boolean isPrime(int x) {
// if (x < 2) {
// return false;
// }
// for (int i = 2; i * i <= x; i++) {
// if (x % i == 0) {
// return false;
// }
// }
// return true;
// }
// }
// Accepted solution for LeetCode #3765: Complete Prime Number
function completePrime(num: number): boolean {
const isPrime = (x: number): boolean => {
if (x < 2) return false;
for (let i = 2; i * i <= x; i++) {
if (x % i === 0) {
return false;
}
}
return true;
};
const s = String(num);
let x = 0;
for (let i = 0; i < s.length; i++) {
x = x * 10 + (s.charCodeAt(i) - 48);
if (!isPrime(x)) {
return false;
}
}
x = 0;
let p = 1;
for (let i = s.length - 1; i >= 0; i--) {
x = p * (s.charCodeAt(i) - 48) + x;
p *= 10;
if (!isPrime(x)) {
return false;
}
}
return true;
}
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.