Quick Overview

Subtract arbitrarily long decimal strings without integer conversion using digit-wise borrowing, unequal-length handling, and canonical leading-zero cleanup.

Subtract Arbitrarily Long Nonnegative Decimal Strings

Company: StackAdapt

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given two nonnegative integers as decimal strings num1 and num2, return `num1 - num2` as a decimal string. Assume num1 is at least num2. Inputs may exceed fixed-width integer limits. Do not convert an entire string to a built-in integer or big-integer type. ### Constraints & Assumptions Inputs contain decimal digits only. Return a canonical result without leading zeros, except that zero is `0`. Leading zeros in inputs may be accepted. ### Clarifying Questions to Ask - Is the nonnegative-result guarantee always valid? - Are leading zeros allowed, and must the output normalize them? ```hint Work from the least significant digit One borrow from the next column changes the current column by ten. ``` ### What a Strong Answer Covers - Right-to-left digit subtraction with borrow propagation. - Unequal input lengths and digit decoding without whole-number conversion. - Leading-zero cleanup and equal-value inputs. ### Follow-up Questions - How would you support a negative result when num1 is smaller? - How would the same algorithm generalize to another positional base?

Overview: Subtract arbitrarily long decimal strings without integer conversion using digit-wise borrowing, unequal-length handling, and canonical leading-zero cleanup.

Given nonempty decimal strings `num1` and `num2` representing nonnegative integers, return their difference `num1 − num2` as a decimal string. The numeric value of `num1` is at least that of `num2`. Inputs may have leading zeros. The result must have no leading zeros except when it is exactly `"0"`. Process individual digits. Do not convert either whole operand to a built-in integer or arbitrary-precision integer type.

Constraints

  • Each operand contains only ASCII decimal digits and has 1 through 2^31 − 1 characters.
  • The numeric value of num1 is at least that of num2.

Examples

Input: ('1000', '7')

Expected Output: '993'

Explanation: The borrow propagates across two zero columns.

Input: ('0042', '00000017')

Expected Output: '25'

Explanation: Leading zeros affect string length without changing either value.

Hints

  1. Leading zeros may make the string for the smaller value longer.

Subtract Two Decimal Strings

mediumStackAdaptSoftware Engineer

Given nonempty decimal strings num1 and num2 representing nonnegative integers, return their difference num1 − num2 as a decimal string. The numeric value of num1 is at least that of num2. Inputs may have leading zeros. The result must have no leading zeros except when it is exactly "0".

Process individual digits. Do not convert either whole operand to a built-in integer or arbitrary-precision integer type.

View full question (all parts)

Given two nonnegative integers as decimal strings num1 and num2, return num1 - num2 as a decimal string. Assume num1 is at least num2. Inputs may exceed fixed-width integer limits. Do not convert an entire string to a built-in integer or big-integer type.

Constraints & Assumptions

Inputs contain decimal digits only. Return a canonical result without leading zeros, except that zero is 0. Leading zeros in inputs may be accepted.

Clarifying Questions to Ask

  • Is the nonnegative-result guarantee always valid?
  • Are leading zeros allowed, and must the output normalize them?

What a Strong Answer Covers

  • Right-to-left digit subtraction with borrow propagation.
  • Unequal input lengths and digit decoding without whole-number conversion.
  • Leading-zero cleanup and equal-value inputs.

Follow-up Questions

  • How would you support a negative result when num1 is smaller?
  • How would the same algorithm generalize to another positional base?

Examples

Example 1
Input
('1000', '7')
Output
'993'
Notes
The borrow propagates across two zero columns.
Example 2
Input
('0042', '00000017')
Output
'25'
Notes
Leading zeros affect string length without changing either value.

Constraints

  • •Each operand contains only ASCII decimal digits and has 1 through 2^31 − 1 characters.
  • •The numeric value of num1 is at least that of num2.

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 subtract_decimal_strings(num1: str, num2: str) -> str:
Sample call
subtract_decimal_strings('1000', '7')
Language
Case 1
Input
('1000', '7')
Expected
'993'
Case 2
Input
('0042', '00000017')
Expected
'25'

Browse More Questions

Show the approach

Approach

Align the operands on their right ends and treat absent high digits as zero. Scan from the units column toward the most significant column. Subtract the second digit and the incoming borrow from the first. If the value is negative, add ten and pass a borrow to the next column. Otherwise pass no borrow. Each written digit is final because higher columns can affect only the outgoing borrow. The nonnegative input guarantee ensures no borrow remains after the last column. Remove zeroes from the high end of the reversed buffer, keeping at least one digit, and reverse it.

Time complexity:
O(max(m,n)) for operand lengths m and n.
Space complexity:
O(max(m,n)) for the result buffer.