OfferGenie
All Questions

How can I multiply two integers represented as arrays and return the product in the same format?

GoogleTechnicalDifficulty: Hard
Share on

Ready to answer it out loud?

Run a mock interview on this exact question and get instant AI feedback.

Practice this question

Question Explain

How can I calculate the product of two integers, each represented by an array of digits, and return the result in the same array format? Please provide a detailed explanation and step-by-step solution for solving this problem.

Answer Example

To solve the problem of multiplying two integers represented as arrays and returning the product in the same format, we need to simulate the multiplication process similar to how it is done manually on paper. Here's a detailed step-by-step solution:

Problem Understanding

  1. Input: Two arrays of digits where each element in the array corresponds to a digit of the integers to be multiplied. For example, [1, 2, 3] represents the integer 123.
  2. Output: An array of digits representing the product of the two input integers.

Solution Approach

To multiply two numbers represented as arrays:

  1. Initialization:

    • Let num1 and num2 be the arrays representing the numbers.
    • Initialize a result array result with zeros of a size equal to len(num1) + len(num2) since the maximum length of the product will be the sum of the lengths of both numbers.
  2. Multiplication:

    • Loop through each digit in num1 and num2 in reverse order (starting from the least significant digit).
    • Multiply each digit in num1 by each in num2 and add the result to the corresponding position in the result array. Take care of the carry by adding it to the next position.
  3. Handling Carry:

    • If a multiplication results in a two-digit number, the tens place is carried over to the next higher position.
  4. Removing Leading Zeros:

    • After performing all multiplications and additions, the result may contain leading zeros. These should be removed unless the result is zero (in which case, it should be [0]).

Let's see the solution in code:

def multiplyArrays(num1, num2):
    # Initialize result array with zeros
    m, n = len(num1), len(num2)
    result = [0] * (m + n)
    
    # Multiply each digit of num1 with each digit of num2
    for i in range(m - 1, -1, -1):
        for j in range(n - 1, -1, -1):
            mul = num1[i] * num2[j]  # Multiply digits
            sum = mul + result[i + j + 1]  # Add to the current position

            # Carry handling
            result[i + j + 1] = sum % 10    # Current digit
            result[i + j] += sum // 10      # Add carry to the next position

    # Remove leading zeros
    while len(result) > 1 and result[0] == 0:
        result.pop(0)

    return result

# Example Usage
num1 = [1, 2, 3]
num2 = [4, 5, 6]
product = multiplyArrays(num1, num2)
print("Product:", product)  # Output: [5, 6, 0, 8, 8] which represents 56088

Explanation:

  • The result array's length is m + n to accommodate all possible digits.
  • We iterate over each pair of digits in reverse to simulate the manual multiplication process.
  • Each multiplication updates the result array at the appropriate index accounting for any carry.
  • We remove leading zeros in the final result for a clean output.

This approach ensures that the product is computed correctly as it models the manual multiplication process precisely.