In high-precision financial systems, such as central bank digital currencies (CBDC) or cross-border settlement layers, transaction volumes can often exceed the capacity of standard 64-bit floating-point numbers. In JavaScript, Number.MAX_SAFE_INTEGER is $2^{53} - 1$ (or 9,007,199,254,740,991).
Once a transaction total crosses this threshold, the engine loses precision, leading to "penny-drop" errors that can be catastrophic in a banking context. While modern environments support the BigInt type, many legacy systems or cross-language middleware require data to be handled as strings to ensure zero precision loss during serialization.
Problem Statement
You are tasked with building a core utility for a banking ledger. Implement a function bigAdd(a, b) that takes two non-negative integers represented as strings and returns their sum as a string.
Constraints & Requirements:
No Native BigInt: You may not use the
BigInt()constructor or BigInt literals (e.g.,10n).Arbitrary Length: The strings can be hundreds of characters long, far exceeding the limits of the
Numbertype.Non-negative: You only need to handle non-negative integers.
Efficiency: The solution should ideally run in $O(\max(N, M))$ time, where $N$ and $M$ are the lengths of the input strings.
Examples
Input a | Input b | Output | Logic |
|
|
| Standard carry-over logic. |
|
|
| Exceeds |
|
|
| Edge case: identity. |
|
|
| Extremely large values. |