Complex Number Class
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in August 2020, and the trap is hiding in the formatting rules, not the math. Adding two complex numbers is trivial. Printing them is where a naive solution dies: zero terms vanish, 1 becomes i, -1 becomes -i, and a sum of 0 + 0i has to print as "0". It's a design-flavored problem with a simulation core. If you're taking this OA soon, memorize the format cases below. StealthCoder is the safety net running invisibly during the live OA if you blank on one of them, but you shouldn't need it here.
The problem
Construct two integer complex numbers, add them, compare them for equality, and format them. Return [formattedSum, equality, formattedFirst, formattedSecond]. Formatting omits zero terms, writes coefficient 1 as i and -1 as -i, and uses a + bi or a - bi when both terms are nonzero. Function complexNumberSummary(firstReal: int, firstImaginary: int, secondReal: int, secondImaginary: int) → String[] Examples Example 1 firstReal = 2 firstImaginary = -1 secondReal = -2 secondImaginary = 1 return = ["0","false","2 - i","-2 + i"] The numbers cancel under addition but are not equal. Constraints All parts fit in signed 32-bit integers and their sums do too.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a small class or two helper functions: add, equals, and format. Add sums real and imaginary parts separately. Equality compares both parts. The whole problem is the formatter. Handle cases in order. Both zero returns "0". Only real returns the number. Only imaginary returns "i", "-i", or "bi". Both nonzero returns real, then " + " or " - ", then the absolute imaginary part, with the coefficient dropped when it's 1. The common pitfall is formatting -1 as "-1i" or writing "2 + -i" instead of "2 - i". Another is returning the boolean as a real boolean when the output is a String array, so convert it to "true" or "false". Constraints say sums fit in 32-bit, so no overflow worry, but keep the sign logic on the original values. If your mind goes blank mid-assessment, StealthCoder can supply the formatter branches live.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Complex Number Class cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Complex Number Class FAQ
How hard is the Bloomberg Complex Number Class problem really?+
Easy on algorithm, medium on care. There's no data structure or clever trick. You lose points by missing a formatting branch, like a zero real part with imaginary -1. Write out all the cases before coding and test each one by hand.
What's the trick to the formatting?+
Split into three cases: both zero, one zero, neither zero. For the imaginary part, handle coefficient 1 and -1 as special strings. For the combined case, pick the sign from the imaginary part, then print its absolute value, dropping the 1.
What does the output array contain?+
Four strings in order: the formatted sum, the equality as "true" or "false", the formatted first number, and the formatted second number. In the example, 2 - i plus -2 + i gives "0", and the numbers aren't equal, so you get "false".
Which edge cases should I test?+
Test sum equal to zero, real part zero with imaginary 1 and -1, imaginary part zero, negative real with positive imaginary, and imaginary coefficient of exactly 1 or -1 next to a real part. Also check that identical inputs return "true" for equality.
How do I prepare for this in 48 hours?+
Write the formatter from scratch twice without notes, then run the example plus five edge cases. Practice a class-based layout too, since it's a design-style prompt. That's about an hour of work and covers everything this problem can throw at you.