Multiply Strings - Problem

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Input & Output

Example 1 — Basic Multiplication
$ Input: num1 = "2", num2 = "3"
Output: "6"
💡 Note: Simple single digit multiplication: 2 × 3 = 6
Example 2 — Multi-digit Numbers
$ Input: num1 = "123", num2 = "456"
Output: "56088"
💡 Note: Grade school multiplication: 123 × 456 = 56088 using digit-by-digit multiplication with carries
Example 3 — Zero Case
$ Input: num1 = "0", num2 = "9133"
Output: "0"
💡 Note: Any number multiplied by zero equals zero

Constraints

  • 1 ≤ num1.length, num2.length ≤ 200
  • num1 and num2 consist of digits only
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself

Visualization

Tap to expand
Multiply Strings - Grade School Multiplication INPUT num1 (as string) "2" num2 (as string) "3" Character Arrays: '2' idx 0 x '3' idx 0 digit1 = 2, digit2 = 3 (char - '0' conversion) ALGORITHM STEPS 1 Initialize Result Array Size = len1 + len2 = 2 0 0 2 Multiply Digits i=0, j=0: 2 x 3 = 6 2 * 3 = 6 3 Place in Result pos = i + j + 1 = 1 0 6 pos 1 4 Handle Carry carry = 6/10 = 0 digit = 6%10 = 6 FINAL RESULT Result Array: 0 6 leading 0 result Skip leading zeros Convert to String: "6" [OK] Product computed! "2" x "3" = "6" Time: O(m*n) Space: O(m+n) Key Insight: Grade school multiplication: multiply each digit pair and store at position (i + j + 1) in result array. Handle carries by propagating to position (i + j). Result array size is always len(num1) + len(num2). This avoids BigInteger by processing digit-by-digit, just like manual multiplication on paper. TutorialsPoint - Multiply Strings | Grade School Multiplication Approach
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen