Magicsheet logo

Complex Number Multiplication

Medium
25%
Updated 8/1/2025

Asked by 2 Companies

Complex Number Multiplication

What is this problem about?

The Complex Number Multiplication interview question asks you to multiply two complex numbers given as strings in the format "real+imaginaryi" (e.g., "1+1i"). You must return the result in the same string format. A complex number multiplication follows the rule: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i.

Why is this asked in interviews?

Meta and Amazon use this Math interview pattern to test basic string manipulation and arithmetic logic. It's an "Easy/Medium" level problem that checks if a candidate can correctly parse input, handle negative numbers, and apply a mathematical formula without making silly errors. It’s more about "clean code" and "attention to detail" than complex algorithms.

Algorithmic pattern used

The pattern is String Parsing and Simulation.

  1. Split the input strings to extract the real and imaginary parts.
  2. Convert these parts to integers.
  3. Calculate the new real part: real1 * real2 - imag1 * imag2.
  4. Calculate the new imaginary part: real1 * imag2 + imag1 * real2.
  5. Format the result back into the required string.

Example explanation

Input: v1 = "1+1i", v2 = "1+1i"

  1. a=1, b=1, c=1, d=1.
  2. Real part: (1 * 1) - (1 * 1) = 0.
  3. Imaginary part: (1 * 1) + (1 * 1) = 2. Result: "0+2i".

Input: v1 = "1+-1i", v2 = "1+-1i"

  1. a=1, b=-1, c=1, d=-1.
  2. Real part: (1 * 1) - (-1 * -1) = 1 - 1 = 0.
  3. Imaginary part: (1 * -1) + (-1 * 1) = -2. Result: "0+-2i".

Common mistakes candidates make

  • Parsing Errors: Failing to handle the + and i characters correctly, especially when negative numbers are involved (e.g., 1+-1i).
  • Formula Error: Forgetting the minus sign in ac - bd. Remember that i^2 = -1.
  • Leading/Trailing characters: Including the i in the integer conversion, which will throw an error.

Interview preparation tip

Use built-in string splitting functions (like .split('+')) and then strip the last character (i) from the second part. This is usually cleaner and faster than using complex regular expressions.

Similar Questions