Complex Number Multiplication

Medium
1205.

Complex Number Multiplication

Complex Number Multiplication is a medium coding problem on strings, math and simulation, asked in Amazon and Apple interviews.

A complex number is written as "real+imaginaryi", where real and imaginary are integers (a negative one keeps its minus sign, so 1 − 4i is written "1+-4i"), and i² = −1.

Given two complex numbers num1 and num2 in that format, return their product in the same format: the real part, a +, the imaginary part, and a trailing i — even when a part is zero or negative.

Example 1

Input
num1 = "3+2i", num2 = "1+-4i"
Output
"11+-10i"
Explanation
(3 + 2i)(1 − 4i) = 3 − 12i + 2i − 8i² = 11 − 10i.

Example 2

Input
num1 = "-2+3i", num2 = "4+-1i"
Output
"-5+14i"

Example 3

Input
num1 = "0+0i", num2 = "-5+7i"
Output
"0+0i"

Constraints

  • num1 and num2 are valid complex numbers in the format above
  • -100 <= real, imaginary <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var complexNumberMultiply = function(num1, num2) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Complex Number Multiplication — Problem & Solution

Complex Number Multiplication solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.

Complex Number Multiplication is a medium coding problem on strings, math and simulation, asked in Amazon and Apple interviews.

  • Difficulty: Medium
  • Topics: Strings, Math, Simulation
  • Asked at: Amazon, Apple
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A complex number is written as "real+imaginaryi", where real and imaginary are integers (a negative one keeps its minus sign, so 1 − 4i is written "1+-4i"), and i² = −1.

Given two complex numbers num1 and num2 in that format, return their product in the same format: the real part, a +, the imaginary part, and a trailing i — even when a part is zero or negative.

Example 1

Input: num1 = "3+2i", num2 = "1+-4i"
Output: "11+-10i"
Explanation: (3 + 2i)(1 − 4i) = 3 − 12i + 2i − 8i² = 11 − 10i.

Example 2

Input: num1 = "-2+3i", num2 = "4+-1i"
Output: "-5+14i"

Example 3

Input: num1 = "0+0i", num2 = "-5+7i"
Output: "0+0i"

Constraints

  • num1 and num2 are valid complex numbers in the format above
  • -100 <= real, imaginary <= 100

Hints for Complex Number Multiplication

  1. Hint 1

    Split each string at the +: the part before is the real part, the part after (without the trailing i) is the imaginary part.

  2. Hint 2

    The real part never contains a +, so the first + is always the separator.

  3. Hint 3

    (a + bi)(c + di) = (ac − bd) + (ad + bc)i.

How to solve Complex Number Multiplication

Parse the two integer parts of each number, apply the product rule with i² = −1, and print the parts back in the same format.

Approach

  1. Find the + in each string; parse the text before it as a (or c) and the text between it and the final i as b (or d).
  2. Compute real = a·c − b·d and imag = a·d + b·c.
  3. Return real + "+" + imag + "i".

Why it works

Expanding (a + bi)(c + di) gives ac + adi + bci + bdi²; since i² = −1 the last term is real, which yields the two formulas. With parts in [−100, 100] the results stay within ±20 000, so ordinary integers are fine.

Complexity

  • Time — O(1) — the strings have constant length
  • Space — O(1)

Pitfalls

  • A negative imaginary part is printed as +-, never as a bare -.
  • Splitting on - breaks for negative real parts; split on + instead.
  • Forgetting the minus sign from i² = −1 makes the real part ac + bd.

Reference solution

Python

def complexNumberMultiply(num1: str, num2: str) -> str:
    a, b = num1[:-1].split("+")
    c, d = num2[:-1].split("+")
    a, b, c, d = int(a), int(b), int(c), int(d)
    return str(a * c - b * d) + "+" + str(a * d + b * c) + "i"

Java

public static String complexNumberMultiply(String num1, String num2) {
    int p = num1.indexOf('+'), q = num2.indexOf('+');
    int a = Integer.parseInt(num1.substring(0, p)), b = Integer.parseInt(num1.substring(p + 1, num1.length() - 1));
    int c = Integer.parseInt(num2.substring(0, q)), d = Integer.parseInt(num2.substring(q + 1, num2.length() - 1));
    return (a * c - b * d) + "+" + (a * d + b * c) + "i";
}

C++

string complexNumberMultiply(string num1, string num2) {
    size_t p = num1.find('+'), q = num2.find('+');
    int a = stoi(num1.substr(0, p)), b = stoi(num1.substr(p + 1, num1.size() - p - 2));
    int c = stoi(num2.substr(0, q)), d = stoi(num2.substr(q + 1, num2.size() - q - 2));
    return to_string(a * c - b * d) + "+" + to_string(a * d + b * c) + "i";
}

JavaScript

var complexNumberMultiply = function(num1, num2) {
    var p = num1.indexOf("+"), q = num2.indexOf("+");
    var a = parseInt(num1.substring(0, p), 10), b = parseInt(num1.substring(p + 1, num1.length - 1), 10);
    var c = parseInt(num2.substring(0, q), 10), d = parseInt(num2.substring(q + 1, num2.length - 1), 10);
    return (a * c - b * d) + "+" + (a * d + b * c) + "i";
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings