Multiply Two Non-Negative Integers Given as Strings Without Integer Conversion
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Multiply two non-negative integers given as decimal strings of up to 200 digits and return the product as a string, without big-integer libraries or direct conversion. It tests grade-school multiplication, mapping each digit product to the right output position, carry handling, and removing leading zeros.
Constraints
- 1 <= len(num1), len(num2) <= 200
- num1 and num2 consist of digits only.
- Neither input has leading zeros, except for the number zero itself, which is written "0".
- No built-in BigInteger or big-integer library, and no conversion of a whole input string to an integer. Converting a single digit character to its value is allowed.
- The product can have up to 400 digits, far larger than 2^31-1 or 2^63-1, so it is returned as a string.
Examples
Input: ('2', '3')
Expected Output: '6'
Explanation: Smallest operands, one digit each. The one-digit product leaves a leading zero slot that must be stripped.
Input: ('9', '9')
Expected Output: '81'
Explanation: Single digits whose product carries into a second digit.
Hints
- How many digits can the product of an n-digit number and an m-digit number have? Compare 99 x 99 with 10 x 10.
- Example 1 splits 456 into 400 + 56 and adds the partial products. Every piece of that work can be reduced to multiplying and adding single digits.
- A product of zero must come back as exactly "0". Remove extra zeros only from the front of the result, never from the middle or the end.