Multiply Numeric Strings
Medium About 25 min Free, no account needed
Schoolbook multiplication of numbers thousands of digits long.
The brief
Given two non-negative decimal numbers as strings, return their product as a string.
Parsing into built-in integers dies on the hidden 3,000-digit factors, which
overflow every fixed-width type. You will need to multiply the numbers digit by
digit yourself. Mind the leading zeros (and the "0" result).
Digit-by-digit is necessary but not sufficient. Accumulating each partial product
with result = digit + result copies the whole answer every time, which is
O(n^2) in the answer length and misses the budget. Build into a char[] or a
StringBuilder and reverse once.
Starter code
using System;
public class Solution
{
// TODO: return the product of two non-negative numeric strings.
public string Multiply(string a, string b)
{
throw new NotImplementedException();
}
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
What it practices
- String processing
- Parse, scan and match text character by character with correct edge cases.
- Math and number theory
- Replace enumeration with a formula: GCD, modular arithmetic, digit math and closed-form counting.
Related katas
- Add Binary Strings Easy Free Add two binary numbers far too long for any built-in integer.
- Base64 Decoded Length Easy Free The decoded size is pure arithmetic - no need to decode.
- Excel Column Number Easy Free Spreadsheet columns are base-26 with no zero digit.
- Decode String Medium Free Expand k[encoded] patterns, nesting included.