Plus One

Plus One looks like a warm-up. Just add one to a number, right? But the number is stored as separate digits in an array. That small twist is the whole point. The interviewer wants to see if you handle the carry cleanly, especially the tricky case where every digit is a nine.

🎯 The Problem

You get a number stored as digits and you have to add one to it. Here are the rules.

  • Each box in the array holds one digit.
  • The most important digit comes first.
  • Add one to the whole number and return the new digits.

A carry is the extra one that moves to the next digit when a digit goes past nine. For example, 9 + 1 gives 10. The right digit becomes 0 and a 1 carries to the left.

Let us say the array is [1, 2, 3]. That is the number 123. Add one and you get 124. So the answer is [1, 2, 4].

Input: digits = [1, 2, 3]
Output: [1, 2, 4]
Explanation: 123 + 1 = 124

The hard case is something like [9, 9, 9]. That is 999. Add one and you get 1000. So the array grows by one box. You have to handle that growth.

Here is a picture of the easy case and the hard case side by side.

[1, 2, 3] -> add 1 to last -> 3 becomes 4 -> [1, 2, 4]

[9, 9, 9] -> add 1 to last -> 9 becomes 0 carry 1 -> carry ripples left -> [1, 0, 0, 0]

🐒 Approach 1: Turn It Into a Number (Brute Force)

The idea in one line: glue the digits into one real number, add one, then split it back.

The idea:

  • Walk the array and build the integer 123.
  • Add one to get 124.
  • Split it back into digits.

Why it is weak:

  • Interview inputs can be hundreds of digits long.
  • A normal integer cannot hold a number that big. It overflows.
  • Overflow means the value is too large for the variable, so it wraps around and becomes wrong.
  • Python hides this because its integers grow without limit. But C, C++, and Java break. So it is not safe in general.

Here is the conversion code for that idea:

plus_one_conversion.py
def plus_one(digits):
value = int("".join(str(digit) for digit in digits))
value += 1
return [int(ch) for ch in str(value)]
print(plus_one([1, 2, 9]))

⚑ Approach 2: One Backward Pass With Carry (Best)

The idea in one line: add one to the last digit and walk left, carrying nines, exactly like adding on paper.

How it works:

  • Start at the rightmost digit.
  • If it is less than nine, add one and stop. No carry needed.
  • If it is a nine, set it to zero and carry one to the left.
  • Keep walking left as long as you keep hitting nines.

The all-nines case:

  • If you walk past the left edge and still carry, every digit was a nine.
  • Like 999 becoming 1000.
  • Put a 1 at the front, the rest stay zero. The array grows by one box.

Why it is fast:

  • Each digit is touched once.
  • So it runs in O(n) time, where n is the number of digits.
  • It usually changes the array in place, so the extra space is O(1).

This picture shows the dry run on [9, 9, 9]. Follow the carry as it moves left.

start: [9, 9, 9]

last digit 9 + 1 = 10, set 0 carry 1 -> [9, 9, 0]

next 9 + carry = 10, set 0 carry 1 -> [9, 0, 0]

next 9 + carry = 10, set 0 carry 1 -> [0, 0, 0]

carry still 1, walked off the left, add 1 at front -> [1, 0, 0, 0]

Steps to Solve

  1. Start at the last index of the array.
  2. If that digit is less than nine, add one to it and return the array. You are done.
  3. If that digit is nine, set it to zero and move one step to the left.
  4. Repeat until a digit is less than nine, or until you run off the left edge.
  5. If you ran off the left edge, put a 1 at the front of the array and return it.

This Python version walks the digits from the right and adds a leading one only when needed.

plus_one.py
def plus_one(digits):
for i in range(len(digits) - 1, -1, -1): # walk from the last digit left
if digits[i] < 9: # room to add, no carry needed
digits[i] += 1
return digits
digits[i] = 0 # nine becomes zero, carry left
return [1] + digits # all nines, grow by one box
digits = [1, 2, 3]
print(plus_one(digits))

The output of the above code will be:

[1, 2, 4]

Let us walk through the Python version line by line. Code first, then the why.

def plus_one(digits):
for i in range(len(digits) - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digits

for i in range(len(digits) - 1, -1, -1): walks the indexes from the last one down to zero. We go right to left because addition carries from the small digit to the big digit. So we start where the plus one lands.

if digits[i] < 9: checks if there is room. Any digit below nine can take a plus one without spilling over. So this is the place where the work ends.

digits[i] += 1 adds the one. There is no carry past this point. So the rest of the digits on the left stay the same.

return digits stops right away. Once we add without a carry, the job is done. We do not need to look at any more digits.

digits[i] = 0 runs only when the digit was a nine. Nine plus one is ten. The digit becomes zero and the carry moves to the next loop step on the left.

return [1] + digits runs only if the loop never returned early. That means every digit was a nine, so they are all zeros now. We add a fresh 1 at the front. That is how 999 becomes 1000.

⏱️ Time and Space Complexity

The naive convert idea is fine for tiny inputs but breaks on long numbers because of overflow. The backward pass touches each digit at most once, so it is O(n) time. It usually changes the array in place, so it is O(1) extra space. Only the all-nines case grows the array by one box.

Approach Time Complexity Space Complexity
Convert to number, add, split back O(n) O(n), and overflows on long inputs
Backward pass with carry O(n) O(1) extra, or O(n) only when all nines

Tip

The all-nines case is the one interviewers watch for. Always test your code on [9, 9, 9] out loud. If you handle the array growing by one box, you have handled the trickiest part.

🧩 Key Takeaways

  • βœ… The number is stored as separate digits, with the most important digit first.
  • βœ… Start at the rightmost digit and walk left, just like adding on paper.
  • βœ… A digit below nine takes the plus one and you stop right away.
  • βœ… A nine becomes a zero and carries one to the left.
  • βœ… If every digit is a nine, put a 1 at the front so the array grows by one box.

Check Your Knowledge

4 questions Show quiz Hide quiz

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    How is the number stored in the Plus One problem?

    Why: Each array box holds one digit, and the most important digit comes first.

  2. 2

    Why is converting the digits to one integer risky?

    Why: Inputs can be very long, and a normal integer cannot hold such a big value, so it overflows.

  3. 3

    What happens to a digit that is nine when we add one to it?

    Why: Nine plus one is ten, so the digit becomes zero and a carry moves to the next digit on the left.

  4. 4

    What does the array [9, 9, 9] become after Plus One?

    Why: 999 + 1 = 1000, so the array grows by one box and becomes [1, 0, 0, 0].

πŸš€ What’s Next?