This is a classic interview warm-up because it tests whether we can implement grade-school addition cleanly while handling tricky carry cases (lots of trailing 9s). It is a favorite among interviewers at companies like Google, Meta, and Amazon because the core pattern: carry propagation from right to left, shows up in many array and math problems. This problem teaches us how to solve it in a single backward pass without converting the whole array into an integer.
Problem statement
We are given a large non-negative integer as an array digits, where each element in digits[i] represents one digit of the integer. The digits are stored from most significant to least significant (left to right). Our task is to add 1 to this number and return the resulting digits as an array, preserving the same format and without introducing leading zeros.
Constraints
- 1 <=
digits.length<= 100 - 0 <=
digits[i]<= 9 digitshas no leading 0s
Examples
| Input | Output | Explanation |
| [2, 5, 9] | [2, 6, 0] | Start from the last digit: 9 + 1 = 10, write 0, carry 1. Then 5 + 1 = 6 carry stops, so the prefix 2 remains unchanged. |
| [7, 1, 3, 4] | [7, 1, 3, 5] | The last digit is not 9, so we can increment it directly: 4 + 1 = 5. No carry is produced, so we return immediately. |
| [9, 9, 9, 9] | [1, 0, 0, 0, 0] | Every digit becomes 0 after adding 1 with carry. After processing all digits, a carry remains, so we prepend 1 to form the new most significant digit. |
Optimized approach using carry propagation
Think of this like adding 1 to a number on paper. We start from the least significant digit (the end of the array). If the digit is less than 9, we simply increment it, and we are done. If it is 9, it becomes 0 and creates a carry that must be applied to the digit on the left. This carry may propagate across many digits, but it always moves in one direction: right to left.
Step-by-step algorithm
- Store the length of digits in
len_digits. - Iterate over the
digitsarray from right to left.- If
digits[i] < 9,- Increment it and return immediately (no carry).
- Otherwise,
- Set
digits[i] = 0and continue (carry persists).
- Set
- Decrement
ias we move left.
- If
- If we successfully process the
digitsarray, it means all digits were9. So, create a new array with a leading1followed bynzeros, and return it.
Let’s look at the following illustration to better understand the solution.
Code implementation
Time complexity
The algorithm’s time complexity is O(N), as in the worst case, we scan the whole array from right to left once (all 9s) and touch every digit exactly once.
Space complexity
The algorithm’s space complexity is O(N), becuase in the worst case, when all digits are 9, we must allocate a new list of size N +1, as we need to add a new leading 1, which requires additional space proportional to the number of digits.
Common pitfalls and interview tips
- Forgetting the “all 9s” case: If every digit becomes
0, you must prepend1(e.g.,[9,9] -> [1,0,0]). - Converting digits to an integer: This may overflow in languages with fixed-width integers, and it misses the point of the problem’s constraints.
- Not returning early: Once we increment a digit that is
< 9, the carry stops, and continuing the loop risks incorrect extra modifications.