← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Google SWE coding round, one problem the whole session. Subtraction on big number strings, no BigInteger allowed. Felt manageable at first but the edge cases piled up fast.

Questions Asked (1)

Q1

Given two non-negative integers as strings, compute their difference (num1 minus num2) and return the result as a string. You cannot convert the inputs to a native big integer type; you have to do the subtraction digit by digit. The result can be negative, and leading zeros should be stripped from the output.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I started okay, figured out I needed to handle the sign first by comparing which string was larger, then do the actual subtraction on the bigger minus the smaller.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, compare the two strings as numbers to determine the sign of the result and which number is larger. Then, perform digit-by-digit subtraction from right to left, handling borrows, and finally strip leading zeros from the result. If the result is negative, prepend a minus sign.

Pro tip: Clarify with the interviewer whether the inputs are guaranteed to be valid non-negative integers and whether the result should be returned as a string without leading zeros. Also, mention that you'll handle edge cases like equal numbers and zeros.

1. Compare and Determine Sign

Compare the lengths of the two strings; if lengths differ, the longer string represents the larger number. If lengths are equal, compare lexicographically to determine which is larger. Based on this, decide the sign of the result and which number to subtract from which.

2. Set Up Subtraction

Ensure the larger number is the minuend and the smaller is the subtrahend. Pad the shorter string with leading zeros to align digits for subtraction.

3. Perform Digit-by-Digit Subtraction

Iterate from the least significant digit (rightmost) to the most significant. For each digit, subtract the subtrahend digit and any borrow from the minuend digit. If the result is negative, add 10 and set borrow to 1; otherwise, set borrow to 0. Build the result string in reverse order.

4. Handle Borrow and Strip Leading Zeros

After the loop, if there is a remaining borrow, it indicates an error (should not happen if the larger number was chosen correctly). Remove any leading zeros from the result string. If the result is empty, return '0'.

5. Add Sign and Return

If the original comparison indicated that num1 < num2, prepend a minus sign to the result. Return the final string.

Key Points to Mention

  • Time and space complexity: O(n) time and O(n) space where n is the length of the longer string.
  • Handling of borrow propagation and its analogy to addition with carry.
  • Edge cases: equal numbers, one number is zero, both numbers are zero, and result is zero.
  • Avoiding integer overflow by not converting to native types.
  • String manipulation techniques: reversing strings, padding, and stripping leading zeros.
  • Potential trade-offs: in-place modification vs. creating new strings, and readability vs. optimization.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.