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 an integer n.
Return the concatenation of the hexadecimal representation of n2 and the hexatrigesimal representation of n3.
A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9 and the uppercase letters A - F to represent values from 0 to 15.
A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9 and the uppercase letters A - Z to represent values from 0 to 35.
Example 1:
Input: n = 13
Output: "A91P1"
Explanation:
n2 = 13 * 13 = 169. In hexadecimal, it converts to (10 * 16) + 9 = 169, which corresponds to "A9".n3 = 13 * 13 * 13 = 2197. In hexatrigesimal, it converts to (1 * 362) + (25 * 36) + 1 = 2197, which corresponds to "1P1"."A9" + "1P1" = "A91P1".Example 2:
Input: n = 36
Output: "5101000"
Explanation:
n2 = 36 * 36 = 1296. In hexadecimal, it converts to (5 * 162) + (1 * 16) + 0 = 1296, which corresponds to "510".n3 = 36 * 36 * 36 = 46656. In hexatrigesimal, it converts to (1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656, which corresponds to "1000"."510" + "1000" = "5101000".Constraints:
1 <= n <= 1000Problem summary: You are given an integer n. Return the concatenation of the hexadecimal representation of n2 and the hexatrigesimal representation of n3. A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9 and the uppercase letters A - F to represent values from 0 to 15. A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9 and the uppercase letters A - Z to represent values from 0 to 35.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
13
36
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3602: Hexadecimal and Hexatrigesimal Conversion
class Solution {
public String concatHex36(int n) {
int x = n * n;
int y = n * n * n;
return f(x, 16) + f(y, 36);
}
private String f(int x, int k) {
StringBuilder res = new StringBuilder();
while (x > 0) {
int v = x % k;
if (v <= 9) {
res.append((char) ('0' + v));
} else {
res.append((char) ('A' + v - 10));
}
x /= k;
}
return res.reverse().toString();
}
}
// Accepted solution for LeetCode #3602: Hexadecimal and Hexatrigesimal Conversion
func concatHex36(n int) string {
x := n * n
y := n * n * n
return f(x, 16) + f(y, 36)
}
func f(x, k int) string {
res := []byte{}
for x > 0 {
v := x % k
if v <= 9 {
res = append(res, byte('0'+v))
} else {
res = append(res, byte('A'+v-10))
}
x /= k
}
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return string(res)
}
# Accepted solution for LeetCode #3602: Hexadecimal and Hexatrigesimal Conversion
class Solution:
def concatHex36(self, n: int) -> str:
def f(x: int, k: int) -> str:
res = []
while x:
v = x % k
if v <= 9:
res.append(str(v))
else:
res.append(chr(ord("A") + v - 10))
x //= k
return "".join(res[::-1])
x, y = n**2, n**3
return f(x, 16) + f(y, 36)
// Accepted solution for LeetCode #3602: Hexadecimal and Hexatrigesimal Conversion
fn concat_hex36(n: i32) -> String {
fn f(n: i32, base: i32) -> String {
let mut v = vec![];
let mut n = n;
while n > 0 {
let m = n % base;
if m < 10 {
v.push((m as u8 + b'0') as char);
} else {
v.push(((m - 10) as u8 + b'A') as char);
}
n /= base;
}
v.reverse();
v.into_iter().collect()
}
let double = n * n;
let triple = n * n * n;
let s1 = f(double, 16);
let s2 = f(triple, 36);
format!("{s1}{s2}")
}
fn main() {
let ret = concat_hex36(36);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(concat_hex36(13), "A91P1");
assert_eq!(concat_hex36(36), "5101000");
}
// Accepted solution for LeetCode #3602: Hexadecimal and Hexatrigesimal Conversion
function concatHex36(n: number): string {
function f(x: number, k: number): string {
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let res = '';
while (x > 0) {
const v = x % k;
res = digits[v] + res;
x = Math.floor(x / k);
}
return res;
}
const x = n * n;
const y = n * n * n;
return f(x, 16) + f(y, 36);
}
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.