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.
A complex number can be represented as a string on the form "real+imaginaryi" where:
real is the real part and is an integer in the range [-100, 100].imaginary is the imaginary part and is an integer in the range [-100, 100].i2 == -1.Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
Example 1:
Input: num1 = "1+1i", num2 = "1+1i" Output: "0+2i" Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:
Input: num1 = "1+-1i", num2 = "1+-1i" Output: "0+-2i" Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
Constraints:
num1 and num2 are valid complex numbers.Problem summary: A complex number can be represented as a string on the form "real+imaginaryi" where: real is the real part and is an integer in the range [-100, 100]. imaginary is the imaginary part and is an integer in the range [-100, 100]. i2 == -1. Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"1+1i" "1+1i"
"1+-1i" "1+-1i"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #537: Complex Number Multiplication
class Solution {
public String complexNumberMultiply(String num1, String num2) {
int[] x = parse(num1);
int[] y = parse(num2);
int a1 = x[0], b1 = x[1], a2 = y[0], b2 = y[1];
return (a1 * a2 - b1 * b2) + "+" + (a1 * b2 + a2 * b1) + "i";
}
private int[] parse(String s) {
var cs = s.substring(0, s.length() - 1).split("\\+");
return new int[] {Integer.parseInt(cs[0]), Integer.parseInt(cs[1])};
}
}
// Accepted solution for LeetCode #537: Complex Number Multiplication
func complexNumberMultiply(num1 string, num2 string) string {
x, _ := strconv.ParseComplex(num1, 64)
y, _ := strconv.ParseComplex(num2, 64)
return fmt.Sprintf("%d+%di", int(real(x*y)), int(imag(x*y)))
}
# Accepted solution for LeetCode #537: Complex Number Multiplication
class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
a1, b1 = map(int, num1[:-1].split("+"))
a2, b2 = map(int, num2[:-1].split("+"))
return f"{a1 * a2 - b1 * b2}+{a1 * b2 + a2 * b1}i"
// Accepted solution for LeetCode #537: Complex Number Multiplication
struct Solution;
use std::fmt;
use std::ops::Mul;
struct Complex {
r: i32,
i: i32,
}
impl Complex {
fn new(r: i32, i: i32) -> Self {
Complex { r, i }
}
fn from_string(s: String) -> Self {
let p = s.find('+').unwrap();
let n = s.len();
let r = s[0..p].parse::<i32>().unwrap();
let i = s[p + 1..n - 1].parse::<i32>().unwrap();
Self::new(r, i)
}
}
impl Mul for Complex {
type Output = Complex;
fn mul(self, rhs: Complex) -> Self::Output {
Self::new(
self.r * rhs.r - self.i * rhs.i,
self.r * rhs.i + self.i * rhs.r,
)
}
}
impl fmt::Display for Complex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}+{}i", self.r, self.i)
}
}
impl Solution {
fn complex_number_multiply(a: String, b: String) -> String {
(Complex::from_string(a) * Complex::from_string(b)).to_string()
}
}
#[test]
fn test() {
let a = "1+1i".to_string();
let b = "1+1i".to_string();
let res = "0+2i".to_string();
assert_eq!(Solution::complex_number_multiply(a, b), res);
let a = "1+-1i".to_string();
let b = "1+-1i".to_string();
let res = "0+-2i".to_string();
assert_eq!(Solution::complex_number_multiply(a, b), res);
}
// Accepted solution for LeetCode #537: Complex Number Multiplication
function complexNumberMultiply(num1: string, num2: string): string {
const [a1, b1] = num1.slice(0, -1).split('+').map(Number);
const [a2, b2] = num2.slice(0, -1).split('+').map(Number);
return `${a1 * a2 - b1 * b2}+${a1 * b2 + a2 * b1}i`;
}
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.