EasyArrayMathUpdated Sep 2026

66. Plus One

Asked at Google

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given an array of digits that spells out a big non-negative integer. The digits are stored from the most significant (leftmost) to the least significant (rightmost). So [1,2,3] means the number 123.

Your job is to add one to that number and return the resulting array of digits, in the same left-to-right order. The input never has a leading zero, except that the number zero itself would be written as [0].

The tricky part is the carry. If the last digit is not 9, you just bump it up and you are done. But if it is 9, it becomes 0 and the carry moves one step left. If every digit is 9, the answer needs one extra digit, for example [9,9] becomes [1,0,0].

Add one to the number 123

digits123012The rightmost digit is the ones place, so that iswhere we start.result1240123 is not 9, so it becomes 4 and there is no carry tothe left.
The array [1,2,3] means 123. Adding one gives 124, so only the last digit changes.

Constraints

  • ●1 <= digits.length <= 100
  • ●0 <= digits[i] <= 9
  • ●digits does not contain any leading 0's

Examples

Example 1
Inputdigits = [1,2,3]
Output[1,2,4]
WhyThe array stands for 123. Adding one gives 124, and the last digit 3 simply becomes 4 with no carry.
Example 2
Inputdigits = [4,3,2,1]
Output[4,3,2,2]
WhyThe array stands for 4321. Adding one gives 4322, so only the final digit changes from 1 to 2.
Example 3
Inputdigits = [9]
Output[1,0]
WhyThe array stands for 9. Adding one gives 10, so the single 9 turns into 0 and a new leading 1 appears, making the array one element longer.

Asked At

1 companies
CompanyDifficulty
GoogleEasyView all Google questions →

How to Think About It

1

Brute force: convert the array to an integer, add one, convert back to an array — risks overflow for very large inputs.

2

Handle carry: start from the least significant digit, add one, and propagate carry to the left.

3

Optimize for no-carry case: if the last digit is not 9, simply increment it and return — covers most cases in O(1).

4

Handle all-9s case: if every digit is 9, the result has one more digit (e.g., 999 becomes 1000), so create a new array.

5

Optimal: O(n) time, O(1) extra space (or O(n) if a new array is needed for the all-9s case). Single pass from right to left.

Optimal Approach

Traverse the digits array from right to left. If a digit is less than 9, increment it by one and return immediately (no carry to propagate). If the digit is 9, set it to 0 and continue to the next digit (carry propagates). If the loop completes, every digit was 9, so create a new array of size n+1 with the first digit as 1 and the rest as 0. This runs in O(n) time and uses O(1) extra space in the common case.

Walk right to left, carry the 9s

digits129012iStart at the last digit. It is 9, so it must become 0and carry.digits120012iThe 9 is now 0. Move left and check the next digit.digits1300122 is below 9, so add one to it and stop. The answer is[1,3,0].if digit < 9: digit += 1, returnelse: digit = 0, carry leftall 9s -> [1] + zeros
Each 9 turns into 0 and passes the carry left; the first digit below 9 absorbs it.
ComplexityTime O(n)Space O(1)We scan from the right and stop at the first digit below 9, so the cost is the length of the trailing run of 9s; the all-9s case needs a new array of size n+1.

What Trips People Up in Real Interviews

1

Start with the simplest case: last digit is not 9. Show you handle the easy path first.

2

Discuss the all-9s edge case explicitly — this is where most bugs happen.

3

Ask whether the input can be empty or very large to show you think about constraints.

4

Avoid converting to an integer type if the number of digits is very large — stay array-based.

5

Mention that this is a great warm-up for carry-propagation problems like adding two numbers.

Solution Code

O(n) time
def plusOne(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

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Plus One problem?

You are given a large integer represented as an integer array digits, where each digits[i] is the i-th digit of the number (most significant digit first). Increment the integer by one and return the resulting array of digits. The array does not contain any leading zeros except for the number zero itself.

How do you solve Plus One?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Plus One?

Plus One is asked at Google. It is a easy difficulty problem.

What are common mistakes on Plus One?
  • Start with the simplest case: last digit is not 9. Show you handle the easy path first.
  • Discuss the all-9s edge case explicitly — this is where most bugs happen.
  • Ask whether the input can be empty or very large to show you think about constraints.
  • Avoid converting to an integer type if the number of digits is very large — stay array-based.
  • Mention that this is a great warm-up for carry-propagation problems like adding two numbers.