Quick 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.

Multiply Two Non-Negative Integers Given as Strings Without Integer Conversion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two non-negative integers `num1` and `num2` represented as strings, return the product of `num1` and `num2`, also represented as a string. You must not use any built-in `BigInteger` library or convert the inputs to integers directly. ### Function Signature ```python def multiply_strings(num1: str, num2: str) -> str: ``` ### Rules - Converting a single digit character to its value is allowed. Converting a whole input string with `int()` or a similar call, or using any big-integer library, is not. In Python this rules out `int(num1) * int(num2)`. - Return the product in base 10 with no leading zeros. A product of zero is returned as `"0"`. ### 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"`. ### Examples ```text Input: num1 = "123", num2 = "456" Output: "56088" ``` Splitting 456 into 400 + 56 gives 123 × 400 = 49200 and 123 × 56 = 6888, which sum to 56088. ```text Input: num1 = "999", num2 = "999" Output: "998001" ``` $(1000 - 1)^2 = 1000000 - 2000 + 1 = 998001$. Two 3-digit numbers can have at most a 6-digit product, and this one reaches that length. ```text Input: num1 = "0", num2 = "52" Output: "0" ``` Any product with zero is zero, and the answer is the single digit `"0"`, with no padding such as `"00"`.

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.

Given two non-negative integers `num1` and `num2` written as strings of decimal digits, return their product, also written as a string. Rules: - You must not use any built-in `BigInteger` or other big-integer library, and you must not convert a whole input string to an integer. In Python this rules out `int(num1) * int(num2)`. Converting a single digit character to its value is allowed. - Return the product in base 10 with no leading zeros. A product of zero is returned as `"0"`, with no padding such as `"00"`. Each input has up to 200 digits, so the product can have up to 400 digits. That is far larger than 2^31-1 and 2^63-1, and above 2^53, beyond which a double cannot store every integer exactly. The inputs and the result are strings in every language. Example 1: Input: num1 = "123", num2 = "456" Output: "56088" Splitting 456 into 400 + 56 gives 123 x 400 = 49200 and 123 x 56 = 6888, which sum to 56088. Example 2: Input: num1 = "0", num2 = "52" Output: "0" Any product with zero is zero, and the answer is the single digit "0". 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".

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

  1. 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.
  2. 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.
  3. 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.

Multiply Two Numbers Given as Digit Strings

mediumGeneralSoftware Engineer

Given two non-negative integers num1 and num2 written as strings of decimal digits, return their product, also written as a string.

Rules:

  • You must not use any built-in BigInteger or other big-integer library, and you must not convert a whole input string to an integer. In Python this rules out int(num1) * int(num2). Converting a single digit character to its value is allowed.
  • Return the product in base 10 with no leading zeros. A product of zero is returned as "0", with no padding such as "00".

Each input has up to 200 digits, so the product can have up to 400 digits. That is far larger than 2^31-1 and 2^63-1, and above 2^53, beyond which a double cannot store every integer exactly. The inputs and the result are strings in every language.

Example 1: Input: num1 = "123", num2 = "456" Output: "56088" Splitting 456 into 400 + 56 gives 123 x 400 = 49200 and 123 x 56 = 6888, which sum to 56088.

Example 2: Input: num1 = "0", num2 = "52" Output: "0" Any product with zero is zero, and the answer is the single digit "0".

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".

Examples

Example 1
Input
('2', '3')
Output
'6'
Notes
Smallest operands, one digit each. The one-digit product leaves a leading zero slot that must be stripped.
Example 2
Input
('9', '9')
Output
'81'
Notes
Single digits whose product carries into a second digit.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def multiply_strings(num1: str, num2: str) -> str:
Sample call
multiply_strings('2', '3')
Language
Case 1
Input
('2', '3')
Expected
'6'
Case 2
Input
('9', '9')
Expected
'81'

Browse More Questions