Complex Number Multiplication

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A complex number is given as a string of the form "real+imaginaryi", where real and imaginary are integers and i^2 = -1. A negative imaginary part is written after the plus sign, e.g. "1+-1i". Given two such strings num1 and num2, return their product in the same format: "a+bi" with a and b written as plain integers (a negative b appears as +-, and zero is written as 0).

Examples

Input: num1 = "1+1i", num2 = "1+1i"
Output: "0+2i"
(1 + i)(1 + i) = 1 + 2i + i^2 = 2i.
Input: num1 = "1+-1i", num2 = "1+-1i"
Output: "0+-2i"
(1 - i)(1 - i) = 1 - 2i + i^2 = -2i.

Constraints

  • `num1` and `num2` are valid complex numbers in the format above
  • The real and imaginary parts are integers in `[-100, 100]`
  • Inputs never contain `-0`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems