Technical Interview Challenge: The "Signed Ledger" Arithmetic Engine
Context
In advanced financial engineering and scientific computing, precision is non-negotiable. Standard JavaScript numbers use the IEEE 754 format, which loses integer precision beyond $2^{53} - 1$. While unsigned "Big Addition" is a common problem, real-world systems must handle signed arithmetic—processing both credits (positive) and debits (negative) of arbitrary magnitude.
You are building the core arithmetic module for a high-precision calculator. This module must handle the mathematical signs manually, effectively simulating how a CPU performs arithmetic at the bit level, but using decimal strings.
Problem Statement
Implement a function bigAddSigned(a, b) that takes two strings representing signed integers and returns their sum as a string.
Requirements:
Sign Handling: Correctly process inputs that are positive (e.g.,
"123"), negative (e.g.,"-456"), or zero ("0").No Native BigInt: You are strictly forbidden from using the
BigIntconstructor or BigInt literals.Arbitrary Precision: The strings may contain hundreds of digits, far exceeding the limits of the
Numbertype.Mathematical Correctness:
Adding two negatives:
(-A) + (-B) = -(A + B)Adding a positive and a negative:
A + (-B) = A - B
Clean Output: Resulting strings should not have unnecessary leading zeros (e.g., return
"0", not"-0"or"007").
Example Use Cases
Example 1: Opposite Signs (Subtraction Logic)
JavaScript
Example 2: Both Negative (Addition Logic)
JavaScript