All questions

BigInt String Arithmetic

Premium

BigInt String Arithmetic

Arbitrary-precision arithmetic adds and multiplies numbers that are too large to fit in a machine word by operating on their decimal digits directly, exactly the way you would on paper. JavaScript's number type is an IEEE-754 double that stays exact only up to Number.MAX_SAFE_INTEGER (that is 2^53 - 1, or 9007199254740991); past that, calling Number('12345678901234567890') silently rounds the value before you have done any math. You will implement add and multiply for non-negative integers handed to you as decimal strings of any length, packaged on one object bigintStringArithmetic = { add, multiply } — no BigInt, no Number on the whole value, no libraries.

Signature

bigintStringArithmetic.add(a, b)       // (decimal string, decimal string) -> decimal string
bigintStringArithmetic.multiply(a, b)  // (decimal string, decimal string) -> decimal string

Examples

bigintStringArithmetic.add('2', '3');   // '5'
bigintStringArithmetic.add('99', '1');  // '100'
bigintStringArithmetic.add('12345678901234567890', '98765432109876543210');
// '111111111011111111100'  — a plain Number(a) + Number(b) drops the low digits
bigintStringArithmetic.multiply('123', '456');  // '56088'
bigintStringArithmetic.multiply('99', '99');    // '9801'
bigintStringArithmetic.multiply('12345678901234567890', '0');  // '0'

Notes

  • Inputs are canonical — each argument is a non-negative integer with no sign and no leading zeros, except '0' itself, which is exactly one zero. You do not need to validate them.
  • No shortcuts — you may not use BigInt, convert the whole string with Number or parseInt, or reach for a big-number library. Converting a single digit character is fine; converting the whole value is the bug.
  • Return a canonical string — strip any leading zeros so multiply('0', '5') returns '0', not '000', and add('1', '9') returns '10'.
  • Any length — inputs can be hundreds of digits long, so your solution must work digit by digit rather than lean on machine-word size.
  • Non-negative only — no subtraction, no negatives, no decimal points. Just addition and multiplication of whole numbers.

FAQ

Why can't I just use Number or parseInt on the strings?
A JavaScript number is an IEEE-754 double, exact only up to Number.MAX_SAFE_INTEGER (2^53 - 1, about 9.007 x 10^15). The moment you call Number on a longer value it rounds to the nearest representable double, silently corrupting the low digits before you have added or multiplied anything. Working on the digit characters keeps every digit exact.
What are the time complexities of add and multiply?
add is O(n) in the length of the longer input, since it sweeps each column once. multiply is O(n*m) for an n-digit and m-digit input — the schoolbook double loop over every digit pair. Faster multiplication (Karatsuba, FFT) exists but is out of scope.
Isn't this exactly what BigInt is for?
Yes — BigInt performs this digit-by-digit algorithm in native engine code, and in production you would just write (BigInt(a) * BigInt(b)).toString(). The exercise is to implement the underlying arithmetic by hand, which is why BigInt and whole-string Number conversion are off-limits here.

Unlock the solution & editor

  • Runnable editor + tests

    Solve in the browser with instant Jest feedback.

  • Detailed solutions

    Walkthroughs, edge cases, and complexity notes.

  • Multi-framework variants

    React, Vue, Vanilla, Angular — same question, different stacks.

Upgrade to Premium