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.
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.
The pattern is String Parsing and Simulation.
real1 * real2 - imag1 * imag2.real1 * imag2 + imag1 * real2.Input: v1 = "1+1i", v2 = "1+1i"
a=1, b=1, c=1, d=1.(1 * 1) - (1 * 1) = 0.(1 * 1) + (1 * 1) = 2.
Result: "0+2i".Input: v1 = "1+-1i", v2 = "1+-1i"
a=1, b=-1, c=1, d=-1.(1 * 1) - (-1 * -1) = 1 - 1 = 0.(1 * -1) + (-1 * 1) = -2.
Result: "0+-2i".+ and i characters correctly, especially when negative numbers are involved (e.g., 1+-1i).ac - bd. Remember that i^2 = -1.i in the integer conversion, which will throw an error.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.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Solve the Equation | Medium | Solve | |
| Fraction Addition and Subtraction | Medium | Solve | |
| Multiply Strings | Medium | Solve | |
| Robot Bounded In Circle | Medium | Solve | |
| Add Strings | Easy | Solve |